Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 1 | """Generic interface to all dbm clones. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 2 | |
| 3 | Instead of |
| 4 | |
| 5 | import dbm |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 6 | d = dbm.open(file, 'w', 0666) |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 7 | |
| 8 | use |
| 9 | |
| 10 | import anydbm |
| 11 | d = anydbm.open(file) |
| 12 | |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 13 | The returned object is a dbhash, gdbm, dbm or dumbdbm object, |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 28 | tested for existence, add interfaces to other dbm-like |
| 29 | implementations, and (in the presence of multiple implementations) |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e03a86c | 1995-08-11 14:18:27 +0000 | [diff] [blame] | 34 | 'r' to open the database for reading only. The default is 'r', like |
| 35 | the dbm default. |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 37 | """ |
Guido van Rossum | a48061a | 1995-01-10 00:31:14 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 39 | _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] |
| 40 | |
| 41 | for _name in _names: |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 42 | try: |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 43 | exec "import %s; _mod = %s" % (_name, _name) |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 44 | except ImportError: |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 45 | continue |
| 46 | else: |
| 47 | break |
| 48 | else: |
| 49 | raise ImportError, "no dbm clone found; tried %s" % _names |
Guido van Rossum | 0182c06 | 1996-01-25 18:26:57 +0000 | [diff] [blame] | 50 | |
| 51 | error = _mod.error |
| 52 | |
Guido van Rossum | e03a86c | 1995-08-11 14:18:27 +0000 | [diff] [blame] | 53 | def open(file, flag = 'r', mode = 0666): |
Guido van Rossum | 4ac0050 | 1995-08-10 19:24:30 +0000 | [diff] [blame] | 54 | return _mod.open(file, flag, mode) |