annotate service/mqtt_to_rdf/rdflib_debug_patches.py @ 1648:3059f31b2dfa

more performance work
author drewp@bigasterisk.com
date Fri, 17 Sep 2021 11:10:18 -0700
parents 413a280828bf
children 82ddd3e6b227
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
10
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
11 def patchSlimReprs():
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
12 """From: rdflib.term.URIRef('foo')
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
13 To: U('foo')
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
14 """
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
15
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
16 def ur(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
17 clsName = "U" if self.__class__ is rdflib.term.URIRef else self.__class__.__name__
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
18 return """%s(%s)""" % (clsName, super(rdflib.term.URIRef, self).__repr__())
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
19
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
20 rdflib.term.URIRef.__repr__ = ur
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
21
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
22 def br(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
23 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
24 return """%s(%s)""" % (clsName, super(rdflib.term.BNode, self).__repr__())
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
25
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
26 rdflib.term.BNode.__repr__ = br
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
27
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
28 def vr(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
29 clsName = "V" if self.__class__ is rdflib.term.Variable else self.__class__.__name__
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
30 return """%s(%s)""" % (clsName, super(rdflib.term.Variable, self).__repr__())
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
31
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
32 rdflib.term.Variable.__repr__ = vr
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
33
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
34
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
35 def patchBnodeCounter():
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
36 """From: rdflib.terms.BNode('ne7bb4a51624993acdf51cc5d4e8add30e1')
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
37 To: BNode('f-6-1')
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 serial = itertools.count()
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
40
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
41 def n(cls, value=None, _sn_gen='', _prefix='') -> BNode:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
42 if value is None:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
43 value = 'N-%s' % next(serial)
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
44 return rdflib.term.Identifier.__new__(cls, value)
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
45
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
46 rdflib.term.BNode.__new__ = n
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 def newBlankNode(self, uri=None, why=None):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
49 if uri is None:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
50 self.counter += 1
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
51 bn = BNode('f-%s-%s' % (self.number, self.counter))
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
52 else:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
53 bn = BNode(uri.split('#').pop().replace('_', 'b'))
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
54 return bn
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
55
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
56 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode