rdkit.VLib.NodeLib.DbMolSupply

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

# Source Code for Module rdkit.VLib.NodeLib.DbMolSupply

     1  #  $Id$
     2  #
     3  #  Copyright (C) 2003 Rational Discovery LLC
     4  #     All Rights Reserved
     5  #
     6  import sys, os.path
     7  from rdkit import RDConfig
     8  from rdkit.VLib.Supply import SupplyNode
     9  from rdkit import Chem
    10  from rdkit.Chem.Suppliers import DbMolSupplier
    11
    12
    13 -class DbMolSupplyNode(SupplyNode):
    14    """ Supplies molecules from a db result set:
    15
    16    Sample Usage:
    17      >>> from rdkit.Dbase.DbConnection import DbConnect
    18      >>> dbName = os.path.join(RDConfig.RDCodeDir,'Chem','Fingerprints',\
    19                               'test_data','data.gdb')
    20      >>> conn = DbConnect(dbName,'simple_mols')
    21      >>> dataset = conn.GetData()
    22      >>> suppl = DbMolSupplyNode(dataset)
    23      >>> ms = [x for x in suppl]
    24      >>> len(ms)
    25      12
    26      >>> ms[0].GetProp("ID")
    27      'ether-1'
    28      >>> ms[10].GetProp("ID")
    29      'acid-4'
    30      >>> suppl.reset()
    31      >>> suppl.next().GetProp("ID")
    32      'ether-1'
    33      >>> suppl.next().GetProp("ID")
    34      'acid-1'
    35      >>> suppl.reset()
    36
    37    """
    38
    39 -  def __init__(self, dbResults, **kwargs):
    40      SupplyNode.__init__(self, **kwargs)
    41      self._dbResults = dbResults
    42      self._supplier = DbMolSupplier.RandomAccessDbMolSupplier(self._dbResults, **kwargs)
    43
    44 -  def reset(self):
    45      SupplyNode.reset(self)
    46      self._supplier.Reset()
    47
    48 -  def next(self):
    49      """
    50
    51      """
    52      return self._supplier.next()
    53
    54
    55 -def GetNode(dbName, tableName):
    56    from rdkit.Dbase.DbConnection import DbConnect
    57    conn = DbConnect(dbName, tableName)
    58    return DbMolSupplyNode(conn.GetData())
    59
    60
    61  #------------------------------------
    62  #
    63  #  doctest boilerplate
    64  #
    65 -def _test():
    66    import doctest, sys
    67    return doctest.testmod(sys.modules["__main__"])
    68
    69
    70  if __name__ == '__main__':
    71    import sys
    72    failed, tried = _test()
    73    sys.exit(failed)
    74