Package rdkit :: Package VLib :: Module Node

[hide private]
[frames] | no frames]

# Source Code for Module rdkit.VLib.Node

      1  #  $Id$
      2  #
      3  #  Copyright (C) 2003-2006 Rational Discovery LLC
      4  #     All Rights Reserved
      5  #
      6  import sys
      7  from rdkit import six
      8
      9 -class VLibNode(object):
     10    """ base class for all virtual library nodes,
     11    defines minimal required interface
     12
     13    """
     14 -  def __init__(self,*args,**kwargs):
     15      self._children = []
     16      self._parents = []
     17
     18    #------------------------------------
     19    #
     20    #  Iteration
     21    #
     22 -  def __iter__(self):
     23      """ part of the iterator interface """
     24      self.reset()
     25      return self
     26 -  def next(self):
     27      """ part of the iterator interface
     28
     29        raises StopIteration on failure
     30      """
     31      pass
     32 -  def reset(self):
     33      """ resets our iteration state
     34
     35      """
     36      for parent in self.GetParents():
     37        parent.reset()
     38
     39
     40    #------------------------------------
     41    #
     42    #  Library graph operations
     43    #  Probably most of these won't need to be reimplemented in
     44    #  child classes
     45    #
     46 -  def AddChild(self,child,notify=1):
     47      """
     48
     49      >>> p1 = VLibNode()
     50      >>> p2 = VLibNode()
     51      >>> c1 = VLibNode()
     52      >>> p1.AddChild(c1)
     53      >>> len(c1.GetParents())
     54      1
     55      >>> len(p1.GetChildren())
     56      1
     57      >>> p2.AddChild(c1,notify=0)
     58      >>> len(c1.GetParents())
     59      1
     60      >>> len(p2.GetChildren())
     61      1
     62      >>> c1.AddParent(p2,notify=0)
     63      >>> len(c1.GetParents())
     64      2
     65      >>> len(p2.GetChildren())
     66      1
     67
     68      """
     69      self._children.append(child)
     70      if notify:
     71        child.AddParent(self,notify=0)
     72 -  def RemoveChild(self,child,notify=1):
     73      """
     74      >>> p1 = VLibNode()
     75      >>> c1 = VLibNode()
     76      >>> p1.AddChild(c1)
     77      >>> len(c1.GetParents())
     78      1
     79      >>> len(p1.GetChildren())
     80      1
     81      >>> p1.RemoveChild(c1)
     82      >>> len(c1.GetParents())
     83      0
     84      >>> len(p1.GetChildren())
     85      0
     86      """
     87      self._children.remove(child)
     88      if notify:
     89        child.RemoveParent(self,notify=0)
     90 -  def GetChildren(self):
     91      return tuple(self._children)
     92
     93 -  def AddParent(self,parent,notify=1):
     94      """
     95      >>> p1 = VLibNode()
     96      >>> p2 = VLibNode()
     97      >>> c1 = VLibNode()
     98      >>> c1.AddParent(p1)
     99      >>> len(c1.GetParents())
    100      1
    101      >>> len(p1.GetChildren())
    102      1
    103      >>> c1.AddParent(p2,notify=0)
    104      >>> len(c1.GetParents())
    105      2
    106      >>> len(p2.GetChildren())
    107      0
    108      >>> p2.AddChild(c1,notify=0)
    109      >>> len(c1.GetParents())
    110      2
    111      >>> len(p2.GetChildren())
    112      1
    113      """
    114      self._parents.append(parent)
    115      if notify:
    116        parent.AddChild(self,notify=0)
    117 -  def RemoveParent(self,parent,notify=1):
    118      """
    119      >>> p1 = VLibNode()
    120      >>> c1 = VLibNode()
    121      >>> p1.AddChild(c1)
    122      >>> len(c1.GetParents())
    123      1
    124      >>> len(p1.GetChildren())
    125      1
    126      >>> c1.RemoveParent(p1)
    127      >>> len(c1.GetParents())
    128      0
    129      >>> len(p1.GetChildren())
    130      0
    131      """
    132      self._parents.remove(parent)
    133      if notify:
    134        parent.RemoveChild(self,notify=0)
    135 -  def GetParents(self):
    136      return tuple(self._parents)
    137
    138 -  def Destroy(self,notify=1,propagateDown=0,propagateUp=0):
    139      """
    140      >>> p1 = VLibNode()
    141      >>> p2 = VLibNode()
    142      >>> c1 = VLibNode()
    143      >>> c2 = VLibNode()
    144      >>> p1.AddChild(c1)
    145      >>> p2.AddChild(c1)
    146      >>> p2.AddChild(c2)
    147      >>> len(c1.GetParents())
    148      2
    149      >>> len(c2.GetParents())
    150      1
    151      >>> len(p1.GetChildren())
    152      1
    153      >>> len(p2.GetChildren())
    154      2
    155      >>> c1.Destroy(propagateUp=1)
    156      >>> len(p2.GetChildren())
    157      0
    158      >>> len(c1.GetParents())
    159      0
    160      >>> len(c2.GetParents())
    161      0
    162
    163      """
    164      #sys.stderr.write('DESTROY: %s\n'%(str(self)))
    165      if hasattr(self,'_destroyed'): return
    166      self._destroyed=1
    167
    168      if notify:
    169        for o in self.GetChildren():
    170          o.RemoveParent(self,notify=0)
    171          if propagateDown:
    172            o.Destroy(notify=1,propagateDown=1,propagateUp=propagateUp)
    173        for o in self.GetParents():
    174          #sys.stderr.write('\tparent: %s\n'%(str(o)))
    175          o.RemoveChild(self,notify=0)
    176          if propagateUp:
    177            o.Destroy(notify=1,propagateDown=propagateDown,propagateUp=1)
    178      self._children = []
    179      self._parents = []
    180
    181  if six.PY3:
    182      VLibNode.__next__ = VLibNode.next
    183
    184
    185  #------------------------------------
    186  #
    187  #  doctest boilerplate
    188  #
    189 -def _test():
    190    import doctest,sys
    191    return doctest.testmod(sys.modules["__main__"])
    192
    193  if __name__ == '__main__':
    194    import sys
    195    failed,tried = _test()
    196    sys.exit(failed)
    197