Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Lib / anydbm.py
blob2dc312d5fd3a25dc4ebd2862bbcc2ca0a078f6ab
1 """Generic interface to all dbm clones.
3 Instead of
5 import dbm
6 d = dbm.open(file, 'w', 0666)
8 use
10 import anydbm
11 d = anydbm.open(file)
13 The returned object is a dbhash, gdbm, dbm or dumbdbm object,
14 dependent on availability of the modules (tested in this order).
16 It has the following interface (key and data are strings):
18 d[key] = data # store data at key (may override data at
19 # existing key)
20 data = d[key] # retrieve data at key (raise KeyError if no
21 # such key)
22 del d[key] # delete data stored at key (raises KeyError
23 # if no such key)
24 flag = d.has_key(key) # true if the key exists
25 list = d.keys() # return a list of all existing keys (slow!)
27 Future versions may change the order in which implementations are
28 tested for existence, add interfaces to other dbm-like
29 implementations, and (in the presence of multiple implementations)
30 decide which module to use based upon the extension or contents of an
31 existing database file.
33 The open function has an optional second argument. This can be set to
34 'r' to open the database for reading only. The default is 'r', like
35 the dbm default.
37 """
39 _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
41 for _name in _names:
42 try:
43 exec "import %s; _mod = %s" % (_name, _name)
44 except ImportError:
45 continue
46 else:
47 break
48 else:
49 raise ImportError, "no dbm clone found; tried %s" % _names
51 error = _mod.error
53 def open(file, flag = 'r', mode = 0666):
54 return _mod.open(file, flag, mode)