annotate Lib/anydbm.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents a1de0bab5475
children 229c28cb9afb 5c3f3d352e0a
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
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
5 import dbm
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
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
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
10 import anydbm
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
11 d = anydbm.open(file, 'w')
2303
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,
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
14 dependent on the type of database being opened (determined by whichdb
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
15 module) in the case of an existing dbm. If the dbm does not exist and
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
16 the create or new flag ('c' or 'n') was specified, the dbm type will
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
17 be determined by the availability of the modules (tested in the above
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
18 order).
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
19
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
20 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
21
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
22 d[key] = data # store data at key (may override data at
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
23 # existing key)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
24 data = d[key] # retrieve data at key (raise KeyError if no
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
25 # such key)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
26 del d[key] # delete data stored at key (raises KeyError
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
27 # if no such key)
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 22366
diff changeset
28 flag = key in d # true if the key exists
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
29 list = d.keys() # return a list of all existing keys (slow!)
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
30
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
31 Future versions may change the order in which implementations are
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
32 tested for existence, add interfaces to other dbm-like
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
33 implementations.
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
34
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
35 The open function has an optional second argument. This can be 'r',
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
36 for read-only access, 'w', for read-write access of an existing
8170
284add74fe37 Oops, I had 'n' and 'c' mixed up in my mind. Get rid of the comment
Guido van Rossum <guido@python.org>
parents: 8169
diff changeset
37 database, 'c' for read-write access to a new or existing database, and
284add74fe37 Oops, I had 'n' and 'c' mixed up in my mind. Get rid of the comment
Guido van Rossum <guido@python.org>
parents: 8169
diff changeset
38 'n' for read-write access to a new database. The default is 'r'.
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
39
8170
284add74fe37 Oops, I had 'n' and 'c' mixed up in my mind. Get rid of the comment
Guido van Rossum <guido@python.org>
parents: 8169
diff changeset
40 Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
284add74fe37 Oops, I had 'n' and 'c' mixed up in my mind. Get rid of the comment
Guido van Rossum <guido@python.org>
parents: 8169
diff changeset
41 only if it doesn't exist; and 'n' always creates a new database.
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
42
2303
505fc478ff08 added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents: 2100
diff changeset
43 """
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
44
22366
121e198f6dda dump the string exception fallback for anydbm.error.
Skip Montanaro <skip@pobox.com>
parents: 17793
diff changeset
45 class error(Exception):
121e198f6dda dump the string exception fallback for anydbm.error.
Skip Montanaro <skip@pobox.com>
parents: 17793
diff changeset
46 pass
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
47
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
48 _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
49 _errors = [error]
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
50 _defaultmod = None
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
51
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
52 for _name in _names:
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
53 try:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
54 _mod = __import__(_name)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
55 except ImportError:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
56 continue
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
57 if not _defaultmod:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
58 _defaultmod = _mod
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
59 _errors.append(_mod.error)
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
60
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
61 if not _defaultmod:
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
62 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
63
8169
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
64 error = tuple(_errors)
3356
6d762a2f9b69 inherit error from _mod.error
Guido van Rossum <guido@python.org>
parents: 2966
diff changeset
65
2966
f9fbfd903153 change default flag to match dbm/gdbm
Guido van Rossum <guido@python.org>
parents: 2949
diff changeset
66 def open(file, flag = 'r', mode = 0666):
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
67 # guess the type of an existing database
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
68 from whichdb import whichdb
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
69 result=whichdb(file)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
70 if result is None:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
71 # db doesn't exist
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
72 if 'c' in flag or 'n' in flag:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
73 # file doesn't exist and the new
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
74 # flag was used so use default type
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
75 mod = _defaultmod
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
76 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
77 raise error, "need 'c' or 'n' flag to open new db"
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
78 elif result == "":
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
79 # db type cannot be determined
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
80 raise error, "db type could not be determined"
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
81 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
82 mod = __import__(result)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
83 return mod.open(file, flag, mode)