Mercurial > cpython-lru_cache-weakref
comparison Doc/lib/libgdbm.tex @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | c6fe80ce7c20 |
| children | 4887d754692a |
comparison
equal
deleted
inserted
replaced
| 5816:6a0d7cf19d38 | 5817:48b94de85bf3 |
|---|---|
| 1 \section{Built-in Module \sectcode{gdbm}} | 1 \section{Built-in Module \sectcode{gdbm}} |
| 2 \label{module-gdbm} | |
| 2 \bimodindex{gdbm} | 3 \bimodindex{gdbm} |
| 3 | 4 |
| 4 This module is nearly identical to the \code{dbm} module, but uses | 5 This module is quite similar to the \code{dbm} module, but uses {\sc gdbm} |
| 5 GDBM instead. Its interface is identical, and not repeated here. | 6 instead to provide some additional functionality. Please note that |
| 7 the file formats created by {\sc gdbm} and dbm are incompatible. | |
| 8 \bimodindex{dbm} | |
| 6 | 9 |
| 7 Warning: the file formats created by gdbm and dbm are incompatible. | 10 The \code{gdbm} module provides an interface to the GNU DBM |
| 8 \bimodindex{dbm} | 11 library. {\sc gdbm} objects behave like mappings |
| 12 (dictionaries), except that keys and values are always strings. | |
| 13 Printing a {\sc gdbm} object doesn't print the keys and values, and the | |
| 14 \code{items()} and \code{values()} methods are not supported. | |
| 15 | |
| 16 The module defines the following constant and functions: | |
| 17 | |
| 18 \renewcommand{\indexsubitem}{(in module dbm)} | |
| 19 \begin{excdesc}{error} | |
| 20 Raised on dbm-specific errors, such as I/O errors. \code{KeyError} is | |
| 21 raised for general mapping errors like specifying an incorrect key. | |
| 22 \end{excdesc} | |
| 23 | |
| 24 \begin{funcdesc}{open}{filename\, \optional{flag\, \optional{mode}}} | |
| 25 Open a dbm database and return a dbm object. The \var{filename} | |
| 26 argument is the name of the database file (without the \file{.dir} or | |
| 27 \file{.pag} extensions). | |
| 28 | |
| 29 The 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 | |
| 35 Appending \code{f} to the flag opens the database in fast mode; | |
| 36 altered data will not automatically be written to the disk after every | |
| 37 change. This results in faster writes to the database, but may result | |
| 38 in an inconsistent database if the program crashes while the database | |
| 39 is still open. Use the \code{sync()} method to force any unwritten | |
| 40 data to be written to the disk. | |
| 41 | |
| 42 The optional \var{mode} argument is the \UNIX{} mode of the file, used | |
| 43 only when the database has to be created. It defaults to octal | |
| 44 \code{0666}. | |
| 45 \end{funcdesc} | |
| 46 | |
| 47 In addition to the dictionary-like methods, {\sc gdbm} objects have the | |
| 48 following methods: | |
| 49 | |
| 50 \begin{funcdesc}{firstkey}{} | |
| 51 It's possible to loop over every key in the database using this method | |
| 52 and the \code{nextkey()} method. The traversal is ordered by {\sc gdbm}'s | |
| 53 internal hash values, and won't be sorted by the key values. This | |
| 54 method returns the starting key. | |
| 55 \end{funcdesc} | |
| 56 | |
| 57 \begin{funcdesc}{nextkey}{key} | |
| 58 Returns the key that follows \var{key} in the traversal. The | |
| 59 following code prints every key in the database \code{db}, without having to | |
| 60 create a list in memory that contains them all: | |
| 61 \bcode\begin{verbatim} | |
| 62 k=db.firstkey() | |
| 63 while k!=None: | |
| 64 print k | |
| 65 k=db.nextkey(k) | |
| 66 \end{verbatim}\ecode | |
| 67 \end{funcdesc} | |
| 68 | |
| 69 \begin{funcdesc}{reorganize}{} | |
| 70 If you have carried out a lot of deletions and would like to shrink | |
| 71 the space used by the {\sc gdbm} file, this routine will reorganize the | |
| 72 database. {\sc gdbm} will not shorten the length of a database file except | |
| 73 by using this reorganization; otherwise, deleted file space will be | |
| 74 kept and reused as new (key,value) pairs are added. | |
| 75 \end{funcdesc} | |
| 76 | |
| 77 \begin{funcdesc}{sync}{} | |
| 78 When the database has been opened in fast mode, this method forces any | |
| 79 unwritten data to be written to the disk. | |
| 80 \end{funcdesc} | |
| 81 |
