/usr/lib/python2.7/dist-packages/rdflib/BNode.py is in python-rdflib 2.4.2-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | # TODO: where can we move _unique_id and _serial_number_generator?
from string import ascii_letters
from random import choice
try:
from hashlib import md5
except ImportError:
from md5 import md5
def _unique_id():
"""Create a (hopefully) unique prefix"""
id = ""
for i in xrange(0,8):
id += choice(ascii_letters)
return id
def _serial_number_generator():
i = 0
while 1:
yield i
i = i + 1
from rdflib.Identifier import Identifier
from rdflib.syntax.xml_names import is_ncname
import threading
bNodeLock = threading.RLock()
class BNode(Identifier):
"""
Blank Node: http://www.w3.org/TR/rdf-concepts/#section-blank-nodes
"In non-persistent O-O software construction, support for object
identity is almost accidental: in the simplest implementation,
each object resides at a certain address, and a reference to the
object uses that address, which serves as immutable object
identity.
...
Maintaining object identity in shared databases raises problems:
every client that needs to create objects must obtain a unique
identity for them; " -- Bertand Meyer
"""
__slots__ = ()
def __new__(cls, value=None, # only store implementations should pass in a value
_sn_gen=_serial_number_generator(), _prefix=_unique_id()):
if value==None:
# so that BNode values do not
# collide with ones created with a different instance of this module
# at some other time.
bNodeLock.acquire()
node_id = _sn_gen.next()
bNodeLock.release()
value = "%s%s" % (_prefix, node_id)
else:
# TODO: check that value falls within acceptable bnode value range
# for RDF/XML needs to be something that can be serialzed as a nodeID
# for N3 ??
# Unless we require these constraints be enforced elsewhere?
pass #assert is_ncname(unicode(value)), "BNode identifiers must be valid NCNames"
return Identifier.__new__(cls, value)
def n3(self):
return "_:%s" % self
def __getnewargs__(self):
return (unicode(self), )
def __reduce__(self):
return (BNode, (unicode(self),))
def __ne__(self, other):
return not self.__eq__(other)
def __eq__(self, other):
"""
>>> from rdflib.URIRef import URIRef
>>> from rdflib.BNode import BNode
>>> BNode("foo")==None
False
>>> BNode("foo")==URIRef("foo")
False
>>> URIRef("foo")==BNode("foo")
False
>>> BNode("foo")!=URIRef("foo")
True
>>> URIRef("foo")!=BNode("foo")
True
"""
if isinstance(other, BNode):
return unicode(self)==unicode(other)
else:
return False
def __str__(self):
return self.encode("unicode-escape")
def __repr__(self):
return """rdflib.BNode('%s')""" % str(self)
def md5_term_hash(self):
d = md5(str(self))
d.update("B")
return d.hexdigest()
|