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