Oops -- Lib/Test should be Lib/test, of course!
[python/dscho.git] / Doc / lib / libgdbm.tex
blob8f2d7d1bf02e24494871f85052264ec2b45c2eff
1 \section{Built-in Module \module{gdbm}}
2 \label{module-gdbm}
3 \bimodindex{gdbm}
5 % Note that if this section appears on the same page as the first
6 % paragraph of the dbm module section, makeindex will produce the
7 % warning:
9 % ## Warning (input = lib.idx, line = 1184; output = lib.ind, line = 852):
10 % -- Conflicting entries: multiple encaps for the same page under same key.
12 % This is because the \bimodindex{gdbm} and \refbimodindex{gdbm}
13 % entries in the .idx file are slightly different (the \bimodindex{}
14 % version includes "|textbf" at the end to make the defining occurance
15 % bold). There doesn't appear to be anything that can be done about
16 % this; it's just a little annoying. The warning can be ignored, but
17 % the index produced uses the non-bold version.
19 This module is quite similar to the \code{dbm} module, but uses \code{gdbm}
20 instead to provide some additional functionality. Please note that
21 the file formats created by \code{gdbm} and \code{dbm} are incompatible.
22 \refbimodindex{dbm}
24 The \code{gdbm} module provides an interface to the GNU DBM
25 library. \code{gdbm} objects behave like mappings
26 (dictionaries), except that keys and values are always strings.
27 Printing a \code{gdbm} object doesn't print the keys and values, and the
28 \code{items()} and \code{values()} methods are not supported.
30 The module defines the following constant and functions:
32 \begin{excdesc}{error}
33 Raised on \code{gdbm}-specific errors, such as I/O errors. \code{KeyError} is
34 raised for general mapping errors like specifying an incorrect key.
35 \end{excdesc}
37 \begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}}
38 Open a \code{gdbm} database and return a \code{gdbm} object. The
39 \var{filename} argument is the name of the database file.
41 The optional \var{flag} argument can be
42 \code{'r'} (to open an existing database for reading only --- default),
43 \code{'w'} (to open an existing database for reading and writing),
44 \code{'c'} (which creates the database if it doesn't exist), or
45 \code{'n'} (which always creates a new empty database).
47 Appending \code{f} to the flag opens the database in fast mode;
48 altered data will not automatically be written to the disk after every
49 change. This results in faster writes to the database, but may result
50 in an inconsistent database if the program crashes while the database
51 is still open. Use the \code{sync()} method to force any unwritten
52 data to be written to the disk.
54 The optional \var{mode} argument is the \UNIX{} mode of the file, used
55 only when the database has to be created. It defaults to octal
56 \code{0666}.
57 \end{funcdesc}
59 In addition to the dictionary-like methods, \code{gdbm} objects have the
60 following methods:
62 \begin{funcdesc}{firstkey}{}
63 It's possible to loop over every key in the database using this method
64 and the \code{nextkey()} method. The traversal is ordered by \code{gdbm}'s
65 internal hash values, and won't be sorted by the key values. This
66 method returns the starting key.
67 \end{funcdesc}
69 \begin{funcdesc}{nextkey}{key}
70 Returns the key that follows \var{key} in the traversal. The
71 following code prints every key in the database \code{db}, without having to
72 create a list in memory that contains them all:
73 \begin{verbatim}
74 k=db.firstkey()
75 while k!=None:
76 print k
77 k=db.nextkey(k)
78 \end{verbatim}
79 \end{funcdesc}
81 \begin{funcdesc}{reorganize}{}
82 If you have carried out a lot of deletions and would like to shrink
83 the space used by the \code{gdbm} file, this routine will reorganize the
84 database. \code{gdbm} will not shorten the length of a database file except
85 by using this reorganization; otherwise, deleted file space will be
86 kept and reused as new (key,value) pairs are added.
87 \end{funcdesc}
89 \begin{funcdesc}{sync}{}
90 When the database has been opened in fast mode, this method forces any
91 unwritten data to be written to the disk.
92 \end{funcdesc}