annotate Lib/anydbm.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 5c3f3d352e0a
children
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
69770
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
32 tested for existence, and 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 """
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
35
22366
121e198f6dda dump the string exception fallback for anydbm.error.
Skip Montanaro <skip@pobox.com>
parents: 17793
diff changeset
36 class error(Exception):
121e198f6dda dump the string exception fallback for anydbm.error.
Skip Montanaro <skip@pobox.com>
parents: 17793
diff changeset
37 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
38
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
39 _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
40 _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
41 _defaultmod = None
2949
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
42
bc0803084e60 revamped somewhat
Guido van Rossum <guido@python.org>
parents: 2303
diff changeset
43 for _name in _names:
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
44 try:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
45 _mod = __import__(_name)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
46 except ImportError:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
47 continue
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
48 if not _defaultmod:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
49 _defaultmod = _mod
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
50 _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
51
c9d0d631abaa Inspired by Ben Sayer, rewritten the code and some of the comments to
Guido van Rossum <guido@python.org>
parents: 3356
diff changeset
52 if not _defaultmod:
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
53 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
54
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
55 error = tuple(_errors)
3356
6d762a2f9b69 inherit error from _mod.error
Guido van Rossum <guido@python.org>
parents: 2966
diff changeset
56
69770
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
57 def open(file, flag='r', mode=0666):
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
58 """Open or create database at path given by *file*.
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
59
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
60 Optional argument *flag* can be 'r' (default) for read-only access, 'w'
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
61 for read-write access of an existing database, 'c' for read-write access
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
62 to a new or existing database, and 'n' for read-write access to a new
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
63 database.
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
64
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
65 Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
66 only if it doesn't exist; and 'n' always creates a new database.
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
67 """
5c3f3d352e0a Add docstring to anydbm.open
Éric Araujo <merwok@netwok.org>
parents: 23620
diff changeset
68
15857
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
69 # guess the type of an existing database
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
70 from whichdb import whichdb
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
71 result=whichdb(file)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
72 if result is None:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
73 # db doesn't exist
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
74 if 'c' in flag or 'n' in flag:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
75 # file doesn't exist and the new
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
76 # flag was used so use default type
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
77 mod = _defaultmod
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
78 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
79 raise error, "need 'c' or 'n' flag to open new db"
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
80 elif result == "":
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
81 # db type cannot be determined
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
82 raise error, "db type could not be determined"
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
83 else:
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
84 mod = __import__(result)
0a8da6f397b7 Whitespace standardization.
Tim Peters <tim.peters@gmail.com>
parents: 8170
diff changeset
85 return mod.open(file, flag, mode)