Mercurial > code > home > repos > homeauto
annotate service/mqtt_to_rdf/rdflib_debug_patches.py @ 1675:3cf7f313b285
forgot a file in the BNode subtypes commit a few steps back
author | drewp@bigasterisk.com |
---|---|
date | Wed, 22 Sep 2021 01:03:25 -0700 |
parents | 2b905c07e82b |
children | 37710d28890b |
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 | |
8 from rdflib import BNode | |
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 |
1595 | 12 |
13 def patchSlimReprs(): | |
14 """From: rdflib.term.URIRef('foo') | |
15 To: U('foo') | |
16 """ | |
17 | |
18 def ur(self): | |
19 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
|
20 s = super(rdflib.term.URIRef, self).__str__() |
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
21 if s.startswith(str(ROOM)): |
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
22 s = ':' + s[len(ROOM):] |
82ddd3e6b227
abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents:
1595
diff
changeset
|
23 return """%s(%s)""" % (clsName, s) |
1595 | 24 |
25 rdflib.term.URIRef.__repr__ = ur | |
26 | |
27 def br(self): | |
28 clsName = "BNode" if self.__class__ is rdflib.term.BNode else self.__class__.__name__ | |
29 return """%s(%s)""" % (clsName, super(rdflib.term.BNode, self).__repr__()) | |
30 | |
31 rdflib.term.BNode.__repr__ = br | |
32 | |
33 def vr(self): | |
34 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
|
35 return """%s(%s)""" % (clsName, '?' + super(rdflib.term.Variable, self).__str__()) |
1595 | 36 |
37 rdflib.term.Variable.__repr__ = vr | |
38 | |
39 | |
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
|
40 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
|
41 """From: rdflib.terms.BNode('ne7bb4a51624993acdf51cc5d4e8add30e1' |
1595 | 42 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
|
43 |
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
|
44 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
|
45 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
|
46 always get short ids. |
1595 | 47 """ |
48 serial = itertools.count() | |
49 | |
50 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
|
51 if always or value is None: |
1595 | 52 value = 'N-%s' % next(serial) |
53 return rdflib.term.Identifier.__new__(cls, value) | |
54 | |
55 rdflib.term.BNode.__new__ = n | |
56 | |
57 def newBlankNode(self, uri=None, why=None): | |
58 if uri is None: | |
59 self.counter += 1 | |
60 bn = BNode('f-%s-%s' % (self.number, self.counter)) | |
61 else: | |
62 bn = BNode(uri.split('#').pop().replace('_', 'b')) | |
63 return bn | |
64 | |
65 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode |