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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1595
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
1 """rdflib patches for prettier debug outut"""
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
2
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
3 import itertools
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
4
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
5 import rdflib
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
6 import rdflib.plugins.parsers.notation3
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
7 import rdflib.term
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
8 from rdflib import BNode
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
12
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
13 def patchSlimReprs():
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
14 """From: rdflib.term.URIRef('foo')
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
15 To: U('foo')
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
16 """
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
17
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
18 def ur(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
24
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
25 rdflib.term.URIRef.__repr__ = ur
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
26
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
27 def br(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
28 clsName = "BNode" if self.__class__ is rdflib.term.BNode else self.__class__.__name__
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
29 return """%s(%s)""" % (clsName, super(rdflib.term.BNode, self).__repr__())
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
30
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
31 rdflib.term.BNode.__repr__ = br
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
32
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
33 def vr(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
36
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
37 rdflib.term.Variable.__repr__ = vr
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
38
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
47 """
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
48 serial = itertools.count()
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
49
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
52 value = 'N-%s' % next(serial)
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
53 return rdflib.term.Identifier.__new__(cls, value)
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
54
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
55 rdflib.term.BNode.__new__ = n
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
56
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
57 def newBlankNode(self, uri=None, why=None):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
58 if uri is None:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
59 self.counter += 1
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
60 bn = BNode('f-%s-%s' % (self.number, self.counter))
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
61 else:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
62 bn = BNode(uri.split('#').pop().replace('_', 'b'))
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
63 return bn
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
64
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
65 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode