Home | History | Annotate | Download | only in library
      1 :mod:`dbm` --- Interfaces to Unix "databases"
      2 =============================================
      3 
      4 .. module:: dbm
      5    :synopsis: Interfaces to various Unix "database" formats.
      6 
      7 **Source code:** :source:`Lib/dbm/__init__.py`
      8 
      9 --------------
     10 
     11 :mod:`dbm` is a generic interface to variants of the DBM database ---
     12 :mod:`dbm.gnu` or :mod:`dbm.ndbm`.  If none of these modules is installed, the
     13 slow-but-simple implementation in module :mod:`dbm.dumb` will be used.  There
     14 is a `third party interface <https://www.jcea.es/programacion/pybsddb.htm>`_ to
     15 the Oracle Berkeley DB.
     16 
     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 
     27    This function attempts to guess which of the several simple database modules
     28    available --- :mod:`dbm.gnu`, :mod:`dbm.ndbm` or :mod:`dbm.dumb` --- should
     29    be used to open a given file.
     30 
     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 
     37 .. function:: open(file, flag='r', mode=0o666)
     38 
     39    Open the database file *file* and return a corresponding object.
     40 
     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 
     45    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    +---------+-------------------------------------------+
     62 
     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 
     68 The object returned by :func:`.open` supports the same basic functionality as
     69 dictionaries; keys and their corresponding values can be stored, retrieved, and
     70 deleted, and the :keyword:`in` operator and the :meth:`keys` method are
     71 available, 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 
     76 Key and values are always stored as bytes. This means that when
     77 strings are used they are implicitly converted to the default encoding before
     78 being stored.
     79 
     80 These objects also support being used in a :keyword:`with` statement, which
     81 will 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 
     87 The following example records some hostnames and a corresponding title,  and
     88 then prints out the contents of the database::
     89 
     90    import dbm
     91 
     92    # Open database, creating it if necessary.
     93    with dbm.open('cache', 'c') as db:
     94 
     95        # Record some values
     96        db[b'hello'] = b'there'
     97        db['www.python.org'] = 'Python Website'
     98        db['www.cnn.com'] = 'Cable News Network'
     99 
    100        # 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'
    104 
    105        # Often-used methods of the dict interface work too.
    106        print(db.get('python.org', b'not present'))
    107 
    108        # Storing a non-string key or value will raise an exception (most
    109        # likely a TypeError).
    110        db['www.yahoo.com'] = 4
    111 
    112    # db is automatically closed when leaving the with statement.
    113 
    114 
    115 .. seealso::
    116 
    117    Module :mod:`shelve`
    118       Persistence module which stores non-string data.
    119 
    120 
    121 The individual submodules are described in the following sections.
    122 
    123 
    124 :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 
    131 **Source code:** :source:`Lib/dbm/gnu.py`
    132 
    133 --------------
    134 
    135 This 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
    137 file formats created by :mod:`dbm.gnu` and :mod:`dbm.ndbm` are incompatible.
    138 
    139 The :mod:`dbm.gnu` module provides an interface to the GNU DBM library.
    140 ``dbm.gnu.gdbm`` objects behave like mappings (dictionaries), except that keys and
    141 values are always converted to bytes before storing.  Printing a ``gdbm``
    142 object doesn't print the
    143 keys and values, and the :meth:`items` and :meth:`values` methods are not
    144 supported.
    145 
    146 .. exception:: error
    147 
    148    Raised on :mod:`dbm.gnu`-specific errors, such as I/O errors. :exc:`KeyError` is
    149    raised for general mapping errors like specifying an incorrect key.
    150 
    151 
    152 .. function:: open(filename[, flag[, mode]])
    153 
    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
    196    database has to be created.  It defaults to octal ``0o666``.
    197 
    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``
    223       objects will not shorten the length of a database file except by using this
    224       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 
    232    .. method:: gdbm.close()
    233 
    234       Close the ``gdbm`` database.
    235 
    236 :mod:`dbm.ndbm` --- Interface based on ndbm
    237 -------------------------------------------
    238 
    239 .. module:: dbm.ndbm
    240    :platform: Unix
    241    :synopsis: The standard "database" interface, based on ndbm.
    242 
    243 **Source code:** :source:`Lib/dbm/ndbm.py`
    244 
    245 --------------
    246 
    247 The :mod:`dbm.ndbm` module provides an interface to the Unix "(n)dbm" library.
    248 Dbm objects behave like mappings (dictionaries), except that keys and values are
    249 always stored as bytes. Printing a ``dbm`` object doesn't print the keys and
    250 values, and the :meth:`items` and :meth:`values` methods are not supported.
    251 
    252 This module can be used with the "classic" ndbm interface or the GNU GDBM
    253 compatibility interface. On Unix, the :program:`configure` script will attempt
    254 to locate the appropriate header file to simplify building this module.
    255 
    256 .. exception:: error
    257 
    258    Raised on :mod:`dbm.ndbm`-specific errors, such as I/O errors. :exc:`KeyError` is raised
    259    for general mapping errors like specifying an incorrect key.
    260 
    261 
    262 .. data:: library
    263 
    264    Name of the ``ndbm`` implementation library used.
    265 
    266 
    267 .. function:: open(filename[, flag[, mode]])
    268 
    269    Open a dbm database and return a ``ndbm`` object.  The *filename* argument is the
    270    name of the database file (without the :file:`.dir` or :file:`.pag` extensions).
    271 
    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
    291    database has to be created.  It defaults to octal ``0o666`` (and will be
    292    modified by the prevailing umask).
    293 
    294    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.
    300 
    301 
    302 :mod:`dbm.dumb` --- Portable DBM implementation
    303 -----------------------------------------------
    304 
    305 .. module:: dbm.dumb
    306    :synopsis: Portable implementation of the simple DBM interface.
    307 
    308 **Source code:** :source:`Lib/dbm/dumb.py`
    309 
    310 .. index:: single: databases
    311 
    312 .. note::
    313 
    314    The :mod:`dbm.dumb` module is intended as a last resort fallback for the
    315    :mod:`dbm` module when a more robust module is not available. The :mod:`dbm.dumb`
    316    module is not written for speed and is not nearly as heavily used as the other
    317    database modules.
    318 
    319 --------------
    320 
    321 The :mod:`dbm.dumb` module provides a persistent dictionary-like interface which
    322 is written entirely in Python.  Unlike other modules such as :mod:`dbm.gnu` no
    323 external library is required.  As with other persistent mappings, the keys and
    324 values are always stored as bytes.
    325 
    326 The module defines the following:
    327 
    328 
    329 .. exception:: error
    330 
    331    Raised on :mod:`dbm.dumb`-specific errors, such as I/O errors.  :exc:`KeyError` is
    332    raised for general mapping errors like specifying an incorrect key.
    333 
    334 
    335 .. function:: open(filename[, flag[, mode]])
    336 
    337    Open a ``dumbdbm`` database and return a dumbdbm object.  The *filename* argument is
    338    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 
    342    The optional *flag* argument supports only the semantics of ``'c'``
    343    and ``'n'`` values.  Other values will default to database being always
    344    opened for update, and will be created if it does not exist.
    345 
    346    The optional *mode* argument is the Unix mode of the file, used only when the
    347    database has to be created.  It defaults to octal ``0o666`` (and will be modified
    348    by the prevailing umask).
    349 
    350    .. warning::
    351       It is possible to crash the Python interpreter when loading a database
    352       with a sufficiently large/complex entry due to stack depth limitations in
    353       Python's AST compiler.
    354 
    355    .. versionchanged:: 3.5
    356       :func:`.open` always creates a new database when the flag has the value
    357       ``'n'``.
    358 
    359    .. deprecated-removed:: 3.6 3.8
    360       Creating database in ``'r'`` and ``'w'`` modes.  Modifying database in
    361       ``'r'`` mode.
    362 
    363    In addition to the methods provided by the
    364    :class:`collections.abc.MutableMapping` class, :class:`dumbdbm` objects
    365    provide the following methods:
    366 
    367    .. method:: dumbdbm.sync()
    368 
    369       Synchronize the on-disk directory and data files.  This method is called
    370       by the :meth:`Shelve.sync` method.
    371 
    372    .. method:: dumbdbm.close()
    373 
    374       Close the ``dumbdbm`` database.
    375 
    376