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