Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 1 | :mod:`dbm` --- Interfaces to Unix "databases" |
| 2 | ============================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: dbm |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 5 | :synopsis: Interfaces to various Unix "database" formats. |
| 6 | |
| 7 | :mod:`dbm` is a generic interface to variants of the DBM database --- |
| 8 | :mod:`dbm.bsd` (requires :mod:`bsddb`), :mod:`dbm.gnu`, or :mod:`dbm.ndbm`. If |
| 9 | none of these modules is installed, the slow-but-simple implementation in module |
| 10 | :mod:`dbm.dumb` will be used. |
| 11 | |
| 12 | |
| 13 | .. exception:: error |
| 14 | |
| 15 | A tuple containing the exceptions that can be raised by each of the supported |
| 16 | modules, with a unique exception also named :exc:`dbm.error` as the first |
| 17 | item --- the latter is used when :exc:`dbm.error` is raised. |
| 18 | |
| 19 | |
| 20 | .. function:: whichdb(filename) |
| 21 | |
| 22 | This functionattempts to guess which of the several simple database modules |
| 23 | available --- :mod:`dbm.bsd`, :mod:`dbm.gnu`, :mod:`dbm.ndbm` or |
| 24 | :mod:`dbm.dumb` --- should be used to open a given file. |
| 25 | |
| 26 | Returns one of the following values: ``None`` if the file can't be opened |
| 27 | because it's unreadable or doesn't exist; the empty string (``''``) if the |
| 28 | file's format can't be guessed; or a string containing the required module |
| 29 | name, such as ``'dbm.ndbm'`` or ``'dbm.gnu'``. |
| 30 | |
| 31 | |
| 32 | .. function:: open(filename[, flag[, mode]]) |
| 33 | |
| 34 | Open the database file *filename* and return a corresponding object. |
| 35 | |
| 36 | If the database file already exists, the :func:`whichdb` function is used to |
| 37 | determine its type and the appropriate module is used; if it does not exist, |
| 38 | the first module listed above that can be imported is used. |
| 39 | |
| 40 | The optional *flag* argument can be ``'r'`` to open an existing database for |
| 41 | reading only, ``'w'`` to open an existing database for reading and writing, |
| 42 | ``'c'`` to create the database if it doesn't exist, or ``'n'``, which will |
| 43 | always create a new empty database. If not specified, the default value is |
| 44 | ``'r'``. |
| 45 | |
| 46 | The optional *mode* argument is the Unix mode of the file, used only when the |
| 47 | database has to be created. It defaults to octal ``0o666`` (and will be |
| 48 | modified by the prevailing umask). |
| 49 | |
| 50 | |
| 51 | The object returned by :func:`open` supports most of the same functionality as |
| 52 | dictionaries; keys and their corresponding values can be stored, retrieved, and |
| 53 | deleted, and the :keyword:`in` operator and the :meth:`keys` method are |
| 54 | available. Keys and values must always be strings. |
| 55 | |
| 56 | The following example records some hostnames and a corresponding title, and |
| 57 | then prints out the contents of the database:: |
| 58 | |
| 59 | import dbm |
| 60 | |
| 61 | # Open database, creating it if necessary. |
| 62 | db = dbm.open('cache', 'c') |
| 63 | |
| 64 | # Record some values |
| 65 | db['www.python.org'] = 'Python Website' |
| 66 | db['www.cnn.com'] = 'Cable News Network' |
| 67 | |
| 68 | # Loop through contents. Other dictionary methods |
| 69 | # such as .keys(), .values() also work. |
| 70 | for k, v in db.iteritems(): |
| 71 | print(k, '\t', v) |
| 72 | |
| 73 | # Storing a non-string key or value will raise an exception (most |
| 74 | # likely a TypeError). |
| 75 | db['www.yahoo.com'] = 4 |
| 76 | |
| 77 | # Close when done. |
| 78 | db.close() |
| 79 | |
| 80 | |
| 81 | .. seealso:: |
| 82 | |
| 83 | Module :mod:`shelve` |
| 84 | Persistence module which stores non-string data. |
| 85 | |
| 86 | |
| 87 | The individual submodules are described in the following sections. |
| 88 | |
| 89 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 90 | :mod:`dbm.gnu` --- GNU's reinterpretation of dbm |
| 91 | ------------------------------------------------ |
| 92 | |
| 93 | .. module:: dbm.gnu |
| 94 | :platform: Unix |
| 95 | :synopsis: GNU's reinterpretation of dbm. |
| 96 | |
| 97 | |
| 98 | This module is quite similar to the :mod:`dbm` module, but uses the GNU library |
| 99 | ``gdbm`` instead to provide some additional functionality. Please note that the |
| 100 | file formats created by ``gdbm`` and ``dbm`` are incompatible. |
| 101 | |
| 102 | The :mod:`dbm.gnu` module provides an interface to the GNU DBM library. |
| 103 | ``gdbm`` objects behave like mappings (dictionaries), except that keys and |
| 104 | values are always strings. Printing a :mod:`dbm.gnu` object doesn't print the |
| 105 | keys and values, and the :meth:`items` and :meth:`values` methods are not |
| 106 | supported. |
| 107 | |
| 108 | .. exception:: error |
| 109 | |
| 110 | Raised on ``gdbm``\ -specific errors, such as I/O errors. :exc:`KeyError` is |
| 111 | raised for general mapping errors like specifying an incorrect key. |
| 112 | |
| 113 | |
| 114 | .. function:: open(filename, [flag, [mode]]) |
| 115 | |
| 116 | Open a ``gdbm`` database and return a :class:`gdbm` object. The *filename* |
| 117 | argument is the name of the database file. |
| 118 | |
| 119 | The optional *flag* argument can be: |
| 120 | |
| 121 | +---------+-------------------------------------------+ |
| 122 | | Value | Meaning | |
| 123 | +=========+===========================================+ |
| 124 | | ``'r'`` | Open existing database for reading only | |
| 125 | | | (default) | |
| 126 | +---------+-------------------------------------------+ |
| 127 | | ``'w'`` | Open existing database for reading and | |
| 128 | | | writing | |
| 129 | +---------+-------------------------------------------+ |
| 130 | | ``'c'`` | Open database for reading and writing, | |
| 131 | | | creating it if it doesn't exist | |
| 132 | +---------+-------------------------------------------+ |
| 133 | | ``'n'`` | Always create a new, empty database, open | |
| 134 | | | for reading and writing | |
| 135 | +---------+-------------------------------------------+ |
| 136 | |
| 137 | The following additional characters may be appended to the flag to control |
| 138 | how the database is opened: |
| 139 | |
| 140 | +---------+--------------------------------------------+ |
| 141 | | Value | Meaning | |
| 142 | +=========+============================================+ |
| 143 | | ``'f'`` | Open the database in fast mode. Writes | |
| 144 | | | to the database will not be synchronized. | |
| 145 | +---------+--------------------------------------------+ |
| 146 | | ``'s'`` | Synchronized mode. This will cause changes | |
| 147 | | | to the database to be immediately written | |
| 148 | | | to the file. | |
| 149 | +---------+--------------------------------------------+ |
| 150 | | ``'u'`` | Do not lock database. | |
| 151 | +---------+--------------------------------------------+ |
| 152 | |
| 153 | Not all flags are valid for all versions of ``gdbm``. The module constant |
| 154 | :const:`open_flags` is a string of supported flag characters. The exception |
| 155 | :exc:`error` is raised if an invalid flag is specified. |
| 156 | |
| 157 | The optional *mode* argument is the Unix mode of the file, used only when the |
Georg Brandl | f4a4123 | 2008-05-26 17:55:52 +0000 | [diff] [blame] | 158 | database has to be created. It defaults to octal ``0o666``. |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 159 | |
| 160 | In addition to the dictionary-like methods, ``gdbm`` objects have the |
| 161 | following methods: |
| 162 | |
| 163 | .. method:: gdbm.firstkey() |
| 164 | |
| 165 | It's possible to loop over every key in the database using this method and the |
| 166 | :meth:`nextkey` method. The traversal is ordered by ``gdbm``'s internal |
| 167 | hash values, and won't be sorted by the key values. This method returns |
| 168 | the starting key. |
| 169 | |
| 170 | .. method:: gdbm.nextkey(key) |
| 171 | |
| 172 | Returns the key that follows *key* in the traversal. The following code prints |
| 173 | every key in the database ``db``, without having to create a list in memory that |
| 174 | contains them all:: |
| 175 | |
| 176 | k = db.firstkey() |
| 177 | while k != None: |
| 178 | print(k) |
| 179 | k = db.nextkey(k) |
| 180 | |
| 181 | .. method:: gdbm.reorganize() |
| 182 | |
| 183 | If you have carried out a lot of deletions and would like to shrink the space |
| 184 | used by the ``gdbm`` file, this routine will reorganize the database. ``gdbm`` |
| 185 | will not shorten the length of a database file except by using this |
| 186 | reorganization; otherwise, deleted file space will be kept and reused as new |
| 187 | (key, value) pairs are added. |
| 188 | |
| 189 | .. method:: gdbm.sync() |
| 190 | |
| 191 | When the database has been opened in fast mode, this method forces any |
| 192 | unwritten data to be written to the disk. |
| 193 | |
| 194 | |
| 195 | :mod:`dbm.ndbm` --- Interface based on ndbm |
| 196 | ------------------------------------------- |
| 197 | |
| 198 | .. module:: dbm.ndbm |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 199 | :platform: Unix |
| 200 | :synopsis: The standard "database" interface, based on ndbm. |
| 201 | |
| 202 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 203 | The :mod:`dbm.ndbm` module provides an interface to the Unix "(n)dbm" library. |
| 204 | Dbm objects behave like mappings (dictionaries), except that keys and values are |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 205 | always strings. Printing a dbm object doesn't print the keys and values, and the |
| 206 | :meth:`items` and :meth:`values` methods are not supported. |
| 207 | |
| 208 | This module can be used with the "classic" ndbm interface, the BSD DB |
| 209 | compatibility interface, or the GNU GDBM compatibility interface. On Unix, the |
| 210 | :program:`configure` script will attempt to locate the appropriate header file |
| 211 | to simplify building this module. |
| 212 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 213 | .. exception:: error |
| 214 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 215 | Raised on dbm-specific errors, such as I/O errors. :exc:`KeyError` is raised |
| 216 | for general mapping errors like specifying an incorrect key. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 217 | |
| 218 | |
| 219 | .. data:: library |
| 220 | |
| 221 | Name of the ``ndbm`` implementation library used. |
| 222 | |
| 223 | |
| 224 | .. function:: open(filename[, flag[, mode]]) |
| 225 | |
| 226 | Open a dbm database and return a dbm object. The *filename* argument is the |
| 227 | name of the database file (without the :file:`.dir` or :file:`.pag` extensions; |
| 228 | note that the BSD DB implementation of the interface will append the extension |
| 229 | :file:`.db` and only create one file). |
| 230 | |
| 231 | The optional *flag* argument must be one of these values: |
| 232 | |
| 233 | +---------+-------------------------------------------+ |
| 234 | | Value | Meaning | |
| 235 | +=========+===========================================+ |
| 236 | | ``'r'`` | Open existing database for reading only | |
| 237 | | | (default) | |
| 238 | +---------+-------------------------------------------+ |
| 239 | | ``'w'`` | Open existing database for reading and | |
| 240 | | | writing | |
| 241 | +---------+-------------------------------------------+ |
| 242 | | ``'c'`` | Open database for reading and writing, | |
| 243 | | | creating it if it doesn't exist | |
| 244 | +---------+-------------------------------------------+ |
| 245 | | ``'n'`` | Always create a new, empty database, open | |
| 246 | | | for reading and writing | |
| 247 | +---------+-------------------------------------------+ |
| 248 | |
| 249 | The optional *mode* argument is the Unix mode of the file, used only when the |
Georg Brandl | f4a4123 | 2008-05-26 17:55:52 +0000 | [diff] [blame] | 250 | database has to be created. It defaults to octal ``0o666`` (and will be |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 251 | modified by the prevailing umask). |
| 252 | |
| 253 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 254 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 255 | :mod:`dbm.dumb` --- Portable DBM implementation |
| 256 | ----------------------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 257 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 258 | .. module:: dbm.dumb |
| 259 | :synopsis: Portable implementation of the simple DBM interface. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 260 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 261 | .. index:: single: databases |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 262 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 263 | .. note:: |
| 264 | |
| 265 | The :mod:`dbm.dumb` module is intended as a last resort fallback for the |
| 266 | :mod:`dbm` module when no more robust module is available. The :mod:`dbm.dumb` |
| 267 | module is not written for speed and is not nearly as heavily used as the other |
| 268 | database modules. |
| 269 | |
| 270 | The :mod:`dbm.dumb` module provides a persistent dictionary-like interface which |
Benjamin Peterson | 9a46cab | 2008-09-08 02:49:30 +0000 | [diff] [blame^] | 271 | is written entirely in Python. Unlike other modules such as :mod:`gdbm` no |
| 272 | external library is required. As with other persistent mappings, the keys and |
| 273 | values must always be strings. |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 274 | |
| 275 | The module defines the following: |
| 276 | |
| 277 | |
| 278 | .. exception:: error |
| 279 | |
| 280 | Raised on dbm.dumb-specific errors, such as I/O errors. :exc:`KeyError` is |
| 281 | raised for general mapping errors like specifying an incorrect key. |
| 282 | |
| 283 | |
| 284 | .. function:: open(filename[, flag[, mode]]) |
| 285 | |
| 286 | Open a dumbdbm database and return a dumbdbm object. The *filename* argument is |
| 287 | the basename of the database file (without any specific extensions). When a |
| 288 | dumbdbm database is created, files with :file:`.dat` and :file:`.dir` extensions |
| 289 | are created. |
| 290 | |
| 291 | The optional *flag* argument is currently ignored; the database is always opened |
| 292 | for update, and will be created if it does not exist. |
| 293 | |
| 294 | The optional *mode* argument is the Unix mode of the file, used only when the |
| 295 | database has to be created. It defaults to octal ``0o666`` (and will be modified |
| 296 | by the prevailing umask). |
| 297 | |
| 298 | In addition to the methods provided by the :class:`collections.MutableMapping` class, |
| 299 | :class:`dumbdbm` objects provide the following method: |
| 300 | |
| 301 | .. method:: dumbdbm.sync() |
| 302 | |
| 303 | Synchronize the on-disk directory and data files. This method is called |
| 304 | by the :meth:`Shelve.sync` method. |