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  from rdkit import six
     7  from rdkit.VLib.Node import VLibNode
     8
     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
    49 -  def __init__(self, func=None, **kwargs):
    50      VLibNode.__init__(self, **kwargs)
    51      self._func = func
    52
    53 -  def next(self):
    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 = self._func(*args)
    64      else:
    65        res = args
    66      return res
    67
    68
    69  if six.PY3:
    70    TransformNode.__next__ = TransformNode.next
    71
    72
    73  # ------------------------------------
    74  #
    75  #  doctest boilerplate
    76  #
    77 -def _runDoctests(verbose=None):  # pragma: nocover
    78    import sys
    79    import doctest
    80    failed, _ = doctest.testmod(optionflags=doctest.ELLIPSIS, verbose=verbose)
    81    sys.exit(failed)
    82
    83
    84  if __name__ == '__main__':  # pragma: nocover
    85    _runDoctests()
    86