blob: c7c73475eb64fd0bdf85916ef174a668e92b4482 [file] [log] [blame]
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00001:mod:`dbm` --- Interfaces to Unix "databases"
2=============================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: dbm
Georg Brandl0a7ac7d2008-05-26 10:29:35 +00005 :synopsis: Interfaces to various Unix "database" formats.
6
7:mod:`dbm` is a generic interface to variants of the DBM database ---
Georg Brandl611f8f52010-08-01 19:17:57 +00008:mod:`dbm.gnu` or :mod:`dbm.ndbm`. If none of these modules is installed, the
9slow-but-simple implementation in module :mod:`dbm.dumb` will be used. There
10is a `third party interface <http://www.jcea.es/programacion/pybsddb.htm>`_ to
11the Oracle Berkeley DB.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000012
13
14.. exception:: error
15
16 A tuple containing the exceptions that can be raised by each of the supported
17 modules, with a unique exception also named :exc:`dbm.error` as the first
18 item --- the latter is used when :exc:`dbm.error` is raised.
19
20
21.. function:: whichdb(filename)
22
Georg Brandlf6c8fd62011-02-25 09:48:21 +000023 This function attempts to guess which of the several simple database modules
Georg Brandl1fa11af2010-08-01 21:03:01 +000024 available --- :mod:`dbm.gnu`, :mod:`dbm.ndbm` or :mod:`dbm.dumb` --- should
25 be used to open a given file.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000026
27 Returns one of the following values: ``None`` if the file can't be opened
28 because it's unreadable or doesn't exist; the empty string (``''``) if the
29 file's format can't be guessed; or a string containing the required module
30 name, such as ``'dbm.ndbm'`` or ``'dbm.gnu'``.
31
32
Georg Brandlc2a4f4f2009-04-10 09:03:43 +000033.. function:: open(filename, flag='r', mode=0o666)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000034
35 Open the database file *filename* and return a corresponding object.
36
37 If the database file already exists, the :func:`whichdb` function is used to
38 determine its type and the appropriate module is used; if it does not exist,
39 the first module listed above that can be imported is used.
40
Georg Brandl1a040582009-05-25 21:15:01 +000041 The optional *flag* argument can be:
42
43 +---------+-------------------------------------------+
44 | Value | Meaning |
45 +=========+===========================================+
46 | ``'r'`` | Open existing database for reading only |
47 | | (default) |
48 +---------+-------------------------------------------+
49 | ``'w'`` | Open existing database for reading and |
50 | | writing |
51 +---------+-------------------------------------------+
52 | ``'c'`` | Open database for reading and writing, |
53 | | creating it if it doesn't exist |
54 +---------+-------------------------------------------+
55 | ``'n'`` | Always create a new, empty database, open |
56 | | for reading and writing |
57 +---------+-------------------------------------------+
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000058
59 The optional *mode* argument is the Unix mode of the file, used only when the
60 database has to be created. It defaults to octal ``0o666`` (and will be
61 modified by the prevailing umask).
62
63
Georg Brandlc5605df2009-08-13 08:26:44 +000064The object returned by :func:`.open` supports most of the same functionality as
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000065dictionaries; keys and their corresponding values can be stored, retrieved, and
66deleted, and the :keyword:`in` operator and the :meth:`keys` method are
Brett Cannon7317c1e2008-11-25 19:19:17 +000067available. Key and values are always stored as bytes. This means that when
68strings are used they are implicitly converted to the default encoding before
69being stored.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000070
71The following example records some hostnames and a corresponding title, and
72then prints out the contents of the database::
73
74 import dbm
75
76 # Open database, creating it if necessary.
77 db = dbm.open('cache', 'c')
78
79 # Record some values
Brett Cannon7317c1e2008-11-25 19:19:17 +000080 db[b'hello'] = b'there'
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000081 db['www.python.org'] = 'Python Website'
82 db['www.cnn.com'] = 'Cable News Network'
83
Brett Cannon7317c1e2008-11-25 19:19:17 +000084 # Note that the keys are considered bytes now.
85 assert db[b'www.python.org'] == b'Python Website'
86 # Notice how the value is now in bytes.
87 assert db['www.cnn.com'] == b'Cable News Network'
88
Georg Brandld98934c2011-02-25 10:03:34 +000089 # Often-used methods of the dict interface work too.
90 print(db.get('python.org', b'not present'))
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000091
92 # Storing a non-string key or value will raise an exception (most
93 # likely a TypeError).
94 db['www.yahoo.com'] = 4
95
96 # Close when done.
97 db.close()
98
99
100.. seealso::
101
102 Module :mod:`shelve`
103 Persistence module which stores non-string data.
104
105
106The individual submodules are described in the following sections.
107
108
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000109:mod:`dbm.gnu` --- GNU's reinterpretation of dbm
110------------------------------------------------
111
112.. module:: dbm.gnu
113 :platform: Unix
114 :synopsis: GNU's reinterpretation of dbm.
115
116
117This module is quite similar to the :mod:`dbm` module, but uses the GNU library
118``gdbm`` instead to provide some additional functionality. Please note that the
Brett Cannon7317c1e2008-11-25 19:19:17 +0000119file formats created by :mod:`dbm.gnu` and :mod:`dbm.ndbm` are incompatible.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000120
121The :mod:`dbm.gnu` module provides an interface to the GNU DBM library.
Brett Cannon7317c1e2008-11-25 19:19:17 +0000122``dbm.gnu.gdbm`` objects behave like mappings (dictionaries), except that keys and
123values are always converted to bytes before storing. Printing a ``gdbm``
124object doesn't print the
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000125keys and values, and the :meth:`items` and :meth:`values` methods are not
126supported.
127
128.. exception:: error
129
Brett Cannon7317c1e2008-11-25 19:19:17 +0000130 Raised on :mod:`dbm.gnu`-specific errors, such as I/O errors. :exc:`KeyError` is
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000131 raised for general mapping errors like specifying an incorrect key.
132
133
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000134.. function:: open(filename[, flag[, mode]])
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000135
136 Open a ``gdbm`` database and return a :class:`gdbm` object. The *filename*
137 argument is the name of the database file.
138
139 The optional *flag* argument can be:
140
141 +---------+-------------------------------------------+
142 | Value | Meaning |
143 +=========+===========================================+
144 | ``'r'`` | Open existing database for reading only |
145 | | (default) |
146 +---------+-------------------------------------------+
147 | ``'w'`` | Open existing database for reading and |
148 | | writing |
149 +---------+-------------------------------------------+
150 | ``'c'`` | Open database for reading and writing, |
151 | | creating it if it doesn't exist |
152 +---------+-------------------------------------------+
153 | ``'n'`` | Always create a new, empty database, open |
154 | | for reading and writing |
155 +---------+-------------------------------------------+
156
157 The following additional characters may be appended to the flag to control
158 how the database is opened:
159
160 +---------+--------------------------------------------+
161 | Value | Meaning |
162 +=========+============================================+
163 | ``'f'`` | Open the database in fast mode. Writes |
164 | | to the database will not be synchronized. |
165 +---------+--------------------------------------------+
166 | ``'s'`` | Synchronized mode. This will cause changes |
167 | | to the database to be immediately written |
168 | | to the file. |
169 +---------+--------------------------------------------+
170 | ``'u'`` | Do not lock database. |
171 +---------+--------------------------------------------+
172
173 Not all flags are valid for all versions of ``gdbm``. The module constant
174 :const:`open_flags` is a string of supported flag characters. The exception
175 :exc:`error` is raised if an invalid flag is specified.
176
177 The optional *mode* argument is the Unix mode of the file, used only when the
Georg Brandlf4a41232008-05-26 17:55:52 +0000178 database has to be created. It defaults to octal ``0o666``.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000179
180 In addition to the dictionary-like methods, ``gdbm`` objects have the
181 following methods:
182
183 .. method:: gdbm.firstkey()
184
185 It's possible to loop over every key in the database using this method and the
186 :meth:`nextkey` method. The traversal is ordered by ``gdbm``'s internal
187 hash values, and won't be sorted by the key values. This method returns
188 the starting key.
189
190 .. method:: gdbm.nextkey(key)
191
192 Returns the key that follows *key* in the traversal. The following code prints
193 every key in the database ``db``, without having to create a list in memory that
194 contains them all::
195
196 k = db.firstkey()
197 while k != None:
198 print(k)
199 k = db.nextkey(k)
200
201 .. method:: gdbm.reorganize()
202
203 If you have carried out a lot of deletions and would like to shrink the space
204 used by the ``gdbm`` file, this routine will reorganize the database. ``gdbm``
Brett Cannon7317c1e2008-11-25 19:19:17 +0000205 objects will not shorten the length of a database file except by using this
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000206 reorganization; otherwise, deleted file space will be kept and reused as new
207 (key, value) pairs are added.
208
209 .. method:: gdbm.sync()
210
211 When the database has been opened in fast mode, this method forces any
212 unwritten data to be written to the disk.
213
214
215:mod:`dbm.ndbm` --- Interface based on ndbm
216-------------------------------------------
217
218.. module:: dbm.ndbm
Georg Brandl116aa622007-08-15 14:28:22 +0000219 :platform: Unix
220 :synopsis: The standard "database" interface, based on ndbm.
221
222
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000223The :mod:`dbm.ndbm` module provides an interface to the Unix "(n)dbm" library.
224Dbm objects behave like mappings (dictionaries), except that keys and values are
Brett Cannon7317c1e2008-11-25 19:19:17 +0000225always stored as bytes. Printing a ``dbm`` object doesn't print the keys and
226values, and the :meth:`items` and :meth:`values` methods are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000227
Georg Brandl1fa11af2010-08-01 21:03:01 +0000228This module can be used with the "classic" ndbm interface or the GNU GDBM
229compatibility interface. On Unix, the :program:`configure` script will attempt
230to locate the appropriate header file to simplify building this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Georg Brandl116aa622007-08-15 14:28:22 +0000232.. exception:: error
233
Brett Cannon7317c1e2008-11-25 19:19:17 +0000234 Raised on :mod:`dbm.ndbm`-specific errors, such as I/O errors. :exc:`KeyError` is raised
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000235 for general mapping errors like specifying an incorrect key.
Georg Brandl116aa622007-08-15 14:28:22 +0000236
237
238.. data:: library
239
240 Name of the ``ndbm`` implementation library used.
241
242
243.. function:: open(filename[, flag[, mode]])
244
Brett Cannon7317c1e2008-11-25 19:19:17 +0000245 Open a dbm database and return a ``dbm`` object. The *filename* argument is the
Georg Brandl1fa11af2010-08-01 21:03:01 +0000246 name of the database file (without the :file:`.dir` or :file:`.pag` extensions).
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248 The optional *flag* argument must be one of these values:
249
250 +---------+-------------------------------------------+
251 | Value | Meaning |
252 +=========+===========================================+
253 | ``'r'`` | Open existing database for reading only |
254 | | (default) |
255 +---------+-------------------------------------------+
256 | ``'w'`` | Open existing database for reading and |
257 | | writing |
258 +---------+-------------------------------------------+
259 | ``'c'`` | Open database for reading and writing, |
260 | | creating it if it doesn't exist |
261 +---------+-------------------------------------------+
262 | ``'n'`` | Always create a new, empty database, open |
263 | | for reading and writing |
264 +---------+-------------------------------------------+
265
266 The optional *mode* argument is the Unix mode of the file, used only when the
Georg Brandlf4a41232008-05-26 17:55:52 +0000267 database has to be created. It defaults to octal ``0o666`` (and will be
Georg Brandl116aa622007-08-15 14:28:22 +0000268 modified by the prevailing umask).
269
270
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000272:mod:`dbm.dumb` --- Portable DBM implementation
273-----------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000275.. module:: dbm.dumb
276 :synopsis: Portable implementation of the simple DBM interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000278.. index:: single: databases
Georg Brandl116aa622007-08-15 14:28:22 +0000279
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000280.. note::
281
282 The :mod:`dbm.dumb` module is intended as a last resort fallback for the
Brett Cannon7317c1e2008-11-25 19:19:17 +0000283 :mod:`dbm` module when a more robust module is not available. The :mod:`dbm.dumb`
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000284 module is not written for speed and is not nearly as heavily used as the other
285 database modules.
286
287The :mod:`dbm.dumb` module provides a persistent dictionary-like interface which
Brett Cannon7317c1e2008-11-25 19:19:17 +0000288is written entirely in Python. Unlike other modules such as :mod:`dbm.gnu` no
Benjamin Peterson9a46cab2008-09-08 02:49:30 +0000289external library is required. As with other persistent mappings, the keys and
Brett Cannon7317c1e2008-11-25 19:19:17 +0000290values are always stored as bytes.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000291
292The module defines the following:
293
294
295.. exception:: error
296
Brett Cannon7317c1e2008-11-25 19:19:17 +0000297 Raised on :mod:`dbm.dumb`-specific errors, such as I/O errors. :exc:`KeyError` is
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000298 raised for general mapping errors like specifying an incorrect key.
299
300
301.. function:: open(filename[, flag[, mode]])
302
Brett Cannon7317c1e2008-11-25 19:19:17 +0000303 Open a ``dumbdbm`` database and return a dumbdbm object. The *filename* argument is
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000304 the basename of the database file (without any specific extensions). When a
305 dumbdbm database is created, files with :file:`.dat` and :file:`.dir` extensions
306 are created.
307
308 The optional *flag* argument is currently ignored; the database is always opened
309 for update, and will be created if it does not exist.
310
311 The optional *mode* argument is the Unix mode of the file, used only when the
312 database has to be created. It defaults to octal ``0o666`` (and will be modified
313 by the prevailing umask).
314
315 In addition to the methods provided by the :class:`collections.MutableMapping` class,
316 :class:`dumbdbm` objects provide the following method:
317
318 .. method:: dumbdbm.sync()
319
320 Synchronize the on-disk directory and data files. This method is called
321 by the :meth:`Shelve.sync` method.