Package rdkit :: Package Dbase :: Module StorageUtils

[hide private]
[frames] | no frames]

# Module StorageUtils

source code

    Various storage (molecular and otherwise) functionality

Functions | [hide private]

ValidateRDId(id) returns whether or not an RDId is valid | source code

RDIdToInt(id, validate=1) Returns the integer index for a given RDId Throws a ValueError on error | source code

IndexToRDId(idx, leadText='RDCmpd') Converts an integer index into an RDId | source code

GetNextId(conn, table, idColName='Id') returns the next available Id in the database | source code

GetNextRDId(conn, table, idColName='Id', leadText='') returns the next available RDId in the database | source code

RegisterItem(conn, table, value, columnName, data=None, id='', idColName='Id', leadText='RDCmpd')
>>> dbName = RDConfig.RDTestDatabase >>> conn = DbConnect(dbName) >>> tblName = 'StorageTest' >>> conn.AddTable(tblName,'id varchar(32) not null primary key,label varchar(40),val int') >>> RegisterItem(conn,tblName,'label1','label',['label1',1])==(1, 'RDCmpd-000-001-1') True >>> RegisterItem(conn,tblName,'label2','label',['label2',1])==(1, 'RDCmpd-000-002-2') True >>> RegisterItem(conn,tblName,'label1','label',['label1',1])==(0, 'RDCmpd-000-001-1') True >>> str(GetNextRDId(conn,tblName)) 'RDCmpd-000-003-3' >>> tuple(conn.GetData(table=tblName)[0])==('RDCmpd-000-001-1', 'label1', 1) True

source code

RegisterItems(conn, table, values, columnName, rows, startId='', idColName='Id', leadText='RDCmpd') | source code

_test() | source code

Variables | [hide private]

| _roundtripTests = ``...``
| __test__ = `{"roundtrip": _roundtripTests}`

Imports: RDConfig, DbModule, DbConnect

Function Details | [hide private]

### ValidateRDId(id)

source code

    returns whether or not an RDId is valid

    >>> ValidateRDId('RDCmpd-000-009-9')
    1
    >>> ValidateRDId('RDCmpd-009-000-009-8')
    1
    >>> ValidateRDId('RDCmpd-009-000-109-8')
    0
    >>> ValidateRDId('bogus')
    0

### RDIdToInt(id, validate=1)

source code

    Returns the integer index for a given RDId
    Throws a ValueError on error

    >>> RDIdToInt('RDCmpd-000-009-9')
    9
    >>> RDIdToInt('RDCmpd-009-000-009-8')
    9000009
    >>> RDIdToInt('RDData_000_009_9')
    9
    >>> try:
    ...   RDIdToInt('RDCmpd-009-000-109-8')
    ... except ValueError:
    ...   print 'ok'
    ... else:
    ...   print 'failed'
    ok
    >>> try:
    ...   RDIdToInt('bogus')
    ... except ValueError:
    ...   print 'ok'
    ... else:
    ...   print 'failed'
    ok

### IndexToRDId(idx, leadText='RDCmpd')

source code

    Converts an integer index into an RDId

    The format of the ID is:
      leadText-xxx-xxx-xxx-y
    The number blocks are zero padded and the the final digit (y)
    is a checksum:
    >>> str(IndexToRDId(9))
    'RDCmpd-000-009-9'
    >>> str(IndexToRDId(9009))
    'RDCmpd-009-009-8'

    A millions block is included if it's nonzero:
    >>> str(IndexToRDId(9000009))
    'RDCmpd-009-000-009-8'

    The text at the beginning can be altered:
    >>> str(IndexToRDId(9,leadText='RDAlt'))
    'RDAlt-000-009-9'

    Negative indices are errors:
    >>> try:
    ...   IndexToRDId(-1)
    ... except ValueError:
    ...   print 'ok'
    ... else:
    ...   print 'failed'
    ok

### GetNextId(conn, table, idColName='Id')

source code

    returns the next available Id in the database

    see RegisterItem for testing/documentation

### GetNextRDId(conn, table, idColName='Id', leadText='')

source code

    returns the next available RDId in the database

    see RegisterItem for testing/documentation

### RegisterItem(conn, table, value, columnName, data=None, id='', idColName='Id', leadText='RDCmpd')

source code

    >>> dbName =  RDConfig.RDTestDatabase
    >>> conn = DbConnect(dbName)
    >>> tblName = 'StorageTest'
    >>> conn.AddTable(tblName,'id varchar(32) not null primary key,label varchar(40),val int')
    >>> RegisterItem(conn,tblName,'label1','label',['label1',1])==(1, 'RDCmpd-000-001-1')
    True
    >>> RegisterItem(conn,tblName,'label2','label',['label2',1])==(1, 'RDCmpd-000-002-2')
    True
    >>> RegisterItem(conn,tblName,'label1','label',['label1',1])==(0, 'RDCmpd-000-001-1')
    True
    >>> str(GetNextRDId(conn,tblName))
    'RDCmpd-000-003-3'
    >>> tuple(conn.GetData(table=tblName)[0])==('RDCmpd-000-001-1', 'label1', 1)
    True

    It's also possible to provide ids by hand:
    >>> RegisterItem(conn,tblName,'label10','label',['label10',1],id='RDCmpd-000-010-1')==(1, 'RDCmpd-000-010-1')
    True
    >>> str(GetNextRDId(conn,tblName))
    'RDCmpd-000-011-2'

Variables Details | [hide private]

### _roundtripTests

Value:

      """
      >>> ValidateRDId(IndexToRDId(100))
      1
      >>> ValidateRDId(IndexToRDId(10000,leadText='foo'))
      1
      >>> indices = [1,100,1000,1000000]
      >>> vals = []
      >>> for idx in indices:
      ...