rdkit.VLib.Transform

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

# Source Code for Module rdkit.VLib.Transform

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