PyXR

c:\python24\lib \ anydbm.py



0001 """Generic interface to all dbm clones.
0002 
0003 Instead of
0004 
0005         import dbm
0006         d = dbm.open(file, 'w', 0666)
0007 
0008 use
0009 
0010         import anydbm
0011         d = anydbm.open(file, 'w')
0012 
0013 The returned object is a dbhash, gdbm, dbm or dumbdbm object,
0014 dependent on the type of database being opened (determined by whichdb
0015 module) in the case of an existing dbm. If the dbm does not exist and
0016 the create or new flag ('c' or 'n') was specified, the dbm type will
0017 be determined by the availability of the modules (tested in the above
0018 order).
0019 
0020 It has the following interface (key and data are strings):
0021 
0022         d[key] = data   # store data at key (may override data at
0023                         # existing key)
0024         data = d[key]   # retrieve data at key (raise KeyError if no
0025                         # such key)
0026         del d[key]      # delete data stored at key (raises KeyError
0027                         # if no such key)
0028         flag = key in d   # true if the key exists
0029         list = d.keys() # return a list of all existing keys (slow!)
0030 
0031 Future versions may change the order in which implementations are
0032 tested for existence, add interfaces to other dbm-like
0033 implementations.
0034 
0035 The open function has an optional second argument.  This can be 'r',
0036 for read-only access, 'w', for read-write access of an existing
0037 database, 'c' for read-write access to a new or existing database, and
0038 'n' for read-write access to a new database.  The default is 'r'.
0039 
0040 Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
0041 only if it doesn't exist; and 'n' always creates a new database.
0042 
0043 """
0044 
0045 class error(Exception):
0046     pass
0047 
0048 _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
0049 _errors = [error]
0050 _defaultmod = None
0051 
0052 for _name in _names:
0053     try:
0054         _mod = __import__(_name)
0055     except ImportError:
0056         continue
0057     if not _defaultmod:
0058         _defaultmod = _mod
0059     _errors.append(_mod.error)
0060 
0061 if not _defaultmod:
0062     raise ImportError, "no dbm clone found; tried %s" % _names
0063 
0064 error = tuple(_errors)
0065 
0066 def open(file, flag = 'r', mode = 0666):
0067     # guess the type of an existing database
0068     from whichdb import whichdb
0069     result=whichdb(file)
0070     if result is None:
0071         # db doesn't exist
0072         if 'c' in flag or 'n' in flag:
0073             # file doesn't exist and the new
0074             # flag was used so use default type
0075             mod = _defaultmod
0076         else:
0077             raise error, "need 'c' or 'n' flag to open new db"
0078     elif result == "":
0079         # db type cannot be determined
0080         raise error, "db type could not be determined"
0081     else:
0082         mod = __import__(result)
0083     return mod.open(file, flag, mode)
0084 

Generated by PyXR 0.9.4
SourceForge.net Logo