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