blob: ab45cac830e24d783d21beaa2186a235f0d171a3 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/dbm/__init__.py`
8
9--------------
10
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000011:mod:`dbm` is a generic interface to variants of the DBM database ---
Georg Brandlac958ce2010-07-29 14:46:07 +000012:mod:`dbm.gnu` or :mod:`dbm.ndbm`. If none of these modules is installed, the
13slow-but-simple implementation in module :mod:`dbm.dumb` will be used. There
Georg Brandl5d941342016-02-26 19:37:12 +010014is a `third party interface <https://www.jcea.es/programacion/pybsddb.htm>`_ to
Georg Brandlbb190152010-07-31 21:41:42 +000015the Oracle Berkeley DB.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000016
17
18.. exception:: error
19
20 A tuple containing the exceptions that can be raised by each of the supported
21 modules, with a unique exception also named :exc:`dbm.error` as the first
22 item --- the latter is used when :exc:`dbm.error` is raised.
23
24
25.. function:: whichdb(filename)
26
Georg Brandle8b0d612010-12-04 09:04:04 +000027 This function attempts to guess which of the several simple database modules
Georg Brandlac958ce2010-07-29 14:46:07 +000028 available --- :mod:`dbm.gnu`, :mod:`dbm.ndbm` or :mod:`dbm.dumb` --- should
29 be used to open a given file.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000030
31 Returns one of the following values: ``None`` if the file can't be opened
32 because it's unreadable or doesn't exist; the empty string (``''``) if the
33 file's format can't be guessed; or a string containing the required module
34 name, such as ``'dbm.ndbm'`` or ``'dbm.gnu'``.
35
36
Éric Araujo5c1a0c92011-04-20 19:11:12 +020037.. function:: open(file, flag='r', mode=0o666)
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000038
Éric Araujo5c1a0c92011-04-20 19:11:12 +020039 Open the database file *file* and return a corresponding object.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000040
41 If the database file already exists, the :func:`whichdb` function is used to
42 determine its type and the appropriate module is used; if it does not exist,
43 the first module listed above that can be imported is used.
44
Georg Brandl1a040582009-05-25 21:15:01 +000045 The optional *flag* argument can be:
46
47 +---------+-------------------------------------------+
48 | Value | Meaning |
49 +=========+===========================================+
50 | ``'r'`` | Open existing database for reading only |
51 | | (default) |
52 +---------+-------------------------------------------+
53 | ``'w'`` | Open existing database for reading and |
54 | | writing |
55 +---------+-------------------------------------------+
56 | ``'c'`` | Open database for reading and writing, |
57 | | creating it if it doesn't exist |
58 +---------+-------------------------------------------+
59 | ``'n'`` | Always create a new, empty database, open |
60 | | for reading and writing |
61 +---------+-------------------------------------------+
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000062
63 The optional *mode* argument is the Unix mode of the file, used only when the
64 database has to be created. It defaults to octal ``0o666`` (and will be
65 modified by the prevailing umask).
66
67
Georg Brandld9e833c2010-12-04 09:14:36 +000068The object returned by :func:`.open` supports the same basic functionality as
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000069dictionaries; keys and their corresponding values can be stored, retrieved, and
70deleted, and the :keyword:`in` operator and the :meth:`keys` method are
Georg Brandld9e833c2010-12-04 09:14:36 +000071available, as well as :meth:`get` and :meth:`setdefault`.
72
73.. versionchanged:: 3.2
74 :meth:`get` and :meth:`setdefault` are now available in all database modules.
75
76Key and values are always stored as bytes. This means that when
Brett Cannon7317c1e2008-11-25 19:19:17 +000077strings are used they are implicitly converted to the default encoding before
78being stored.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000079
Nick Coghlanc610aba2013-11-17 15:59:51 +100080These objects also support being used in a :keyword:`with` statement, which
81will automatically close them when done.
82
83.. versionchanged:: 3.4
84 Added native support for the context management protocol to the objects
85 returned by :func:`.open`.
86
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000087The following example records some hostnames and a corresponding title, and
88then prints out the contents of the database::
89
90 import dbm
91
92 # Open database, creating it if necessary.
Nick Coghlanc610aba2013-11-17 15:59:51 +100093 with dbm.open('cache', 'c') as db:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000094
Nick Coghlanc610aba2013-11-17 15:59:51 +100095 # Record some values
96 db[b'hello'] = b'there'
97 db['www.python.org'] = 'Python Website'
98 db['www.cnn.com'] = 'Cable News Network'
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000099
Nick Coghlanc610aba2013-11-17 15:59:51 +1000100 # Note that the keys are considered bytes now.
101 assert db[b'www.python.org'] == b'Python Website'
102 # Notice how the value is now in bytes.
103 assert db['www.cnn.com'] == b'Cable News Network'
Brett Cannon7317c1e2008-11-25 19:19:17 +0000104
Nick Coghlanc610aba2013-11-17 15:59:51 +1000105 # Often-used methods of the dict interface work too.
106 print(db.get('python.org', b'not present'))
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000107
Nick Coghlanc610aba2013-11-17 15:59:51 +1000108 # Storing a non-string key or value will raise an exception (most
109 # likely a TypeError).
110 db['www.yahoo.com'] = 4
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000111
Nick Coghlanc610aba2013-11-17 15:59:51 +1000112 # db is automatically closed when leaving the with statement.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000113
114
115.. seealso::
116
117 Module :mod:`shelve`
118 Persistence module which stores non-string data.
119
120
121The individual submodules are described in the following sections.
122
123
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000124:mod:`dbm.gnu` --- GNU's reinterpretation of dbm
125------------------------------------------------
126
127.. module:: dbm.gnu
128 :platform: Unix
129 :synopsis: GNU's reinterpretation of dbm.
130
Terry Jan Reedydcb6c882016-06-22 22:46:34 -0400131**Source code:** :source:`Lib/dbm/gnu.py`
132
133--------------
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000134
135This module is quite similar to the :mod:`dbm` module, but uses the GNU library
136``gdbm`` instead to provide some additional functionality. Please note that the
Brett Cannon7317c1e2008-11-25 19:19:17 +0000137file formats created by :mod:`dbm.gnu` and :mod:`dbm.ndbm` are incompatible.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000138
139The :mod:`dbm.gnu` module provides an interface to the GNU DBM library.
Brett Cannon7317c1e2008-11-25 19:19:17 +0000140``dbm.gnu.gdbm`` objects behave like mappings (dictionaries), except that keys and
141values are always converted to bytes before storing. Printing a ``gdbm``
142object doesn't print the
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000143keys and values, and the :meth:`items` and :meth:`values` methods are not
144supported.
145
146.. exception:: error
147
Brett Cannon7317c1e2008-11-25 19:19:17 +0000148 Raised on :mod:`dbm.gnu`-specific errors, such as I/O errors. :exc:`KeyError` is
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000149 raised for general mapping errors like specifying an incorrect key.
150
151
Georg Brandlc2a4f4f2009-04-10 09:03:43 +0000152.. function:: open(filename[, flag[, mode]])
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000153
154 Open a ``gdbm`` database and return a :class:`gdbm` object. The *filename*
155 argument is the name of the database file.
156
157 The optional *flag* argument can be:
158
159 +---------+-------------------------------------------+
160 | Value | Meaning |
161 +=========+===========================================+
162 | ``'r'`` | Open existing database for reading only |
163 | | (default) |
164 +---------+-------------------------------------------+
165 | ``'w'`` | Open existing database for reading and |
166 | | writing |
167 +---------+-------------------------------------------+
168 | ``'c'`` | Open database for reading and writing, |
169 | | creating it if it doesn't exist |
170 +---------+-------------------------------------------+
171 | ``'n'`` | Always create a new, empty database, open |
172 | | for reading and writing |
173 +---------+-------------------------------------------+
174
175 The following additional characters may be appended to the flag to control
176 how the database is opened:
177
178 +---------+--------------------------------------------+
179 | Value | Meaning |
180 +=========+============================================+
181 | ``'f'`` | Open the database in fast mode. Writes |
182 | | to the database will not be synchronized. |
183 +---------+--------------------------------------------+
184 | ``'s'`` | Synchronized mode. This will cause changes |
185 | | to the database to be immediately written |
186 | | to the file. |
187 +---------+--------------------------------------------+
188 | ``'u'`` | Do not lock database. |
189 +---------+--------------------------------------------+
190
191 Not all flags are valid for all versions of ``gdbm``. The module constant
192 :const:`open_flags` is a string of supported flag characters. The exception
193 :exc:`error` is raised if an invalid flag is specified.
194
195 The optional *mode* argument is the Unix mode of the file, used only when the
Georg Brandlf4a41232008-05-26 17:55:52 +0000196 database has to be created. It defaults to octal ``0o666``.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000197
198 In addition to the dictionary-like methods, ``gdbm`` objects have the
199 following methods:
200
201 .. method:: gdbm.firstkey()
202
203 It's possible to loop over every key in the database using this method and the
204 :meth:`nextkey` method. The traversal is ordered by ``gdbm``'s internal
205 hash values, and won't be sorted by the key values. This method returns
206 the starting key.
207
208 .. method:: gdbm.nextkey(key)
209
210 Returns the key that follows *key* in the traversal. The following code prints
211 every key in the database ``db``, without having to create a list in memory that
212 contains them all::
213
214 k = db.firstkey()
215 while k != None:
216 print(k)
217 k = db.nextkey(k)
218
219 .. method:: gdbm.reorganize()
220
221 If you have carried out a lot of deletions and would like to shrink the space
222 used by the ``gdbm`` file, this routine will reorganize the database. ``gdbm``
Brett Cannon7317c1e2008-11-25 19:19:17 +0000223 objects will not shorten the length of a database file except by using this
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000224 reorganization; otherwise, deleted file space will be kept and reused as new
225 (key, value) pairs are added.
226
227 .. method:: gdbm.sync()
228
229 When the database has been opened in fast mode, this method forces any
230 unwritten data to be written to the disk.
231
Jesus Ceaac4b7f72014-06-25 13:05:31 +0200232 .. method:: gdbm.close()
233
234 Close the ``gdbm`` database.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000235
236:mod:`dbm.ndbm` --- Interface based on ndbm
237-------------------------------------------
238
239.. module:: dbm.ndbm
Georg Brandl116aa622007-08-15 14:28:22 +0000240 :platform: Unix
241 :synopsis: The standard "database" interface, based on ndbm.
242
Terry Jan Reedydcb6c882016-06-22 22:46:34 -0400243**Source code:** :source:`Lib/dbm/ndbm.py`
244
245--------------
Georg Brandl116aa622007-08-15 14:28:22 +0000246
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000247The :mod:`dbm.ndbm` module provides an interface to the Unix "(n)dbm" library.
248Dbm objects behave like mappings (dictionaries), except that keys and values are
Brett Cannon7317c1e2008-11-25 19:19:17 +0000249always stored as bytes. Printing a ``dbm`` object doesn't print the keys and
250values, and the :meth:`items` and :meth:`values` methods are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000251
Georg Brandlac958ce2010-07-29 14:46:07 +0000252This module can be used with the "classic" ndbm interface or the GNU GDBM
253compatibility interface. On Unix, the :program:`configure` script will attempt
254to locate the appropriate header file to simplify building this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Georg Brandl116aa622007-08-15 14:28:22 +0000256.. exception:: error
257
Brett Cannon7317c1e2008-11-25 19:19:17 +0000258 Raised on :mod:`dbm.ndbm`-specific errors, such as I/O errors. :exc:`KeyError` is raised
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000259 for general mapping errors like specifying an incorrect key.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261
262.. data:: library
263
264 Name of the ``ndbm`` implementation library used.
265
266
267.. function:: open(filename[, flag[, mode]])
268
Jesus Ceaac4b7f72014-06-25 13:05:31 +0200269 Open a dbm database and return a ``ndbm`` object. The *filename* argument is the
Georg Brandlac958ce2010-07-29 14:46:07 +0000270 name of the database file (without the :file:`.dir` or :file:`.pag` extensions).
Georg Brandl116aa622007-08-15 14:28:22 +0000271
272 The optional *flag* argument must be one of these values:
273
274 +---------+-------------------------------------------+
275 | Value | Meaning |
276 +=========+===========================================+
277 | ``'r'`` | Open existing database for reading only |
278 | | (default) |
279 +---------+-------------------------------------------+
280 | ``'w'`` | Open existing database for reading and |
281 | | writing |
282 +---------+-------------------------------------------+
283 | ``'c'`` | Open database for reading and writing, |
284 | | creating it if it doesn't exist |
285 +---------+-------------------------------------------+
286 | ``'n'`` | Always create a new, empty database, open |
287 | | for reading and writing |
288 +---------+-------------------------------------------+
289
290 The optional *mode* argument is the Unix mode of the file, used only when the
Georg Brandlf4a41232008-05-26 17:55:52 +0000291 database has to be created. It defaults to octal ``0o666`` (and will be
Georg Brandl116aa622007-08-15 14:28:22 +0000292 modified by the prevailing umask).
293
Jesus Ceaac4b7f72014-06-25 13:05:31 +0200294 In addition to the dictionary-like methods, ``ndbm`` objects
295 provide the following method:
296
297 .. method:: ndbm.close()
298
299 Close the ``ndbm`` database.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Georg Brandl116aa622007-08-15 14:28:22 +0000301
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000302:mod:`dbm.dumb` --- Portable DBM implementation
303-----------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000304
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000305.. module:: dbm.dumb
306 :synopsis: Portable implementation of the simple DBM interface.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Terry Jan Reedydcb6c882016-06-22 22:46:34 -0400308**Source code:** :source:`Lib/dbm/dumb.py`
309
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000310.. index:: single: databases
Georg Brandl116aa622007-08-15 14:28:22 +0000311
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000312.. note::
313
314 The :mod:`dbm.dumb` module is intended as a last resort fallback for the
Brett Cannon7317c1e2008-11-25 19:19:17 +0000315 :mod:`dbm` module when a more robust module is not available. The :mod:`dbm.dumb`
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000316 module is not written for speed and is not nearly as heavily used as the other
317 database modules.
318
Terry Jan Reedydcb6c882016-06-22 22:46:34 -0400319--------------
320
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000321The :mod:`dbm.dumb` module provides a persistent dictionary-like interface which
Brett Cannon7317c1e2008-11-25 19:19:17 +0000322is written entirely in Python. Unlike other modules such as :mod:`dbm.gnu` no
Benjamin Peterson9a46cab2008-09-08 02:49:30 +0000323external library is required. As with other persistent mappings, the keys and
Brett Cannon7317c1e2008-11-25 19:19:17 +0000324values are always stored as bytes.
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000325
326The module defines the following:
327
328
329.. exception:: error
330
Brett Cannon7317c1e2008-11-25 19:19:17 +0000331 Raised on :mod:`dbm.dumb`-specific errors, such as I/O errors. :exc:`KeyError` is
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000332 raised for general mapping errors like specifying an incorrect key.
333
334
335.. function:: open(filename[, flag[, mode]])
336
Brett Cannon7317c1e2008-11-25 19:19:17 +0000337 Open a ``dumbdbm`` database and return a dumbdbm object. The *filename* argument is
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000338 the basename of the database file (without any specific extensions). When a
339 dumbdbm database is created, files with :file:`.dat` and :file:`.dir` extensions
340 are created.
341
Serhiy Storchaka6c85efa52018-02-05 22:47:31 +0200342 The optional *flag* argument can be:
343
344 +---------+-------------------------------------------+
345 | Value | Meaning |
346 +=========+===========================================+
347 | ``'r'`` | Open existing database for reading only |
348 | | (default) |
349 +---------+-------------------------------------------+
350 | ``'w'`` | Open existing database for reading and |
351 | | writing |
352 +---------+-------------------------------------------+
353 | ``'c'`` | Open database for reading and writing, |
354 | | creating it if it doesn't exist |
355 +---------+-------------------------------------------+
356 | ``'n'`` | Always create a new, empty database, open |
357 | | for reading and writing |
358 +---------+-------------------------------------------+
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000359
360 The optional *mode* argument is the Unix mode of the file, used only when the
361 database has to be created. It defaults to octal ``0o666`` (and will be modified
362 by the prevailing umask).
363
Brett Cannon10485eb2018-03-09 15:58:40 -0800364 .. warning::
365 It is possible to crash the Python interpreter when loading a database
366 with a sufficiently large/complex entry due to stack depth limitations in
367 Python's AST compiler.
368
Serhiy Storchakab398d332014-06-10 21:16:00 +0300369 .. versionchanged:: 3.5
370 :func:`.open` always creates a new database when the flag has the value
371 ``'n'``.
372
Serhiy Storchaka6c85efa52018-02-05 22:47:31 +0200373 .. versionchanged:: 3.8
374 A database opened with flags ``'r'`` is now read-only. Opening with
375 flags ``'r'`` and ``'w'`` no longer creates a database if it does not
376 exist.
Serhiy Storchaka0122ae92016-07-06 12:21:58 +0300377
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300378 In addition to the methods provided by the
379 :class:`collections.abc.MutableMapping` class, :class:`dumbdbm` objects
Jesus Ceaac4b7f72014-06-25 13:05:31 +0200380 provide the following methods:
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000381
382 .. method:: dumbdbm.sync()
383
384 Synchronize the on-disk directory and data files. This method is called
385 by the :meth:`Shelve.sync` method.
Jesus Ceaac4b7f72014-06-25 13:05:31 +0200386
387 .. method:: dumbdbm.close()
388
389 Close the ``dumbdbm`` database.
390