blob: f69e667e568d3e6bdc87282418bcb6adecd101ff [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
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
12This module is quite similar to the :mod:`dbm` module, but uses ``gdbm`` instead
13to provide some additional functionality. Please note that the file formats
14created by ``gdbm`` and ``dbm`` are incompatible.
15
16The :mod:`gdbm` module provides an interface to the GNU DBM library. ``gdbm``
17objects behave like mappings (dictionaries), except that keys and values are
18always strings. Printing a ``gdbm`` object doesn't print the keys and values,
19and the :meth:`items` and :meth:`values` methods are not supported.
20
21The 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
76In addition to the dictionary-like methods, ``gdbm`` objects have the following
77methods:
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:
Georg Brandl6911e3c2007-09-04 07:15:32 +000096 print(k)
Georg Brandl116aa622007-08-15 14:28:22 +000097 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