rdkit.VLib.Transform

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

# Source Code for Module rdkit.VLib.Transform

     1  #  $Id: Transform.py 997 2009-02-25 06:12:43Z glandrum $
     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 TransformNode(VLibNode):
    11    """ base class for nodes which filter their input
    12
    13    Assumptions:
    14
    15      - transform function takes a number of arguments equal to the
    16        number of inputs we have.  We return whatever it returns
    17
    18      - inputs (parents) can be stepped through in lockstep
    19
    20    Usage Example:
    21      >>> from rdkit.VLib.Supply import SupplyNode
    22      >>> def func(a,b):
    23      ...   return a+b
    24      >>> tform = TransformNode(func)
    25      >>> suppl1 = SupplyNode(contents=[1,2,3,3])
    26      >>> suppl2 = SupplyNode(contents=[1,2,3,1])
    27      >>> tform.AddParent(suppl1)
    28      >>> tform.AddParent(suppl2)
    29      >>> v = [x for x in tform]
    30      >>> v
    31      [2, 4, 6, 4]
    32      >>> tform.reset()
    33      >>> v = [x for x in tform]
    34      >>> v
    35      [2, 4, 6, 4]
    36
    37      If we don't provide a function, just return the inputs:
    38      >>> tform = TransformNode()
    39      >>> suppl1 = SupplyNode(contents=[1,2,3,3])
    40      >>> suppl2 = SupplyNode(contents=[1,2,3,1])
    41      >>> tform.AddParent(suppl1)
    42      >>> tform.AddParent(suppl2)
    43      >>> v = [x for x in tform]
    44      >>> v
    45      [(1, 1), (2, 2), (3, 3), (3, 1)]
    46
    47    """
    48 -  def __init__(self,func=None,**kwargs):
    49      VLibNode.__init__(self,**kwargs)
    50      self._func = func
    51
    52 -  def next(self):
    53      done = 0
    54      parent = self.GetParents()[0]
    55      args = []
    56      try:
    57        for parent in self.GetParents():
    58          args.append(parent.next())
    59      except StopIteration:
    60        raise StopIteration
    61      args = tuple(args)
    62      if self._func is not None:
    63        res = apply(self._func,args)
    64      else:
    65        res = args
    66      return res
    67
    68
    69
    70  #------------------------------------
    71  #
    72  #  doctest boilerplate
    73  #
    74 -def _test():
    75    import doctest,sys
    76    return doctest.testmod(sys.modules["__main__"])
    77
    78  if __name__ == '__main__':
    79    import sys
    80    failed,tried = _test()
    81    sys.exit(failed)
    82