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