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 dbmaccess.py:

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['www.python.org'] != None:

print 'Found www.python.org' 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 dbmaccess.py

Type of DBM file = dbhash

Found www.python.org

Key = www.wrox.com value =

Wrox home page

Key = www.python.org value

= Python home page

After deleting www.wrox.com,

we have:

Key = www.python.org 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 dbmcreate.py example, shown previously. That example creates the DBM file and stores data in the file. The dbmaccess.py script then opens the pre-existing DBM file. The dbmaccess.py script starts out using the whichdb.whichdb function to determine the type of DBM file created by the previous example, dbmcreate.py, for the DBM persistent dictionary websites. In the example here, it's correctly determined that the type is dbhash.

The dbmaccess.py 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, dbmcreate.py, there should be one value in the dictionary, under the key www.python.org. This example adds the Wrox web site, www.wrox.com, as another key.

The script verifies that the www.python.org key exists in the dictionary, using the following code:

if db['www.python.org'] != None: print 'Found www.python.org' 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['www.wrox.com']

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