2 :mod:`gdbm` --- GNU's reinterpretation of dbm
3 =============================================
7 :synopsis: GNU's reinterpretation of dbm.
10 .. index:: module: dbm
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.
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.
21 The module defines the following constant and functions:
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.
30 .. function:: open(filename, [flag, [mode]])
32 Open a ``gdbm`` database and return a ``gdbm`` object. The *filename* argument
33 is the name of the database file.
35 The optional *flag* argument can be:
37 +---------+-------------------------------------------+
39 +=========+===========================================+
40 | ``'r'`` | Open existing database for reading only |
42 +---------+-------------------------------------------+
43 | ``'w'`` | Open existing database for reading and |
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 +---------+-------------------------------------------+
53 The following additional characters may be appended to the flag to control
54 how the database is opened:
56 +---------+--------------------------------------------+
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 |
65 +---------+--------------------------------------------+
66 | ``'u'`` | Do not lock database. |
67 +---------+--------------------------------------------+
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.
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``.
76 In addition to the dictionary-like methods, ``gdbm`` objects have the following
80 .. function:: firstkey()
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
88 .. function:: nextkey(key)
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
100 .. function:: reorganize()
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.
111 When the database has been opened in fast mode, this method forces any
112 unwritten data to be written to the disk.
118 Generic interface to ``dbm``\ -style databases.
120 Module :mod:`whichdb`
121 Utility module used to determine the type of an existing database.