ยปCore Development>Code coverage>Lib/anydbm.py

Python code coverage for Lib/anydbm.py

#countcontent
1n/a"""Generic interface to all dbm clones.
2n/a
3n/aInstead of
4n/a
5n/a import dbm
6n/a d = dbm.open(file, 'w', 0666)
7n/a
8n/ause
9n/a
10n/a import anydbm
11n/a d = anydbm.open(file, 'w')
12n/a
13n/aThe returned object is a dbhash, gdbm, dbm or dumbdbm object,
14n/adependent on the type of database being opened (determined by whichdb
15n/amodule) in the case of an existing dbm. If the dbm does not exist and
16n/athe create or new flag ('c' or 'n') was specified, the dbm type will
17n/abe determined by the availability of the modules (tested in the above
18n/aorder).
19n/a
20n/aIt has the following interface (key and data are strings):
21n/a
22n/a d[key] = data # store data at key (may override data at
23n/a # existing key)
24n/a data = d[key] # retrieve data at key (raise KeyError if no
25n/a # such key)
26n/a del d[key] # delete data stored at key (raises KeyError
27n/a # if no such key)
28n/a flag = key in d # true if the key exists
29n/a list = d.keys() # return a list of all existing keys (slow!)
30n/a
31n/aFuture versions may change the order in which implementations are
32n/atested for existence, add interfaces to other dbm-like
33n/aimplementations.
34n/a
35n/aThe open function has an optional second argument. This can be 'r',
36n/afor read-only access, 'w', for read-write access of an existing
37n/adatabase, 'c' for read-write access to a new or existing database, and
38n/a'n' for read-write access to a new database. The default is 'r'.
39n/a
40n/aNote: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
41n/aonly if it doesn't exist; and 'n' always creates a new database.
42n/a
431"""
44n/a
452class error(Exception):
461 pass
47n/a
481_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
491_errors = [error]
501_defaultmod = None
51n/a
525for _name in _names:
534 try:
544 _mod = __import__(_name)
550 except ImportError:
560 continue
574 if not _defaultmod:
581 _defaultmod = _mod
594 _errors.append(_mod.error)
60n/a
611if not _defaultmod:
620 raise ImportError, "no dbm clone found; tried %s" % _names
63n/a
641error = tuple(_errors)
65n/a
661def open(file, flag = 'r', mode = 0666):
67n/a # guess the type of an existing database
6888 from whichdb import whichdb
6988 result=whichdb(file)
7088 if result is None:
71n/a # db doesn't exist
7285 if 'c' in flag or 'n' in flag:
73n/a # file doesn't exist and the new
74n/a # flag was used so use default type
7585 mod = _defaultmod
76n/a else:
770 raise error, "need 'c' or 'n' flag to open new db"
783 elif result == "":
79n/a # db type cannot be determined
800 raise error, "db type could not be determined"
81n/a else:
823 mod = __import__(result)
8388 return mod.open(file, flag, mode)