| 
									
										
										
										
											2024-05-08 15:34:40 -04:00
										 |  |  | :mod:`!dbm` --- Interfaces to Unix "databases"
 | 
					
						
							|  |  |  | ==============================================
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: dbm
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    :synopsis: Interfaces to various Unix "database" formats.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  | **Source code:** :source:`Lib/dbm/__init__.py`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 12:14:56 +01: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 14:46:07 +00:00
										 |  |  | slow-but-simple implementation in module :mod:`dbm.dumb` will be used.  There
 | 
					
						
							| 
									
										
										
										
											2016-02-26 19:37:12 +01:00
										 |  |  | is a `third party interface <https://www.jcea.es/programacion/pybsddb.htm>`_ to
 | 
					
						
							| 
									
										
										
										
											2010-07-31 21:41:42 +00:00
										 |  |  | the Oracle Berkeley DB.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00: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 09:04:04 +00:00
										 |  |  |    This function attempts to guess which of the several simple database modules
 | 
					
						
							| 
									
										
										
										
											2024-02-14 12:14:56 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:15:34 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:36:37 +01:00
										 |  |  |    .. versionchanged:: 3.11
 | 
					
						
							|  |  |  |       *filename* accepts a :term:`path-like object`.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-23 20:54:44 +01: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 07:59:34 +01: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 19:11:12 +02:00
										 |  |  | .. function:: open(file, flag='r', mode=0o666)
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01:00
										 |  |  |    Open a database and return the corresponding database object.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01:00
										 |  |  |    :param file:
 | 
					
						
							|  |  |  |       The database file to open.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01: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 21:15:01 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01:00
										 |  |  |    :param str flag:
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |       * ``'r'`` (default): |flag_r|
 | 
					
						
							|  |  |  |       * ``'w'``: |flag_w|
 | 
					
						
							|  |  |  |       * ``'c'``: |flag_c|
 | 
					
						
							|  |  |  |       * ``'n'``: |flag_n|
 | 
					
						
							| 
									
										
										
										
											2024-01-23 20:54:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01:00
										 |  |  |    :param int mode:
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |       |mode_param_doc|
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:36:37 +01:00
										 |  |  |    .. versionchanged:: 3.11
 | 
					
						
							|  |  |  |       *file* accepts a :term:`path-like object`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01:00
										 |  |  | The object returned by :func:`~dbm.open` supports the same basic functionality as a
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:15:34 +01: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 16:11:45 +01:00
										 |  |  | available, as well as :meth:`!get` and :meth:`!setdefault` methods.
 | 
					
						
							| 
									
										
										
										
											2018-12-12 20:46:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:15:34 +01:00
										 |  |  | Key and values are always stored as :class:`bytes`. This means that when
 | 
					
						
							| 
									
										
										
										
											2008-11-25 19:19:17 +00:00
										 |  |  | strings are used they are implicitly converted to the default encoding before
 | 
					
						
							|  |  |  | being stored.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10:00
										 |  |  | These objects also support being used in a :keyword:`with` statement, which
 | 
					
						
							|  |  |  | will automatically close them when done.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01:00
										 |  |  | .. versionchanged:: 3.2
 | 
					
						
							|  |  |  |    :meth:`!get` and :meth:`!setdefault` methods are now available for all
 | 
					
						
							|  |  |  |    :mod:`dbm` backends.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10:00
										 |  |  | .. versionchanged:: 3.4
 | 
					
						
							|  |  |  |    Added native support for the context management protocol to the objects
 | 
					
						
							| 
									
										
										
										
											2024-01-26 16:11:45 +01: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 15:59:51 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00: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 15:59:51 +10:00
										 |  |  |    with dbm.open('cache', 'c') as db:
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10: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 19:19:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10:00
										 |  |  |        # Often-used methods of the dict interface work too.
 | 
					
						
							|  |  |  |        print(db.get('python.org', b'not present'))
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10:00
										 |  |  |        # Storing a non-string key or value will raise an exception (most
 | 
					
						
							|  |  |  |        # likely a TypeError).
 | 
					
						
							|  |  |  |        db['www.yahoo.com'] = 4
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 15:59:51 +10:00
										 |  |  |    # db is automatically closed when leaving the with statement.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Module :mod:`shelve`
 | 
					
						
							|  |  |  |       Persistence module which stores non-string data.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The individual submodules are described in the following sections.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 12:14:56 +01: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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 00:09:53 +01:00
										 |  |  | .. include:: ../includes/wasm-notavail.rst
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 12:14:56 +01:00
										 |  |  | .. 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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01:00
										 |  |  | :mod:`dbm.gnu` --- GNU database manager
 | 
					
						
							|  |  |  | ---------------------------------------
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: dbm.gnu
 | 
					
						
							|  |  |  |    :platform: Unix
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01:00
										 |  |  |    :synopsis: GNU database manager
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 22:46:34 -04:00
										 |  |  | **Source code:** :source:`Lib/dbm/gnu.py`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 19:03:20 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 00:09:53 +01:00
										 |  |  | .. include:: ../includes/wasm-mobile-notavail.rst
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | .. exception:: error
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-25 19:19:17 +00:00
										 |  |  |    Raised on :mod:`dbm.gnu`-specific errors, such as I/O errors. :exc:`KeyError` is
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    raised for general mapping errors like specifying an incorrect key.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01:00
										 |  |  | .. function:: open(filename, flag="r", mode=0o666, /)
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    Open a GDBM database and return a :class:`!gdbm` object.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    :param filename:
 | 
					
						
							|  |  |  |       The database file to open.
 | 
					
						
							|  |  |  |    :type filename: :term:`path-like object`
 | 
					
						
							| 
									
										
										
										
											2024-01-23 20:54:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    :param str flag:
 | 
					
						
							|  |  |  |       * ``'r'`` (default): |flag_r|
 | 
					
						
							|  |  |  |       * ``'w'``: |flag_w|
 | 
					
						
							|  |  |  |       * ``'c'``: |flag_c|
 | 
					
						
							|  |  |  |       * ``'n'``: |flag_n|
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |       The following additional characters may be appended
 | 
					
						
							|  |  |  |       to control how the database is opened:
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    :param int mode:
 | 
					
						
							|  |  |  |       |mode_param_doc|
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    :raises error:
 | 
					
						
							|  |  |  |       If an invalid *flag* argument is passed.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 14:26:16 +02:00
										 |  |  |    .. versionchanged:: 3.11
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01: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 16:36:13 +01: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 14:26:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    .. method:: gdbm.firstkey()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       It's possible to loop over every key in the database using this method  and the
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01:00
										 |  |  |       :meth:`nextkey` method.  The traversal is ordered by GDBM's internal
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00: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 16:59:52 +03:00
										 |  |  |          while k is not None:
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00: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 11:05:08 +01:00
										 |  |  |       used by the GDBM file, this routine will reorganize the database.  :class:`!gdbm`
 | 
					
						
							| 
									
										
										
										
											2008-11-25 19:19:17 +00:00
										 |  |  |       objects will not shorten the length of a database file except by using this
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00: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 13:05:31 +02:00
										 |  |  |    .. method:: gdbm.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01:00
										 |  |  |       Close the GDBM database.
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-23 22:51:12 +09:00
										 |  |  |    .. method:: gdbm.clear()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 11:05:08 +01:00
										 |  |  |       Remove all items from the GDBM database.
 | 
					
						
							| 
									
										
										
										
											2023-07-23 22:51:12 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |       .. versionadded:: 3.13
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  | :mod:`dbm.ndbm` --- New Database Manager
 | 
					
						
							|  |  |  | ----------------------------------------
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: dbm.ndbm
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  |    :platform: Unix
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  |    :synopsis: The New Database Manager
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 22:46:34 -04:00
										 |  |  | **Source code:** :source:`Lib/dbm/ndbm.py`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01: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 19:03:20 +01: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 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-23 13:33:34 +01:00
										 |  |  | .. warning::
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  |    The NDBM library shipped as part of macOS has an undocumented limitation on the
 | 
					
						
							| 
									
										
										
										
											2023-12-23 13:33:34 +01: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).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-24 00:09:53 +01:00
										 |  |  | .. include:: ../includes/wasm-mobile-notavail.rst
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | .. exception:: error
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-25 19:19:17 +00:00
										 |  |  |    Raised on :mod:`dbm.ndbm`-specific errors, such as I/O errors. :exc:`KeyError` is raised
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    for general mapping errors like specifying an incorrect key.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. data:: library
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  |    Name of the NDBM implementation library used.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  | .. function:: open(filename, flag="r", mode=0o666, /)
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  |    Open an NDBM database and return an :class:`!ndbm` object.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01: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 20:54:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    :param str flag:
 | 
					
						
							|  |  |  |       * ``'r'`` (default): |flag_r|
 | 
					
						
							|  |  |  |       * ``'w'``: |flag_w|
 | 
					
						
							|  |  |  |       * ``'c'``: |flag_c|
 | 
					
						
							|  |  |  |       * ``'n'``: |flag_n|
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |    :param int mode:
 | 
					
						
							|  |  |  |       |mode_param_doc|
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-14 16:36:13 +01: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 13:05:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 14:26:16 +02:00
										 |  |  |    .. versionchanged:: 3.11
 | 
					
						
							|  |  |  |       Accepts :term:`path-like object` for filename.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-25 13:05:31 +02:00
										 |  |  |    .. method:: ndbm.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  |       Close the NDBM database.
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-23 23:26:23 +09:00
										 |  |  |    .. method:: ndbm.clear()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:35:56 +01:00
										 |  |  |       Remove all items from the NDBM database.
 | 
					
						
							| 
									
										
										
										
											2023-07-23 23:26:23 +09:00
										 |  |  | 
 | 
					
						
							|  |  |  |       .. versionadded:: 3.13
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | :mod:`dbm.dumb` --- Portable DBM implementation
 | 
					
						
							|  |  |  | -----------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. module:: dbm.dumb
 | 
					
						
							|  |  |  |    :synopsis: Portable implementation of the simple DBM interface.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 22:46:34 -04:00
										 |  |  | **Source code:** :source:`Lib/dbm/dumb.py`
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | .. index:: single: databases
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. note::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The :mod:`dbm.dumb` module is intended as a last resort fallback for the
 | 
					
						
							| 
									
										
										
										
											2008-11-25 19:19:17 +00:00
										 |  |  |    :mod:`dbm` module when a more robust module is not available. The :mod:`dbm.dumb`
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    module is not written for speed and is not nearly as heavily used as the other
 | 
					
						
							|  |  |  |    database modules.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-22 22:46:34 -04:00
										 |  |  | --------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  | The :mod:`!dbm.dumb` module defines the following:
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. exception:: error
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-25 19:19:17 +00:00
										 |  |  |    Raised on :mod:`dbm.dumb`-specific errors, such as I/O errors.  :exc:`KeyError` is
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    raised for general mapping errors like specifying an incorrect key.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  | .. function:: open(filename, flag="c", mode=0o666)
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01: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 10:29:35 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  |    :param filename:
 | 
					
						
							|  |  |  |       The basename of the database file (without extensions).
 | 
					
						
							|  |  |  |       A new database creates the following files:
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:47:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  |       - :file:`{filename}.dat`
 | 
					
						
							|  |  |  |       - :file:`{filename}.dir`
 | 
					
						
							|  |  |  |    :type database: :term:`path-like object`
 | 
					
						
							| 
									
										
										
										
											2024-01-23 20:54:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  |    :param str flag:
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |       * ``'r'``: |flag_r|
 | 
					
						
							|  |  |  |       * ``'w'``: |flag_w|
 | 
					
						
							|  |  |  |       * ``'c'`` (default): |flag_c|
 | 
					
						
							|  |  |  |       * ``'n'``: |flag_n|
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |    :param int mode:
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |       |mode_param_doc|
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-09 15:58:40 -08: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 21:16:00 +03:00
										 |  |  |    .. versionchanged:: 3.5
 | 
					
						
							| 
									
										
										
										
											2024-01-31 07:59:34 +01:00
										 |  |  |       :func:`~dbm.dumb.open` always creates a new database when *flag* is ``'n'``.
 | 
					
						
							| 
									
										
										
										
											2014-06-10 21:16:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 22:47:31 +02:00
										 |  |  |    .. versionchanged:: 3.8
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01: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 12:21:58 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 14:26:16 +02:00
										 |  |  |    .. versionchanged:: 3.11
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  |       *filename* accepts a :term:`path-like object`.
 | 
					
						
							| 
									
										
										
										
											2021-09-10 14:26:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-13 23:09:14 +03:00
										 |  |  |    In addition to the methods provided by the
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  |    :class:`collections.abc.MutableMapping` class,
 | 
					
						
							|  |  |  |    the following methods are provided:
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |    .. method:: dumbdbm.sync()
 | 
					
						
							| 
									
										
										
										
											2007-08-15 14:28:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 10:29:35 +00:00
										 |  |  |       Synchronize the on-disk directory and data files.  This method is called
 | 
					
						
							| 
									
										
										
										
											2025-02-11 20:06:32 +09:00
										 |  |  |       by the :meth:`shelve.Shelf.sync` method.
 | 
					
						
							| 
									
										
										
										
											2014-06-25 13:05:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    .. method:: dumbdbm.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 19:12:48 +01:00
										 |  |  |       Close the database.
 |