RE: GDBM for Python?

Michael McLay (mclay@eeel.nist.gov)
Thu, 28 Apr 94 19:45:07 EDT

Marc Wachowitz writes:
> Has someone already interfaced GDBM (the GNU-version of DBM - I know that
> the latter is done, but I want to use the former) to Python, and would be
> ready to share that code?

Would you settle for an implementation that uses the BSD version of
ndbm? I posted an announcement about this about a month ago. (The
BSD version has one important feature with GDBM. It does not have a
limit on the size of objects stored in a hash record. A paper in the
distribution compares many hashing methods, including the GDBM code as
well as all the source code from the BSD distribution.) I've been
using this extension for about two months now and find the BSD code to
be very reliable. The announcment describing the extention is in the
README file of the distribution in
ftp.eeel.nist.gov:/pub/python/dbhash.tar.gz.

Only a few copies of my distribution were downloaded so there wasn't a
lot of discussion about it on the mailing list. Guido was on vacation
when I made the announcement, so he may have missed the announcement
entirely. I was hoping it would be added to the contrib directory for
the next release. The BSD version improves performance over Dbm and
it eliminates the ndbm restriction on the size of object it can store.
The BSD code isn't tightly tied to the UNIX file structure like ndbm,
so it might even be possible to build it to work on PCs and Macs.

An example:

The dbhash class in the extension is wrapped in a Dbhash class just
like the ndbm function is wrapped in Dbm. (The distribution includes
a definition of the Dbhash class.) This example groups related hash
tables into an file partdb.py in the class definition Partdb.

###################### File partdb.py ################################
# A database with two table - uinfov and bypartnumberv

class Partdb:
def __init__(self):
self.root = '/usr/local/storeroom/inventory/'
self.uinfov = None
self.bypartnumberv = None

def uinfo(self):
if self.uinfov == None:
self.uinfov = Dbhash(self.root+'userinfo','rw', 0666,1024,32)
return self.uinfov

def bypartnumber(self):
if self.bypartnumberv == None:
self.bypartnumberv = Dbhash(self.root+'electric.bypartnumb','rw', 0666,1024,32)
return self.bypartnumberv

# create an instance of Partdb, but don't open the files unless they
# are accessed.

partdb = Partdb()

############### An application file might include the following ############

# Open the persistent dictionary

import partdb
uinfo = partdb.uinfo()

# Assign a complex list to the persistent dictionary
# The Dbhash class will marshal.dumps the list into a binary string
# and store the string using the hash as an index

uinfo[('pn',0001)] = ["the name",["a second list",(2,3)],'the end']

# Retrieve the persistent value from the archive
# The Dbhash class will marshal.loads the list back into variable x.

x = uinfo[('pn',0001)]

Michael