Kaydet (Commit) 4ac00503 authored tarafından Guido van Rossum's avatar Guido van Rossum

revamped somewhat

üst 3e7a697c
"""A generic interface to all dbm clones. """Generic interface to all dbm clones.
Instead of Instead of
import dbm import dbm
d = dbm.open(file, 'rw', 0666) d = dbm.open(file, 'w', 0666)
use use
import anydbm import anydbm
d = anydbm.open(file) d = anydbm.open(file)
The returned object is a dbm, gdbm or (on the Mac) dbmac object, The returned object is a dbhash, gdbm, dbm or dumbdbm object,
dependent on availability of the modules (tested in this order). dependent on availability of the modules (tested in this order).
It has the following interface (key and data are strings): It has the following interface (key and data are strings):
...@@ -25,26 +25,27 @@ It has the following interface (key and data are strings): ...@@ -25,26 +25,27 @@ It has the following interface (key and data are strings):
list = d.keys() # return a list of all existing keys (slow!) list = d.keys() # return a list of all existing keys (slow!)
Future versions may change the order in which implementations are Future versions may change the order in which implementations are
tested for existence, add interfaces to other db-like implementations tested for existence, add interfaces to other dbm-like
(e.g. BSD Hash), and (in the presence of multiple implementations) implementations, and (in the presence of multiple implementations)
decide which module to use based upon the extension or contents of an decide which module to use based upon the extension or contents of an
existing database file. existing database file.
The open function has an optional second argument. This can be set to The open function has an optional second argument. This can be set to
'r' to open the database for reading only. Don't pas an explicit 'w' 'r' to open the database for reading only. The default is 'w', which
or 'rw' to open it for writing, as the different interfaces have differs from the dbm default ('r') for historic reasons.
different interpretation of their mode argument if it isn't 'r'.
""" """
try: _names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']
import dbm
def open(filename, mode = 'rw'): for _name in _names:
return dbm.open(filename, mode, 0666)
except ImportError:
try: try:
import gdbm exec "import %s; _mod = %s" % (_name, _name)
def open(filename, mode = 'w'):
return gdbm.open(filename, mode, 0666)
except ImportError: except ImportError:
import dbmac continue
open = dbmac.open else:
break
else:
raise ImportError, "no dbm clone found; tried %s" % _names
def open(file, flag = 'w', mode = 0666):
return _mod.open(file, flag, mode)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment