blob: 4cf4cb2405e05151d12be7c7941d1afd7535dbbb [file] [log] [blame]
Guido van Rossumb69e0951994-08-08 08:03:24 +00001\section{Built-in Module \sectcode{gdbm}}
Guido van Rossum3c2a0561997-07-17 16:29:42 +00002\label{module-gdbm}
Guido van Rossumb69e0951994-08-08 08:03:24 +00003\bimodindex{gdbm}
4
Guido van Rossum3c2a0561997-07-17 16:29:42 +00005This module is quite similar to the \code{dbm} module, but uses {\sc gdbm}
6instead to provide some additional functionality. Please note that
7the file formats created by {\sc gdbm} and dbm are incompatible.
Guido van Rossum0bf84751995-07-07 22:55:57 +00008\bimodindex{dbm}
Guido van Rossum3c2a0561997-07-17 16:29:42 +00009
10The \code{gdbm} module provides an interface to the GNU DBM
11library. {\sc gdbm} objects behave like mappings
12(dictionaries), except that keys and values are always strings.
13Printing a {\sc gdbm} object doesn't print the keys and values, and the
14\code{items()} and \code{values()} methods are not supported.
15
16The module defines the following constant and functions:
17
18\renewcommand{\indexsubitem}{(in module dbm)}
19\begin{excdesc}{error}
20Raised on dbm-specific errors, such as I/O errors. \code{KeyError} is
21raised for general mapping errors like specifying an incorrect key.
22\end{excdesc}
23
24\begin{funcdesc}{open}{filename\, \optional{flag\, \optional{mode}}}
25Open a dbm database and return a dbm object. The \var{filename}
26argument is the name of the database file (without the \file{.dir} or
27\file{.pag} extensions).
28
29The optional \var{flag} argument can be
30\code{'r'} (to open an existing database for reading only --- default),
31\code{'w'} (to open an existing database for reading and writing),
32\code{'c'} (which creates the database if it doesn't exist), or
33\code{'n'} (which always creates a new empty database).
34
35Appending \code{f} to the flag opens the database in fast mode;
36altered data will not automatically be written to the disk after every
37change. This results in faster writes to the database, but may result
38in an inconsistent database if the program crashes while the database
39is still open. Use the \code{sync()} method to force any unwritten
40data to be written to the disk.
41
42The optional \var{mode} argument is the \UNIX{} mode of the file, used
43only when the database has to be created. It defaults to octal
44\code{0666}.
45\end{funcdesc}
46
47In addition to the dictionary-like methods, {\sc gdbm} objects have the
48following methods:
49
50\begin{funcdesc}{firstkey}{}
51It's possible to loop over every key in the database using this method
52and the \code{nextkey()} method. The traversal is ordered by {\sc gdbm}'s
53internal hash values, and won't be sorted by the key values. This
54method returns the starting key.
55\end{funcdesc}
56
57\begin{funcdesc}{nextkey}{key}
58Returns the key that follows \var{key} in the traversal. The
59following code prints every key in the database \code{db}, without having to
60create a list in memory that contains them all:
61\bcode\begin{verbatim}
62k=db.firstkey()
63while k!=None:
64 print k
65 k=db.nextkey(k)
66\end{verbatim}\ecode
67\end{funcdesc}
68
69\begin{funcdesc}{reorganize}{}
70If you have carried out a lot of deletions and would like to shrink
71the space used by the {\sc gdbm} file, this routine will reorganize the
72database. {\sc gdbm} will not shorten the length of a database file except
73by using this reorganization; otherwise, deleted file space will be
74kept and reused as new (key,value) pairs are added.
75\end{funcdesc}
76
77\begin{funcdesc}{sync}{}
78When the database has been opened in fast mode, this method forces any
79unwritten data to be written to the disk.
80\end{funcdesc}
81