2024-05-08 16:52:39 -03:00
|
|
|
:mod:`!dbm` --- Interfaces to Unix "databases"
|
|
|
|
==============================================
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. module:: dbm
|
2008-05-26 07:29:35 -03:00
|
|
|
:synopsis: Interfaces to various Unix "database" formats.
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/dbm/__init__.py`
|
|
|
|
|
|
|
|
--------------
|
|
|
|
|
2024-02-14 07:14:56 -04:00
|
|
|
:mod:`dbm` is a generic interface to variants of the DBM database:
|
|
|
|
|
|
|
|
* :mod:`dbm.sqlite3`
|
|
|
|
* :mod:`dbm.gnu`
|
|
|
|
* :mod:`dbm.ndbm`
|
|
|
|
|
|
|
|
If none of these modules are installed, the
|
2010-07-29 11:46:07 -03:00
|
|
|
slow-but-simple implementation in module :mod:`dbm.dumb` will be used. There
|
2016-02-26 14:37:12 -04:00
|
|
|
is a `third party interface <https://www.jcea.es/programacion/pybsddb.htm>`_ to
|
2010-07-31 18:41:42 -03:00
|
|
|
the Oracle Berkeley DB.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-03-28 05:13:13 -03:00
|
|
|
.. include:: ../includes/wasm-ios-notavail.rst
|
2008-05-26 07:29:35 -03:00
|
|
|
|
|
|
|
.. exception:: error
|
|
|
|
|
|
|
|
A tuple containing the exceptions that can be raised by each of the supported
|
|
|
|
modules, with a unique exception also named :exc:`dbm.error` as the first
|
|
|
|
item --- the latter is used when :exc:`dbm.error` is raised.
|
|
|
|
|
|
|
|
|
|
|
|
.. function:: whichdb(filename)
|
|
|
|
|
2010-12-04 05:04:04 -04:00
|
|
|
This function attempts to guess which of the several simple database modules
|
2024-02-14 07:14:56 -04:00
|
|
|
available --- :mod:`dbm.sqlite3`, :mod:`dbm.gnu`, :mod:`dbm.ndbm`,
|
|
|
|
or :mod:`dbm.dumb` --- should be used to open a given file.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 06:15:34 -04:00
|
|
|
Return one of the following values:
|
|
|
|
|
|
|
|
* ``None`` if the file can't be opened because it's unreadable or doesn't exist
|
|
|
|
* the empty string (``''``) if the file's format can't be guessed
|
|
|
|
* a string containing the required module name, such as ``'dbm.ndbm'`` or ``'dbm.gnu'``
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 08:36:37 -04:00
|
|
|
.. versionchanged:: 3.11
|
|
|
|
*filename* accepts a :term:`path-like object`.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-23 15:54:44 -04:00
|
|
|
.. Substitutions for the open() flag param docs;
|
|
|
|
all submodules use the same text.
|
|
|
|
|
|
|
|
.. |flag_r| replace::
|
|
|
|
Open existing database for reading only.
|
|
|
|
|
|
|
|
.. |flag_w| replace::
|
|
|
|
Open existing database for reading and writing.
|
|
|
|
|
|
|
|
.. |flag_c| replace::
|
|
|
|
Open database for reading and writing, creating it if it doesn't exist.
|
|
|
|
|
|
|
|
.. |flag_n| replace::
|
|
|
|
Always create a new, empty database, open for reading and writing.
|
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
.. |mode_param_doc| replace::
|
|
|
|
The Unix file access mode of the file (default: octal ``0o666``),
|
|
|
|
used only when the database has to be created.
|
|
|
|
|
2011-04-20 14:11:12 -03:00
|
|
|
.. function:: open(file, flag='r', mode=0o666)
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
Open a database and return the corresponding database object.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
:param file:
|
|
|
|
The database file to open.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
If the database file already exists, the :func:`whichdb` function is used to
|
|
|
|
determine its type and the appropriate module is used; if it does not exist,
|
|
|
|
the first submodule listed above that can be imported is used.
|
|
|
|
:type file: :term:`path-like object`
|
2009-05-25 18:15:01 -03:00
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
:param str flag:
|
2024-01-31 02:59:34 -04:00
|
|
|
* ``'r'`` (default): |flag_r|
|
|
|
|
* ``'w'``: |flag_w|
|
|
|
|
* ``'c'``: |flag_c|
|
|
|
|
* ``'n'``: |flag_n|
|
2024-01-23 15:54:44 -04:00
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
:param int mode:
|
2024-01-31 02:59:34 -04:00
|
|
|
|mode_param_doc|
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 08:36:37 -04:00
|
|
|
.. versionchanged:: 3.11
|
|
|
|
*file* accepts a :term:`path-like object`.
|
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
The object returned by :func:`~dbm.open` supports the same basic functionality as a
|
2024-01-26 06:15:34 -04:00
|
|
|
:class:`dict`; keys and their corresponding values can be stored, retrieved, and
|
|
|
|
deleted, and the :keyword:`in` operator and the :meth:`!keys` method are
|
2024-01-26 11:11:45 -04:00
|
|
|
available, as well as :meth:`!get` and :meth:`!setdefault` methods.
|
2018-12-12 08:46:55 -04:00
|
|
|
|
2024-01-26 06:15:34 -04:00
|
|
|
Key and values are always stored as :class:`bytes`. This means that when
|
2008-11-25 15:19:17 -04:00
|
|
|
strings are used they are implicitly converted to the default encoding before
|
|
|
|
being stored.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
These objects also support being used in a :keyword:`with` statement, which
|
|
|
|
will automatically close them when done.
|
|
|
|
|
2024-01-26 11:11:45 -04:00
|
|
|
.. versionchanged:: 3.2
|
|
|
|
:meth:`!get` and :meth:`!setdefault` methods are now available for all
|
|
|
|
:mod:`dbm` backends.
|
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
.. versionchanged:: 3.4
|
|
|
|
Added native support for the context management protocol to the objects
|
2024-01-26 11:11:45 -04:00
|
|
|
returned by :func:`~dbm.open`.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.8
|
|
|
|
Deleting a key from a read-only database raises a database module specific exception
|
|
|
|
instead of :exc:`KeyError`.
|
2013-11-17 01:59:51 -04:00
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
The following example records some hostnames and a corresponding title, and
|
|
|
|
then prints out the contents of the database::
|
|
|
|
|
|
|
|
import dbm
|
|
|
|
|
|
|
|
# Open database, creating it if necessary.
|
2013-11-17 01:59:51 -04:00
|
|
|
with dbm.open('cache', 'c') as db:
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
# Record some values
|
|
|
|
db[b'hello'] = b'there'
|
|
|
|
db['www.python.org'] = 'Python Website'
|
|
|
|
db['www.cnn.com'] = 'Cable News Network'
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
# Note that the keys are considered bytes now.
|
|
|
|
assert db[b'www.python.org'] == b'Python Website'
|
|
|
|
# Notice how the value is now in bytes.
|
|
|
|
assert db['www.cnn.com'] == b'Cable News Network'
|
2008-11-25 15:19:17 -04:00
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
# Often-used methods of the dict interface work too.
|
|
|
|
print(db.get('python.org', b'not present'))
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
# Storing a non-string key or value will raise an exception (most
|
|
|
|
# likely a TypeError).
|
|
|
|
db['www.yahoo.com'] = 4
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2013-11-17 01:59:51 -04:00
|
|
|
# db is automatically closed when leaving the with statement.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Module :mod:`shelve`
|
|
|
|
Persistence module which stores non-string data.
|
|
|
|
|
|
|
|
|
|
|
|
The individual submodules are described in the following sections.
|
|
|
|
|
2024-02-14 07:14:56 -04:00
|
|
|
:mod:`dbm.sqlite3` --- SQLite backend for dbm
|
|
|
|
---------------------------------------------
|
|
|
|
|
|
|
|
.. module:: dbm.sqlite3
|
|
|
|
:platform: All
|
|
|
|
:synopsis: SQLite backend for dbm
|
|
|
|
|
|
|
|
.. versionadded:: 3.13
|
|
|
|
|
|
|
|
**Source code:** :source:`Lib/dbm/sqlite3.py`
|
|
|
|
|
|
|
|
--------------
|
|
|
|
|
|
|
|
This module uses the standard library :mod:`sqlite3` module to provide an
|
|
|
|
SQLite backend for the :mod:`dbm` module.
|
|
|
|
The files created by :mod:`dbm.sqlite3` can thus be opened by :mod:`sqlite3`,
|
|
|
|
or any other SQLite browser, including the SQLite CLI.
|
|
|
|
|
|
|
|
.. function:: open(filename, /, flag="r", mode=0o666)
|
|
|
|
|
|
|
|
Open an SQLite database.
|
|
|
|
The returned object behaves like a :term:`mapping`,
|
|
|
|
implements a :meth:`!close` method,
|
|
|
|
and supports a "closing" context manager via the :keyword:`with` keyword.
|
|
|
|
|
|
|
|
:param filename:
|
|
|
|
The path to the database to be opened.
|
|
|
|
:type filename: :term:`path-like object`
|
|
|
|
|
|
|
|
:param str flag:
|
|
|
|
|
|
|
|
* ``'r'`` (default): |flag_r|
|
|
|
|
* ``'w'``: |flag_w|
|
|
|
|
* ``'c'``: |flag_c|
|
|
|
|
* ``'n'``: |flag_n|
|
|
|
|
|
|
|
|
:param mode:
|
|
|
|
The Unix file access mode of the file (default: octal ``0o666``),
|
|
|
|
used only when the database has to be created.
|
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 06:05:08 -04:00
|
|
|
:mod:`dbm.gnu` --- GNU database manager
|
|
|
|
---------------------------------------
|
2008-05-26 07:29:35 -03:00
|
|
|
|
|
|
|
.. module:: dbm.gnu
|
|
|
|
:platform: Unix
|
2024-01-26 06:05:08 -04:00
|
|
|
:synopsis: GNU database manager
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2016-06-22 23:46:34 -03:00
|
|
|
**Source code:** :source:`Lib/dbm/gnu.py`
|
|
|
|
|
|
|
|
--------------
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 06:05:08 -04:00
|
|
|
The :mod:`dbm.gnu` module provides an interface to the :abbr:`GDBM (GNU dbm)`
|
|
|
|
library, similar to the :mod:`dbm.ndbm` module, but with additional
|
|
|
|
functionality like crash tolerance.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-02-14 14:03:20 -04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The file formats created by :mod:`dbm.gnu` and :mod:`dbm.ndbm` are incompatible
|
|
|
|
and can not be used interchangeably.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
|
|
|
.. exception:: error
|
|
|
|
|
2008-11-25 15:19:17 -04:00
|
|
|
Raised on :mod:`dbm.gnu`-specific errors, such as I/O errors. :exc:`KeyError` is
|
2008-05-26 07:29:35 -03:00
|
|
|
raised for general mapping errors like specifying an incorrect key.
|
|
|
|
|
|
|
|
|
2024-01-26 06:05:08 -04:00
|
|
|
.. function:: open(filename, flag="r", mode=0o666, /)
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
Open a GDBM database and return a :class:`!gdbm` object.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:param filename:
|
|
|
|
The database file to open.
|
|
|
|
:type filename: :term:`path-like object`
|
2024-01-23 15:54:44 -04:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:param str flag:
|
|
|
|
* ``'r'`` (default): |flag_r|
|
|
|
|
* ``'w'``: |flag_w|
|
|
|
|
* ``'c'``: |flag_c|
|
|
|
|
* ``'n'``: |flag_n|
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
The following additional characters may be appended
|
|
|
|
to control how the database is opened:
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
* ``'f'``: Open the database in fast mode.
|
|
|
|
Writes to the database will not be synchronized.
|
|
|
|
* ``'s'``: Synchronized mode.
|
|
|
|
Changes to the database will be written immediately to the file.
|
|
|
|
* ``'u'``: Do not lock database.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
Not all flags are valid for all versions of GDBM.
|
|
|
|
See the :data:`open_flags` member for a list of supported flag characters.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:param int mode:
|
|
|
|
|mode_param_doc|
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:raises error:
|
|
|
|
If an invalid *flag* argument is passed.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2021-09-10 09:26:16 -03:00
|
|
|
.. versionchanged:: 3.11
|
2024-01-31 02:59:34 -04:00
|
|
|
*filename* accepts a :term:`path-like object`.
|
|
|
|
|
|
|
|
.. data:: open_flags
|
|
|
|
|
|
|
|
A string of characters the *flag* parameter of :meth:`~dbm.gnu.open` supports.
|
|
|
|
|
2024-02-14 11:36:13 -04:00
|
|
|
:class:`!gdbm` objects behave similar to :term:`mappings <mapping>`,
|
|
|
|
but :meth:`!items` and :meth:`!values` methods are not supported.
|
|
|
|
The following methods are also provided:
|
2021-09-10 09:26:16 -03:00
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
.. method:: gdbm.firstkey()
|
|
|
|
|
|
|
|
It's possible to loop over every key in the database using this method and the
|
2024-01-26 06:05:08 -04:00
|
|
|
:meth:`nextkey` method. The traversal is ordered by GDBM's internal
|
2008-05-26 07:29:35 -03:00
|
|
|
hash values, and won't be sorted by the key values. This method returns
|
|
|
|
the starting key.
|
|
|
|
|
|
|
|
.. method:: gdbm.nextkey(key)
|
|
|
|
|
|
|
|
Returns the key that follows *key* in the traversal. The following code prints
|
|
|
|
every key in the database ``db``, without having to create a list in memory that
|
|
|
|
contains them all::
|
|
|
|
|
|
|
|
k = db.firstkey()
|
2021-08-31 10:59:52 -03:00
|
|
|
while k is not None:
|
2008-05-26 07:29:35 -03:00
|
|
|
print(k)
|
|
|
|
k = db.nextkey(k)
|
|
|
|
|
|
|
|
.. method:: gdbm.reorganize()
|
|
|
|
|
|
|
|
If you have carried out a lot of deletions and would like to shrink the space
|
2024-01-26 06:05:08 -04:00
|
|
|
used by the GDBM file, this routine will reorganize the database. :class:`!gdbm`
|
2008-11-25 15:19:17 -04:00
|
|
|
objects will not shorten the length of a database file except by using this
|
2008-05-26 07:29:35 -03:00
|
|
|
reorganization; otherwise, deleted file space will be kept and reused as new
|
|
|
|
(key, value) pairs are added.
|
|
|
|
|
|
|
|
.. method:: gdbm.sync()
|
|
|
|
|
|
|
|
When the database has been opened in fast mode, this method forces any
|
|
|
|
unwritten data to be written to the disk.
|
|
|
|
|
2014-06-25 08:05:31 -03:00
|
|
|
.. method:: gdbm.close()
|
|
|
|
|
2024-01-26 06:05:08 -04:00
|
|
|
Close the GDBM database.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2023-07-23 10:51:12 -03:00
|
|
|
.. method:: gdbm.clear()
|
|
|
|
|
2024-01-26 06:05:08 -04:00
|
|
|
Remove all items from the GDBM database.
|
2023-07-23 10:51:12 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.13
|
|
|
|
|
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
:mod:`dbm.ndbm` --- New Database Manager
|
|
|
|
----------------------------------------
|
2008-05-26 07:29:35 -03:00
|
|
|
|
|
|
|
.. module:: dbm.ndbm
|
2007-08-15 11:28:22 -03:00
|
|
|
:platform: Unix
|
2024-01-26 08:35:56 -04:00
|
|
|
:synopsis: The New Database Manager
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-06-22 23:46:34 -03:00
|
|
|
**Source code:** :source:`Lib/dbm/ndbm.py`
|
|
|
|
|
|
|
|
--------------
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
The :mod:`dbm.ndbm` module provides an interface to the
|
|
|
|
:abbr:`NDBM (New Database Manager)` library.
|
|
|
|
This module can be used with the "classic" NDBM interface or the
|
|
|
|
:abbr:`GDBM (GNU dbm)` compatibility interface.
|
|
|
|
|
2024-02-14 14:03:20 -04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
The file formats created by :mod:`dbm.gnu` and :mod:`dbm.ndbm` are incompatible
|
|
|
|
and can not be used interchangeably.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-12-23 08:33:34 -04:00
|
|
|
.. warning::
|
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
The NDBM library shipped as part of macOS has an undocumented limitation on the
|
2023-12-23 08:33:34 -04:00
|
|
|
size of values, which can result in corrupted database files
|
|
|
|
when storing values larger than this limit. Reading such corrupted files can
|
|
|
|
result in a hard crash (segmentation fault).
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. exception:: error
|
|
|
|
|
2008-11-25 15:19:17 -04:00
|
|
|
Raised on :mod:`dbm.ndbm`-specific errors, such as I/O errors. :exc:`KeyError` is raised
|
2008-05-26 07:29:35 -03:00
|
|
|
for general mapping errors like specifying an incorrect key.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. data:: library
|
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
Name of the NDBM implementation library used.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
.. function:: open(filename, flag="r", mode=0o666, /)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
Open an NDBM database and return an :class:`!ndbm` object.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:param filename:
|
|
|
|
The basename of the database file
|
|
|
|
(without the :file:`.dir` or :file:`.pag` extensions).
|
|
|
|
:type filename: :term:`path-like object`
|
2024-01-23 15:54:44 -04:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:param str flag:
|
|
|
|
* ``'r'`` (default): |flag_r|
|
|
|
|
* ``'w'``: |flag_w|
|
|
|
|
* ``'c'``: |flag_c|
|
|
|
|
* ``'n'``: |flag_n|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2024-01-31 02:59:34 -04:00
|
|
|
:param int mode:
|
|
|
|
|mode_param_doc|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2024-02-14 11:36:13 -04:00
|
|
|
:class:`!ndbm` objects behave similar to :term:`mappings <mapping>`,
|
|
|
|
but :meth:`!items` and :meth:`!values` methods are not supported.
|
|
|
|
The following methods are also provided:
|
2014-06-25 08:05:31 -03:00
|
|
|
|
2021-09-10 09:26:16 -03:00
|
|
|
.. versionchanged:: 3.11
|
|
|
|
Accepts :term:`path-like object` for filename.
|
|
|
|
|
2014-06-25 08:05:31 -03:00
|
|
|
.. method:: ndbm.close()
|
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
Close the NDBM database.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2023-07-23 11:26:23 -03:00
|
|
|
.. method:: ndbm.clear()
|
|
|
|
|
2024-01-26 08:35:56 -04:00
|
|
|
Remove all items from the NDBM database.
|
2023-07-23 11:26:23 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.13
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
:mod:`dbm.dumb` --- Portable DBM implementation
|
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
.. module:: dbm.dumb
|
|
|
|
:synopsis: Portable implementation of the simple DBM interface.
|
|
|
|
|
2016-06-22 23:46:34 -03:00
|
|
|
**Source code:** :source:`Lib/dbm/dumb.py`
|
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
.. index:: single: databases
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The :mod:`dbm.dumb` module is intended as a last resort fallback for the
|
2008-11-25 15:19:17 -04:00
|
|
|
:mod:`dbm` module when a more robust module is not available. The :mod:`dbm.dumb`
|
2008-05-26 07:29:35 -03:00
|
|
|
module is not written for speed and is not nearly as heavily used as the other
|
|
|
|
database modules.
|
|
|
|
|
2016-06-22 23:46:34 -03:00
|
|
|
--------------
|
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
The :mod:`dbm.dumb` module provides a persistent :class:`dict`-like
|
|
|
|
interface which is written entirely in Python.
|
|
|
|
Unlike other :mod:`dbm` backends, such as :mod:`dbm.gnu`, no
|
|
|
|
external library is required.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
The :mod:`!dbm.dumb` module defines the following:
|
2008-05-26 07:29:35 -03:00
|
|
|
|
|
|
|
.. exception:: error
|
|
|
|
|
2008-11-25 15:19:17 -04:00
|
|
|
Raised on :mod:`dbm.dumb`-specific errors, such as I/O errors. :exc:`KeyError` is
|
2008-05-26 07:29:35 -03:00
|
|
|
raised for general mapping errors like specifying an incorrect key.
|
|
|
|
|
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
.. function:: open(filename, flag="c", mode=0o666)
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
Open a :mod:`!dbm.dumb` database.
|
|
|
|
The returned database object behaves similar to a :term:`mapping`,
|
|
|
|
in addition to providing :meth:`~dumbdbm.sync` and :meth:`~dumbdbm.close`
|
|
|
|
methods.
|
2008-05-26 07:29:35 -03:00
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
:param filename:
|
|
|
|
The basename of the database file (without extensions).
|
|
|
|
A new database creates the following files:
|
2018-02-05 16:47:31 -04:00
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
- :file:`{filename}.dat`
|
|
|
|
- :file:`{filename}.dir`
|
|
|
|
:type database: :term:`path-like object`
|
2024-01-23 15:54:44 -04:00
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
:param str flag:
|
2024-01-31 02:59:34 -04:00
|
|
|
* ``'r'``: |flag_r|
|
|
|
|
* ``'w'``: |flag_w|
|
|
|
|
* ``'c'`` (default): |flag_c|
|
|
|
|
* ``'n'``: |flag_n|
|
2024-01-26 14:12:48 -04:00
|
|
|
|
|
|
|
:param int mode:
|
2024-01-31 02:59:34 -04:00
|
|
|
|mode_param_doc|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2018-03-09 19:58:40 -04:00
|
|
|
.. warning::
|
|
|
|
It is possible to crash the Python interpreter when loading a database
|
|
|
|
with a sufficiently large/complex entry due to stack depth limitations in
|
|
|
|
Python's AST compiler.
|
|
|
|
|
2014-06-10 15:16:00 -03:00
|
|
|
.. versionchanged:: 3.5
|
2024-01-31 02:59:34 -04:00
|
|
|
:func:`~dbm.dumb.open` always creates a new database when *flag* is ``'n'``.
|
2014-06-10 15:16:00 -03:00
|
|
|
|
2018-02-05 16:47:31 -04:00
|
|
|
.. versionchanged:: 3.8
|
2024-01-26 14:12:48 -04:00
|
|
|
A database opened read-only if *flag* is ``'r'``.
|
|
|
|
A database is not created if it does not exist if *flag* is ``'r'`` or ``'w'``.
|
2016-07-06 06:21:58 -03:00
|
|
|
|
2021-09-10 09:26:16 -03:00
|
|
|
.. versionchanged:: 3.11
|
2024-01-26 14:12:48 -04:00
|
|
|
*filename* accepts a :term:`path-like object`.
|
2021-09-10 09:26:16 -03:00
|
|
|
|
2013-10-13 17:09:14 -03:00
|
|
|
In addition to the methods provided by the
|
2024-01-26 14:12:48 -04:00
|
|
|
:class:`collections.abc.MutableMapping` class,
|
|
|
|
the following methods are provided:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
.. method:: dumbdbm.sync()
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2008-05-26 07:29:35 -03:00
|
|
|
Synchronize the on-disk directory and data files. This method is called
|
|
|
|
by the :meth:`Shelve.sync` method.
|
2014-06-25 08:05:31 -03:00
|
|
|
|
|
|
|
.. method:: dumbdbm.close()
|
|
|
|
|
2024-01-26 14:12:48 -04:00
|
|
|
Close the database.
|