DBM Files
Flat files are handy for simple persistence tasks, but they are generally geared toward
a sequential processing mode. Although it is possible to jump around to arbitrary lo-
cations with seek calls, flat files don’t provide much structure to data beyond the notion
of bytes and text lines.
DBM files, a standard tool in the Python library for database management, improve on
that by providing key-based access to stored text strings. They implement a random-
access, single-key view on stored data. For instance, information related to objects can
be stored in a DBM file using a unique key per object and later can be fetched back
directly with the same key. DBM files are implemented by a variety of underlying mod-
ules (including one coded in Python), but if you have Python, you have a DBM.
Using DBM Files
Although DBM filesystems have to do a bit of work to map chunks of stored data to
keys for fast retrieval (technically, they generally use a technique called hashing to store
data in files), your scripts don’t need to care about the action going on behind the
scenes. In fact, DBM is one of the easiest ways to save information in Python—DBM
files behave so much like in-memory dictionaries that you may forget you’re actually
dealing with a file at all. For instance, given a DBM file object:
- Indexing by key fetches data from the file.
- Assigning to an index stores data in the file.
DBM file objects also support common dictionary methods such as keys-list fetches
and tests and key deletions. The DBM library itself is hidden behind this simple model.
Since it is so simple, let’s jump right into an interactive example that creates a DBM
file and shows how the interface works:
C:\...\PP4E\Dbase> python
>>> import dbm # get interface: bsddb, gnu, ndbm, dumb
>>> file = dbm.open('movie', 'c') # make a DBM file called 'movie'
>>> file['Batman'] = 'Pow!' # store a string under key 'Batman'
>>> file.keys() # get the file's key directory
[b'Batman']
>>> file['Batman'] # fetch value for key 'Batman'
b'Pow!'
>>> who = ['Robin', 'Cat-woman', 'Joker']
>>> what = ['Bang!', 'Splat!', 'Wham!']
>>> for i in range(len(who)):
... file[who[i]] = what[i] # add 3 more "records"
...
>>> file.keys()
[b'Cat-woman', b'Batman', b'Joker', b'Robin']
>>> len(file), 'Robin' in file, file['Joker']
DBM Files| 1305