Accessing Persistent Dictionaries

With the DBM modules, you can treat the object you get back from the open function as a dictionary object. Get and set values using code like the following:

Remember that the key and the value must both be text strings.

You can delete a value in the dictionary using del:

You can determine whether a particular key is stored in the dictionary using if:

print 'Key exists in dictionary'

If you use the dbhash module, you can use the following syntax as an alternate means to determine whether a particular key is stored in the dictionary:

print 'Key exists in dictionary'

This syntax works with the dbhash type of DBM module. It does not work with all other DBM modules. The keys method returns a list of all the keys, in the same way it would with a normal dictionary:

The keys method may take a long time to execute if there are a huge number of keys in the file. In addition, this method may require a lot of memory to store the potentially large list that it would create with a large file.

You can use the following script as a guide for how to program with DBM persistent dictionaries.

Try It Out Accessing Persistent Dictionaries

Enter the following script and name the file

import anydbm import whichdb

# Check the type.

print "Type of DBM file =", whichdb.whichdb('websites')

# Open existing file.

# Add another item.

# Verify the previous item remains. if db[''] != None:

print 'Found' else:

print 'Error: Missing item'

# Iterate over the keys. May be slow.

print "Key =",key," value =

= ",db[key]

print "After deleting www.wrox.

.com, we have:"

print "Key =",key," value =

= ",db[key]

# Close and save to disk. db.close()

When you run this script, you'll see output similar to the following:

$ python

Type of DBM file = dbhash


Key = value =

Wrox home page

Key = value

= Python home page

After deleting,

we have:

Key = value

= Python home page

How It Works

This script works with a small database of web site URLs and descriptions. You need to first run the example, shown previously. That example creates the DBM file and stores data in the file. The script then opens the pre-existing DBM file. The script starts out using the whichdb.whichdb function to determine the type of DBM file created by the previous example,, for the DBM persistent dictionary websites. In the example here, it's correctly determined that the type is dbhash.

The script then opens the persistent dictionary websites in read/write mode. The call to the open function will generate an error if the necessary data file or files do not exist on disk in the current directory.

From the previous example,, there should be one value in the dictionary, under the key This example adds the Wrox web site,, as another key.

The script verifies that the key exists in the dictionary, using the following code:

if db[''] != None: print 'Found' else:

print 'Error: Missing item'

Next, the script prints out all of the keys and values in the dictionary:

print "Key =",key," value =",db[key]

Note that there should be only these two entries. 254 TERM LUG

After printing out all of the entries, the script removes one using del: del db['']

The script then prints all of the keys and values again, which should result in just one entry, as shown in the output.

Finally, the close method closes the dictionary, which involves saving all the changes to disk, so the next time the file is opened it will be in the state you left it.

As you can see, the API for working with persistent dictionaries is incredibly simple because it works like files and like dictionaries, which you're already familiar with.

Was this article helpful?

0 0

Post a comment