The anydbm Module

The anydbm module provides a unified interface to the simple database drivers supported by Python.

The first time it is imported, the anydbm module looks for a suitable database driver, testing for dbhash, gdbm, dbm, or dumbdbm, in that order. If no such module is found, it raises an ImportError exception.

In Example 10-1, the open function is used to open or create a database, using the chosen database handler.

Example 10-1. Using the anydbm Module

File: anydbm-example-1.py import anydbm db = anydbm.open("database", "c") db["1"] = "one" db["2"] = "two" db["3"] = "three" db.close() db = anydbm.open("database", "r") for key in db.keys(): print repr(key), repr(db[key]) '2' 'two' '3' 'three' '1' 'one'

Категории