Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 1 | """A generic interface to all dbm clones. |
| 2 | |
| 3 | Instead of |
| 4 | |
| 5 | import dbm |
| 6 | d = dbm.open(file, 'rw', 0666) |
| 7 | |
| 8 | use |
| 9 | |
| 10 | import anydbm |
| 11 | d = anydbm.open(file) |
| 12 | |
| 13 | The returned object is a dbm, gdbm or (on the Mac) dbmac object, |
| 14 | dependent on availability of the modules (tested in this order). |
| 15 | |
| 16 | It has the following interface (key and data are strings): |
| 17 | |
| 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!) |
| 26 | |
| 27 | Future versions may change the order in which implementations are |
| 28 | tested for existence, add interfaces to other db-like implementations |
| 29 | (e.g. BSD Hash), 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. |
| 32 | |
| 33 | The open function has an optional second argument. This can be set to |
| 34 | 'r' to open the database for reading only. Don't pas an explicit 'w' |
| 35 | or 'rw' to open it for writing, as the different interfaces have |
| 36 | different interpretation of their mode argument if it isn't 'r'. |
| 37 | """ |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 38 | |
| 39 | try: |
| 40 | import dbm |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 41 | def open(filename, mode = 'rw'): |
| 42 | return dbm.open(filename, mode, 0666) |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 43 | except ImportError: |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 44 | try: |
| 45 | import gdbm |
| 46 | def open(filename, mode = 'w'): |
| 47 | return gdbm.open(filename, mode, 0666) |
| 48 | except ImportError: |
| 49 | import dbmac |
| 50 | open = dbmac.open |