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

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

Python 2.5

File size: 3.7 KB
Line 
1\section{\module{gdbm} ---
2 GNU's reinterpretation of dbm}
3
4\declaremodule{builtin}{gdbm}
5 \platform{Unix}
6\modulesynopsis{GNU's reinterpretation of dbm.}
7
8
9This module is quite similar to the \refmodule{dbm}\refbimodindex{dbm}
10module, but uses \code{gdbm} instead to provide some additional
11functionality. Please note that the file formats created by
12\code{gdbm} and \code{dbm} are incompatible.
13
14The \module{gdbm} module provides an interface to the GNU DBM
15library. \code{gdbm} objects behave like mappings
16(dictionaries), except that keys and values are always strings.
17Printing a \code{gdbm} object doesn't print the keys and values, and
18the \method{items()} and \method{values()} methods are not supported.
19
20The module defines the following constant and functions:
21
22\begin{excdesc}{error}
23Raised on \code{gdbm}-specific errors, such as I/O errors.
24\exception{KeyError} is raised for general mapping errors like
25specifying an incorrect key.
26\end{excdesc}
27
28\begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}}
29Open a \code{gdbm} database and return a \code{gdbm} object. The
30\var{filename} argument is the name of the database file.
31
32The optional \var{flag} argument can be
33\code{'r'} (to open an existing database for reading only --- default),
34\code{'w'} (to open an existing database for reading and writing),
35\code{'c'} (which creates the database if it doesn't exist), or
36\code{'n'} (which always creates a new empty database).
37
38The following additional characters may be appended to the flag to
39control how the database is opened:
40
41\begin{itemize}
42\item \code{'f'} --- Open the database in fast mode. Writes to the database
43 will not be synchronized.
44\item \code{'s'} --- Synchronized mode. This will cause changes to the database
45 will be immediately written to the file.
46\item \code{'u'} --- Do not lock database.
47\end{itemize}
48
49Not all flags are valid for all versions of \code{gdbm}. The
50module constant \code{open_flags} is a string of supported flag
51characters. The exception \exception{error} is raised if an invalid
52flag is specified.
53
54The optional \var{mode} argument is the \UNIX{} mode of the file, used
55only when the database has to be created. It defaults to octal
56\code{0666}.
57\end{funcdesc}
58
59In addition to the dictionary-like methods, \code{gdbm} objects have the
60following methods:
61
62\begin{funcdesc}{firstkey}{}
63It's possible to loop over every key in the database using this method
64and the \method{nextkey()} method. The traversal is ordered by
65\code{gdbm}'s internal hash values, and won't be sorted by the key
66values. This method returns the starting key.
67\end{funcdesc}
68
69\begin{funcdesc}{nextkey}{key}
70Returns the key that follows \var{key} in the traversal. The
71following code prints every key in the database \code{db}, without
72having to create a list in memory that contains them all:
73
74\begin{verbatim}
75k = db.firstkey()
76while k != None:
77 print k
78 k = db.nextkey(k)
79\end{verbatim}
80\end{funcdesc}
81
82\begin{funcdesc}{reorganize}{}
83If you have carried out a lot of deletions and would like to shrink
84the space used by the \code{gdbm} file, this routine will reorganize
85the database. \code{gdbm} will not shorten the length of a database
86file except by using this reorganization; otherwise, deleted file
87space will be kept and reused as new (key, value) pairs are added.
88\end{funcdesc}
89
90\begin{funcdesc}{sync}{}
91When the database has been opened in fast mode, this method forces any
92unwritten data to be written to the disk.
93\end{funcdesc}
94
95
96\begin{seealso}
97 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
98 \seemodule{whichdb}{Utility module used to determine the type of an
99 existing database.}
100\end{seealso}
Note: See TracBrowser for help on using the repository browser.