cpython-withatomic / Lib / anydbm.py

The branch 'legacy-trunk' does not exist.
"""Generic interface to all dbm clones.

Instead of

	import dbm
	d = dbm.open(file, 'w', 0666)


	import anydbm
	d = anydbm.open(file)

The returned object is a dbhash, gdbm, dbm or dumbdbm object,
dependent on availability of the modules (tested in this order).

It has the following interface (key and data are strings):

	d[key] = data	# store data at key (may override data at
			# existing key)
	data = d[key]	# retrieve data at key (raise KeyError if no
			# such key)
	del d[key]	# delete data stored at key (raises KeyError
			# if no such key)
	flag = d.has_key(key)	# true if the key exists
	list = d.keys()	# return a list of all existing keys (slow!)

Future versions may change the order in which implementations are
tested for existence, add interfaces to other dbm-like
implementations, and (in the presence of multiple implementations)
decide which module to use based upon the extension or contents of an
existing database file.

The open function has an optional second argument.  This can be set to
'r' to open the database for reading only.  The default is 'r', like
the dbm default.


_names = ['dbhash', 'gdbm', 'dbm', 'dumbdbm']

for _name in _names:
		exec "import %s; _mod = %s" % (_name, _name)
	except ImportError:
	raise ImportError, "no dbm clone found; tried %s" % _names

error = _mod.error

def open(file, flag = 'r', mode = 0666):
	return _mod.open(file, flag, mode)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.