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
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
1691
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
8 from rdflib import BNode, RDF
1595
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
1691
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
12 ABBREVIATE = {
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
13 '': ROOM,
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
14 'rdf': RDF,
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
15 }
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
16
1595
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 patchSlimReprs():
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
19 """From: rdflib.term.URIRef('foo')
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
20 To: U('foo')
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
23 def ur(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
26 for short, long in ABBREVIATE.items():
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
27 if s.startswith(str(long)):
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
28 s = short + ':' + s[len(str(long)):]
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
29 break
37710d28890b more debug uri compression
drewp@bigasterisk.com
parents: 1671
diff changeset
30
1665
82ddd3e6b227 abbreviate my specific debug lines some more
drewp@bigasterisk.com
parents: 1595
diff changeset
31 return """%s(%s)""" % (clsName, s)
1595
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 rdflib.term.URIRef.__repr__ = ur
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 br(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
36 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
37 return """%s(%s)""" % (clsName, super(rdflib.term.BNode, self).__repr__())
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 rdflib.term.BNode.__repr__ = br
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 vr(self):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
44
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
45 rdflib.term.Variable.__repr__ = vr
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
46
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
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 serial = itertools.count()
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
57
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
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
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
60 value = 'N-%s' % next(serial)
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
61 return rdflib.term.Identifier.__new__(cls, value)
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
62
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
63 rdflib.term.BNode.__new__ = n
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 def newBlankNode(self, uri=None, why=None):
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
66 if uri is None:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
67 self.counter += 1
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
68 bn = BNode('f-%s-%s' % (self.number, self.counter))
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
69 else:
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
70 bn = BNode(uri.split('#').pop().replace('_', 'b'))
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
71 return bn
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
72
413a280828bf extract module of rdflib output patches
drewp@bigasterisk.com
parents:
diff changeset
73 rdflib.plugins.parsers.notation3.Formula.newBlankNode = newBlankNode