home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / BNode.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.3 KB  |  89 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from string import ascii_letters
  5. from random import choice
  6.  
  7. try:
  8.     from hashlib import md5
  9. except ImportError:
  10.     from md5 import md5
  11.  
  12.  
  13. def _unique_id():
  14.     '''Create a (hopefully) unique prefix'''
  15.     id = ''
  16.     for i in xrange(0, 8):
  17.         id += choice(ascii_letters)
  18.     
  19.     return id
  20.  
  21.  
  22. def _serial_number_generator():
  23.     i = 0
  24.     while None:
  25.         yield i
  26.         i = i + 1
  27.         continue
  28.         return None
  29.  
  30. from rdflib.Identifier import Identifier
  31. from rdflib.syntax.xml_names import is_ncname
  32. import threading
  33. bNodeLock = threading.RLock()
  34.  
  35. class BNode(Identifier):
  36.     '''
  37.     Blank Node: http://www.w3.org/TR/rdf-concepts/#section-blank-nodes
  38.  
  39.     "In non-persistent O-O software construction, support for object
  40.     identity is almost accidental: in the simplest implementation,
  41.     each object resides at a certain address, and a reference to the
  42.     object uses that address, which serves as immutable object
  43.     identity.
  44.  
  45.     ...
  46.  
  47.     Maintaining object identity in shared databases raises problems:
  48.     every client that needs to create objects must obtain a unique
  49.     identity for them; " -- Bertand Meyer
  50.     '''
  51.     __slots__ = ()
  52.     
  53.     def __new__(cls, value = None, _sn_gen = _serial_number_generator(), _prefix = _unique_id()):
  54.         if value == None:
  55.             bNodeLock.acquire()
  56.             node_id = _sn_gen.next()
  57.             bNodeLock.release()
  58.             value = '%s%s' % (_prefix, node_id)
  59.         
  60.         return Identifier.__new__(cls, value)
  61.  
  62.     
  63.     def n3(self):
  64.         return '_:%s' % self
  65.  
  66.     
  67.     def __getnewargs__(self):
  68.         return (unicode(self),)
  69.  
  70.     
  71.     def __reduce__(self):
  72.         return (BNode, (unicode(self),))
  73.  
  74.     
  75.     def __str__(self):
  76.         return self.encode('unicode-escape')
  77.  
  78.     
  79.     def __repr__(self):
  80.         return "rdflib.BNode('%s')" % str(self)
  81.  
  82.     
  83.     def md5_term_hash(self):
  84.         d = md5(str(self))
  85.         d.update('B')
  86.         return d.hexdigest()
  87.  
  88.  
  89.