Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/inference/rdflib_debug_patches.py @ 1727:23e6154e6c11
file moves
author | drewp@bigasterisk.com |
---|---|
date | Tue, 20 Jun 2023 23:26:24 -0700 |
parents | service/mqtt_to_rdf/rdflib_debug_patches.py@37710d28890b |
children |
rev | line source |
---|---|
1595 | 1 """rdflib patches for prettier debug outut""" |
2 | |
3 import itertools | |
4 | |
5 import rdflib | |
6 import rdflib.plugins.parsers.notation3 | |
7 import rdflib.term | |
1691 | 8 from rdflib import BNode, RDF |
1595 | 9 |
1665
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
10 ROOM = rdflib.Namespace('http://projects.bigasterisk.com/room/') |
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
11 |
1691 | 12 ABBREVIATE = { |
13 '': ROOM, | |
14 'rdf': RDF, | |
15 } | |
16 | |
1595 | 17 |
18 def patchSlimReprs(): | |
19 """From: rdflib.term.URIRef('foo') | |
20 To: U('foo') | |
21 """ | |
22 | |
23 def ur(self): | |
24 clsName = "U" if self.__class__ is rdflib.term.URIRef else self.__class__.__name__ | |
1665
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
25 s = super(rdflib.term.URIRef, self).__str__() |
1691 | 26 for short, long in ABBREVIATE.items(): |
27 if s.startswith(str(long)): | |
28 s = short + ':' + s[len(str(long)):] | |
29 break | |
30 | |
1665
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
31 return """%s(%s)""" % (clsName, s) |
1595 | 32 |
33 rdflib.term.URIRef.__repr__ = ur | |
34 | |
35 def br(self): | |
36 clsName = "BNode" if self.__class__ is rdflib.term.BNode else self.__class__.__name__ | |
37 return """%s(%s)""" % (clsName, super(rdflib.term.BNode, self).__repr__()) | |
38 | |
39 rdflib.term.BNode.__repr__ = br | |
40 | |
41 def vr(self): | |
42 clsName = "V" if self.__class__ is rdflib.term.Variable else self.__class__.__name__ | |
1665
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
43 return """%s(%s)""" % (clsName, '?' + super(rdflib.term.Variable, self).__str__()) |
1595 | 44 |
45 rdflib.term.Variable.__repr__ = vr | |
46 | |
47 | |
1671
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
48 def patchBnodeCounter(always=False): |
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
49 """From: rdflib.terms.BNode('ne7bb4a51624993acdf51cc5d4e8add30e1' |
1595 | 50 To: BNode('f-6-1') |
1671
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
51 |
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
52 BNode creation can override this, which might matter when adding BNodes that |
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
53 are known to be the same as each other. Set `always` to disregard this and |
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
54 always get short ids. |
1595 | 55 """ |
56 serial = itertools.count() | |
57 | |
58 def n(cls, value=None, _sn_gen='', _prefix='') -> BNode: | |
1671
2b905c07e82b
try a bnode test mode improvement, but it's not so useful if you parse graphs with reused bnodes in them
drewp@bigasterisk.com
parents:
1665
diff
changeset
|
59 if always or value is None: |
1595 | 60 value = 'N-%s' % next(serial) |
61 return rdflib.term.Identifier.__new__(cls, value) | |
62 | |
63 rdflib.term.BNode.__new__ = n | |
64 | |
65 def newBlankNode(self, uri=None, why=None): | |
66 if uri is None: | |
67 self.counter += 1 | |
68 bn = BNode('f-%s-%s' % (self.number, self.counter)) | |
69 else: | |
70 bn = BNode(uri.split('#').pop().replace('_', 'b')) | |
71 return bn | |
72 | |
73 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode |