Mercurial > cpython-lru_cache-weakref
annotate Lib/anydbm.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 6d762a2f9b69 |
| children | c9d0d631abaa |
| rev | line source |
|---|---|
| 2949 | 1 """Generic interface to all dbm clones. |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
2 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
3 Instead of |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
4 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
5 import dbm |
| 2949 | 6 d = dbm.open(file, 'w', 0666) |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
7 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
8 use |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
9 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
10 import anydbm |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
11 d = anydbm.open(file) |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
12 |
| 2949 | 13 The returned object is a dbhash, gdbm, dbm or dumbdbm object, |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
14 dependent on availability of the modules (tested in this order). |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
15 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
16 It has the following interface (key and data are strings): |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
17 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
18 d[key] = data # store data at key (may override data at |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
19 # existing key) |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
20 data = d[key] # retrieve data at key (raise KeyError if no |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
21 # such key) |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
22 del d[key] # delete data stored at key (raises KeyError |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
23 # if no such key) |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
24 flag = d.has_key(key) # true if the key exists |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
25 list = d.keys() # return a list of all existing keys (slow!) |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
26 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
27 Future versions may change the order in which implementations are |
| 2949 | 28 tested for existence, add interfaces to other dbm-like |
| 29 implementations, and (in the presence of multiple implementations) | |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
30 decide which module to use based upon the extension or contents of an |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
31 existing database file. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
32 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
33 The open function has an optional second argument. This can be set to |
|
2966
f9fbfd903153
change default flag to match dbm/gdbm
Guido van Rossum <guido@python.org>
parents:
2949
diff
changeset
|
34 'r' to open the database for reading only. The default is 'r', like |
|
f9fbfd903153
change default flag to match dbm/gdbm
Guido van Rossum <guido@python.org>
parents:
2949
diff
changeset
|
35 the dbm default. |
| 2949 | 36 |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
37 """ |
|
2100
4574ed657cc9
shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 |
| 2949 | 39 _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm'] |
| 40 | |
| 41 for _name in _names: | |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
42 try: |
| 2949 | 43 exec "import %s; _mod = %s" % (_name, _name) |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2100
diff
changeset
|
44 except ImportError: |
| 2949 | 45 continue |
| 46 else: | |
| 47 break | |
| 48 else: | |
| 49 raise ImportError, "no dbm clone found; tried %s" % _names | |
|
3356
6d762a2f9b69
inherit error from _mod.error
Guido van Rossum <guido@python.org>
parents:
2966
diff
changeset
|
50 |
|
6d762a2f9b69
inherit error from _mod.error
Guido van Rossum <guido@python.org>
parents:
2966
diff
changeset
|
51 error = _mod.error |
|
6d762a2f9b69
inherit error from _mod.error
Guido van Rossum <guido@python.org>
parents:
2966
diff
changeset
|
52 |
|
2966
f9fbfd903153
change default flag to match dbm/gdbm
Guido van Rossum <guido@python.org>
parents:
2949
diff
changeset
|
53 def open(file, flag = 'r', mode = 0666): |
| 2949 | 54 return _mod.open(file, flag, mode) |
