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 | 4f21d54 | 1999-04-05 22:18:12 +0000 | [diff] [blame] | 9 | This module is quite similar to the \refmodule{dbm}\refbimodindex{dbm} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 10 | module, but uses \code{gdbm} instead to provide some additional |
| 11 | functionality. Please note that the file formats created by |
| 12 | \code{gdbm} and \code{dbm} are incompatible. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 13 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 14 | The \module{gdbm} module provides an interface to the GNU DBM |
Fred Drake | c2297c1 | 1997-12-04 04:45:28 +0000 | [diff] [blame] | 15 | library. \code{gdbm} objects behave like mappings |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 16 | (dictionaries), except that keys and values are always strings. |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 17 | Printing a \code{gdbm} object doesn't print the keys and values, and |
| 18 | the \method{items()} and \method{values()} methods are not supported. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 19 | |
| 20 | The module defines the following constant and functions: |
| 21 | |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 22 | \begin{excdesc}{error} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 23 | Raised on \code{gdbm}-specific errors, such as I/O errors. |
| 24 | \exception{KeyError} is raised for general mapping errors like |
| 25 | specifying an incorrect key. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 26 | \end{excdesc} |
| 27 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 28 | \begin{funcdesc}{open}{filename, \optional{flag, \optional{mode}}} |
Fred Drake | c2297c1 | 1997-12-04 04:45:28 +0000 | [diff] [blame] | 29 | Open a \code{gdbm} database and return a \code{gdbm} object. The |
| 30 | \var{filename} argument is the name of the database file. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 31 | |
| 32 | The 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 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 38 | 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] | 39 | altered data will not automatically be written to the disk after every |
| 40 | change. This results in faster writes to the database, but may result |
| 41 | in an inconsistent database if the program crashes while the database |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 42 | 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] | 43 | data to be written to the disk. |
| 44 | |
| 45 | The optional \var{mode} argument is the \UNIX{} mode of the file, used |
| 46 | only when the database has to be created. It defaults to octal |
| 47 | \code{0666}. |
| 48 | \end{funcdesc} |
| 49 | |
Fred Drake | c2297c1 | 1997-12-04 04:45:28 +0000 | [diff] [blame] | 50 | 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] | 51 | following methods: |
| 52 | |
| 53 | \begin{funcdesc}{firstkey}{} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 54 | It's possible to loop over every key in the database using this method |
| 55 | and the \method{nextkey()} method. The traversal is ordered by |
| 56 | \code{gdbm}'s internal hash values, and won't be sorted by the key |
| 57 | values. This method returns the starting key. |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 58 | \end{funcdesc} |
| 59 | |
| 60 | \begin{funcdesc}{nextkey}{key} |
| 61 | Returns the key that follows \var{key} in the traversal. The |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 62 | following code prints every key in the database \code{db}, without |
| 63 | having to create a list in memory that contains them all: |
| 64 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 65 | \begin{verbatim} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 66 | k = db.firstkey() |
| 67 | while k != None: |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 68 | print k |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 69 | k = db.nextkey(k) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 70 | \end{verbatim} |
Guido van Rossum | 3c2a056 | 1997-07-17 16:29:42 +0000 | [diff] [blame] | 71 | \end{funcdesc} |
| 72 | |
| 73 | \begin{funcdesc}{reorganize}{} |
| 74 | 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] | 75 | the space used by the \code{gdbm} file, this routine will reorganize |
| 76 | the database. \code{gdbm} will not shorten the length of a database |
| 77 | file except by using this reorganization; otherwise, deleted file |
| 78 | 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] | 79 | \end{funcdesc} |
| 80 | |
| 81 | \begin{funcdesc}{sync}{} |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 82 | 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] | 83 | unwritten data to be written to the disk. |
| 84 | \end{funcdesc} |
| 85 | |
Fred Drake | 4f21d54 | 1999-04-05 22:18:12 +0000 | [diff] [blame] | 86 | |
| 87 | \begin{seealso} |
| 88 | \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.} |
| 89 | \seemodule{whichdb}{Utility module used to determine the type of an |
| 90 | existing database.} |
| 91 | \end{seealso} |