DBM example


# hash.py
# test hash tables (dictionaries) in Python

hashtable = {'this is a key': 'and this is the value',
             'another key':   'another value'}

print 'line 1:', hashtable['this is a key']

hashtable['third key'] = 'bla'
print 'line 2:', hashtable['third key']

Output:

line 1: and this is the value
line 2: bla

# make_dbmfile.py
# test dbm-type database; gdbm variant

import gdbm

hashfile = gdbm.open('hashfile', 'c')   # create the file
hashfile['a key'] = 'a bit of data'
hashfile['protein sequence'] = 'ASWQQEDFFGLKPVCDAS'

# read_dbmfile.py
# test dbm-type database; gdbm variant

import gdbm

hashfile = gdbm.open('hashfile', 'r')   # open existing file
for key in hashfile.keys():             # loop over all keys in file
    print key, '=', hashfile[key]

Output:

protein sequence = ASWQQEDFFGLKPVCDAS
a key = a bit of data

© 2001 Per Kraulis $Date: 2001/05/09 11:07:11 $