annotate Lib/anydbm.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 6d762a2f9b69
children c9d0d631abaa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
28 tested for existence, add interfaces to other dbm-like
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
39 _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
40
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
41 for _name in _names:
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
42 try:
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
45 continue
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
46 else:
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
47 break
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
48 else:
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
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
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
54 return _mod.open(file, flag, mode)