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