source: vendor/python/2.5/Doc/lib/libanydbm.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.1 KB
Line 
1\section{\module{anydbm} ---
2 Generic access to DBM-style databases}
3
4\declaremodule{standard}{anydbm}
5\modulesynopsis{Generic interface to DBM-style database modules.}
6
7
8\module{anydbm} is a generic interface to variants of the DBM
9database --- \refmodule{dbhash}\refstmodindex{dbhash} (requires
10\refmodule{bsddb}\refbimodindex{bsddb}),
11\refmodule{gdbm}\refbimodindex{gdbm}, or
12\refmodule{dbm}\refbimodindex{dbm}. If none of these modules is
13installed, the slow-but-simple implementation in module
14\refmodule{dumbdbm}\refstmodindex{dumbdbm} will be used.
15
16\begin{funcdesc}{open}{filename\optional{, flag\optional{, mode}}}
17Open the database file \var{filename} and return a corresponding object.
18
19If the database file already exists, the \refmodule{whichdb} module is
20used to determine its type and the appropriate module is used; if it
21does not exist, the first module listed above that can be imported is
22used.
23
24The optional \var{flag} argument can be
25\code{'r'} to open an existing database for reading only,
26\code{'w'} to open an existing database for reading and writing,
27\code{'c'} to create the database if it doesn't exist, or
28\code{'n'}, which will always create a new empty database. If not
29specified, the default value is \code{'r'}.
30
31The optional \var{mode} argument is the \UNIX{} mode of the file, used
32only when the database has to be created. It defaults to octal
33\code{0666} (and will be modified by the prevailing umask).
34\end{funcdesc}
35
36\begin{excdesc}{error}
37A tuple containing the exceptions that can be raised by each of the
38supported modules, with a unique exception \exception{anydbm.error} as
39the first item --- the latter is used when \exception{anydbm.error} is
40raised.
41\end{excdesc}
42
43The object returned by \function{open()} supports most of the same
44functionality as dictionaries; keys and their corresponding values can
45be stored, retrieved, and deleted, and the \method{has_key()} and
46\method{keys()} methods are available. Keys and values must always be
47strings.
48
49The following example records some hostnames and a corresponding title,
50and then prints out the contents of the database:
51
52\begin{verbatim}
53import anydbm
54
55# Open database, creating it if necessary.
56db = anydbm.open('cache', 'c')
57
58# Record some values
59db['www.python.org'] = 'Python Website'
60db['www.cnn.com'] = 'Cable News Network'
61
62# Loop through contents. Other dictionary methods
63# such as .keys(), .values() also work.
64for k, v in db.iteritems():
65 print k, '\t', v
66
67# Storing a non-string key or value will raise an exception (most
68# likely a TypeError).
69db['www.yahoo.com'] = 4
70
71# Close when done.
72db.close()
73\end{verbatim}
74
75
76\begin{seealso}
77 \seemodule{dbhash}{BSD \code{db} database interface.}
78 \seemodule{dbm}{Standard \UNIX{} database interface.}
79 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
80 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
81 \seemodule{shelve}{General object persistence built on top of
82 the Python \code{dbm} interface.}
83 \seemodule{whichdb}{Utility module used to determine the type of an
84 existing database.}
85\end{seealso}
Note: See TracBrowser for help on using the repository browser.