Package rdkit :: Package VLib :: Module Filter
[hide private]
[frames] | no frames]

Source Code for Module rdkit.VLib.Filter

  1  #  $Id$ 
  2  # 
  3  #  Copyright (C) 2003 Rational Discovery LLC 
  4  #     All Rights Reserved 
  5  # 
  6  import sys 
  7   
  8  from rdkit.VLib.Node import VLibNode 
  9   
10 -class FilterNode(VLibNode):
11 """ base class for nodes which filter their input 12 13 Assumptions: 14 15 - filter function takes a number of arguments equal to the 16 number of inputs we have. It returns a bool 17 18 - inputs (parents) can be stepped through in lockstep 19 20 - we return a tuple if there's more than one input 21 22 Usage Example: 23 >>> from rdkit.VLib.Supply import SupplyNode 24 >>> def func(a,b): 25 ... return a+b < 5 26 >>> filt = FilterNode(func=func) 27 >>> suppl1 = SupplyNode(contents=[1,2,3,3]) 28 >>> suppl2 = SupplyNode(contents=[1,2,3,1]) 29 >>> filt.AddParent(suppl1) 30 >>> filt.AddParent(suppl2) 31 >>> v = [x for x in filt] 32 >>> v 33 [(1, 1), (2, 2), (3, 1)] 34 >>> filt.reset() 35 >>> v = [x for x in filt] 36 >>> v 37 [(1, 1), (2, 2), (3, 1)] 38 >>> filt.Destroy() 39 40 Negation is also possible: 41 >>> filt = FilterNode(func=func,negate=1) 42 >>> suppl1 = SupplyNode(contents=[1,2,3,3]) 43 >>> suppl2 = SupplyNode(contents=[1,2,3,1]) 44 >>> filt.AddParent(suppl1) 45 >>> filt.AddParent(suppl2) 46 >>> v = [x for x in filt] 47 >>> v 48 [(3, 3)] 49 >>> filt.Destroy() 50 51 With no function, just return the inputs: 52 >>> filt = FilterNode() 53 >>> suppl1 = SupplyNode(contents=[1,2,3,3]) 54 >>> filt.AddParent(suppl1) 55 >>> v = [x for x in filt] 56 >>> v 57 [1, 2, 3, 3] 58 >>> filt.Destroy() 59 60 61 62 """
63 - def __init__(self,func=None,negate=0,**kwargs):
64 VLibNode.__init__(self,**kwargs) 65 self._func = func 66 self._negate = negate
67 - def SetNegate(self,state):
68 self._negate = state
69 - def Negate(self):
70 return self._negate
71
72 - def next(self):
73 done = 0 74 parents = self.GetParents() 75 while 1: 76 args = [] 77 try: 78 for parent in parents: 79 args.append(parent.next()) 80 except StopIteration: 81 raise StopIteration 82 args = tuple(args) 83 if self._func is not None: 84 r = apply(self._func,args) 85 if self._negate: 86 r = not r 87 #sys.stderr.write('\t\tNEGATE -> %d\n'%(r)) 88 if r: 89 res = args 90 break 91 else: 92 res = args 93 break 94 if len(parents)==1: 95 res = res[0] 96 return res
97 98 99 100 #------------------------------------ 101 # 102 # doctest boilerplate 103 #
104 -def _test():
105 import doctest,sys 106 return doctest.testmod(sys.modules["__main__"])
107 108 if __name__ == '__main__': 109 import sys 110 failed,tried = _test() 111 sys.exit(failed) 112