diff options
Diffstat (limited to 'devdocs/python~3.12/library%2Fdbm.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fdbm.html | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fdbm.html b/devdocs/python~3.12/library%2Fdbm.html new file mode 100644 index 00000000..1b8fb6fd --- /dev/null +++ b/devdocs/python~3.12/library%2Fdbm.html @@ -0,0 +1,115 @@ + <span id="dbm-interfaces-to-unix-databases"></span><h1>dbm — Interfaces to Unix “databases”</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/__init__.py">Lib/dbm/__init__.py</a></p> <p><a class="reference internal" href="#module-dbm" title='dbm: Interfaces to various Unix "database" formats.'><code>dbm</code></a> is a generic interface to variants of the DBM database — <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code>dbm.gnu</code></a> or <a class="reference internal" href="#module-dbm.ndbm" title='dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)'><code>dbm.ndbm</code></a>. If none of these modules is installed, the slow-but-simple implementation in module <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code>dbm.dumb</code></a> will be used. There is a <a class="reference external" href="https://www.jcea.es/programacion/pybsddb.htm">third party interface</a> to the Oracle Berkeley DB.</p> <dl class="py exception"> <dt class="sig sig-object py" id="dbm.error"> +<code>exception dbm.error</code> </dt> <dd> +<p>A tuple containing the exceptions that can be raised by each of the supported modules, with a unique exception also named <a class="reference internal" href="#dbm.error" title="dbm.error"><code>dbm.error</code></a> as the first item — the latter is used when <a class="reference internal" href="#dbm.error" title="dbm.error"><code>dbm.error</code></a> is raised.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="dbm.whichdb"> +<code>dbm.whichdb(filename)</code> </dt> <dd> +<p>This function attempts to guess which of the several simple database modules available — <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code>dbm.gnu</code></a>, <a class="reference internal" href="#module-dbm.ndbm" title='dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)'><code>dbm.ndbm</code></a> or <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code>dbm.dumb</code></a> — should be used to open a given file.</p> <p>Returns one of the following values: <code>None</code> if the file can’t be opened because it’s unreadable or doesn’t exist; the empty string (<code>''</code>) if the file’s format can’t be guessed; or a string containing the required module name, such as <code>'dbm.ndbm'</code> or <code>'dbm.gnu'</code>.</p> </dd> +</dl> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for filename.</p> </div> <dl class="py function"> <dt class="sig sig-object py" id="dbm.open"> +<code>dbm.open(file, flag='r', mode=0o666)</code> </dt> <dd> +<p>Open the database file <em>file</em> and return a corresponding object.</p> <p>If the database file already exists, the <a class="reference internal" href="#dbm.whichdb" title="dbm.whichdb"><code>whichdb()</code></a> function is used to determine its type and the appropriate module is used; if it does not exist, the first module listed above that can be imported is used.</p> <p>The optional <em>flag</em> argument can be:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Value</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr> +<td><p><code>'r'</code></p></td> <td><p>Open existing database for reading only (default)</p></td> </tr> <tr> +<td><p><code>'w'</code></p></td> <td><p>Open existing database for reading and writing</p></td> </tr> <tr> +<td><p><code>'c'</code></p></td> <td><p>Open database for reading and writing, creating it if it doesn’t exist</p></td> </tr> <tr> +<td><p><code>'n'</code></p></td> <td><p>Always create a new, empty database, open for reading and writing</p></td> </tr> </table> <p>The optional <em>mode</em> argument is the Unix mode of the file, used only when the database has to be created. It defaults to octal <code>0o666</code> (and will be modified by the prevailing umask).</p> </dd> +</dl> <p>The object returned by <a class="reference internal" href="#dbm.open" title="dbm.open"><code>open()</code></a> supports the same basic functionality as dictionaries; keys and their corresponding values can be stored, retrieved, and deleted, and the <a class="reference internal" href="../reference/expressions#in"><code>in</code></a> operator and the <code>keys()</code> method are available, as well as <code>get()</code> and <code>setdefault()</code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span><code>get()</code> and <code>setdefault()</code> are now available in all database modules.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Deleting a key from a read-only database raises database module specific error instead of <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for file.</p> </div> <p>Key and values are always stored as bytes. This means that when strings are used they are implicitly converted to the default encoding before being stored.</p> <p>These objects also support being used in a <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement, which will automatically close them when done.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>Added native support for the context management protocol to the objects returned by <a class="reference internal" href="#dbm.open" title="dbm.open"><code>open()</code></a>.</p> </div> <p>The following example records some hostnames and a corresponding title, and then prints out the contents of the database:</p> <pre data-language="python">import dbm + +# Open database, creating it if necessary. +with dbm.open('cache', 'c') as db: + + # Record some values + db[b'hello'] = b'there' + db['www.python.org'] = 'Python Website' + db['www.cnn.com'] = 'Cable News Network' + + # 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' + + # Often-used methods of the dict interface work too. + print(db.get('python.org', b'not present')) + + # Storing a non-string key or value will raise an exception (most + # likely a TypeError). + db['www.yahoo.com'] = 4 + +# db is automatically closed when leaving the with statement. +</pre> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt> +<code>Module</code> <a class="reference internal" href="shelve#module-shelve" title="shelve: Python object persistence."><code>shelve</code></a> +</dt> +<dd> +<p>Persistence module which stores non-string data.</p> </dd> </dl> </div> <p>The individual submodules are described in the following sections.</p> <section id="module-dbm.gnu"> <span id="dbm-gnu-gnu-s-reinterpretation-of-dbm"></span><h2>dbm.gnu — GNU’s reinterpretation of dbm</h2> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/gnu.py">Lib/dbm/gnu.py</a></p> <p>This module is quite similar to the <a class="reference internal" href="#module-dbm" title='dbm: Interfaces to various Unix "database" formats.'><code>dbm</code></a> module, but uses the GNU library <code>gdbm</code> instead to provide some additional functionality. Please note that the file formats created by <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code>dbm.gnu</code></a> and <a class="reference internal" href="#module-dbm.ndbm" title='dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)'><code>dbm.ndbm</code></a> are incompatible.</p> <p>The <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code>dbm.gnu</code></a> module provides an interface to the GNU DBM library. <code>dbm.gnu.gdbm</code> objects behave like mappings (dictionaries), except that keys and values are always converted to bytes before storing. Printing a <code>gdbm</code> object doesn’t print the keys and values, and the <code>items()</code> and <code>values()</code> methods are not supported.</p> <dl class="py exception"> <dt class="sig sig-object py" id="dbm.gnu.error"> +<code>exception dbm.gnu.error</code> </dt> <dd> +<p>Raised on <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code>dbm.gnu</code></a>-specific errors, such as I/O errors. <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a> is raised for general mapping errors like specifying an incorrect key.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="dbm.gnu.open"> +<code>dbm.gnu.open(filename[, flag[, mode]])</code> </dt> <dd> +<p>Open a <code>gdbm</code> database and return a <code>gdbm</code> object. The <em>filename</em> argument is the name of the database file.</p> <p>The optional <em>flag</em> argument can be:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Value</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr> +<td><p><code>'r'</code></p></td> <td><p>Open existing database for reading only (default)</p></td> </tr> <tr> +<td><p><code>'w'</code></p></td> <td><p>Open existing database for reading and writing</p></td> </tr> <tr> +<td><p><code>'c'</code></p></td> <td><p>Open database for reading and writing, creating it if it doesn’t exist</p></td> </tr> <tr> +<td><p><code>'n'</code></p></td> <td><p>Always create a new, empty database, open for reading and writing</p></td> </tr> </table> <p>The following additional characters may be appended to the flag to control how the database is opened:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Value</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr> +<td><p><code>'f'</code></p></td> <td><p>Open the database in fast mode. Writes to the database will not be synchronized.</p></td> </tr> <tr> +<td><p><code>'s'</code></p></td> <td><p>Synchronized mode. This will cause changes to the database to be immediately written to the file.</p></td> </tr> <tr> +<td><p><code>'u'</code></p></td> <td><p>Do not lock database.</p></td> </tr> </table> <p>Not all flags are valid for all versions of <code>gdbm</code>. The module constant <code>open_flags</code> is a string of supported flag characters. The exception <a class="reference internal" href="#dbm.gnu.error" title="dbm.gnu.error"><code>error</code></a> is raised if an invalid flag is specified.</p> <p>The optional <em>mode</em> argument is the Unix mode of the file, used only when the database has to be created. It defaults to octal <code>0o666</code>.</p> <p>In addition to the dictionary-like methods, <code>gdbm</code> objects have the following methods:</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for filename.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="dbm.gnu.gdbm.firstkey"> +<code>gdbm.firstkey()</code> </dt> <dd> +<p>It’s possible to loop over every key in the database using this method and the <a class="reference internal" href="#dbm.gnu.gdbm.nextkey" title="dbm.gnu.gdbm.nextkey"><code>nextkey()</code></a> method. The traversal is ordered by <code>gdbm</code>’s internal hash values, and won’t be sorted by the key values. This method returns the starting key.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="dbm.gnu.gdbm.nextkey"> +<code>gdbm.nextkey(key)</code> </dt> <dd> +<p>Returns the key that follows <em>key</em> in the traversal. The following code prints every key in the database <code>db</code>, without having to create a list in memory that contains them all:</p> <pre data-language="python">k = db.firstkey() +while k is not None: + print(k) + k = db.nextkey(k) +</pre> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="dbm.gnu.gdbm.reorganize"> +<code>gdbm.reorganize()</code> </dt> <dd> +<p>If you have carried out a lot of deletions and would like to shrink the space used by the <code>gdbm</code> file, this routine will reorganize the database. <code>gdbm</code> objects will not shorten the length of a database file except by using this reorganization; otherwise, deleted file space will be kept and reused as new (key, value) pairs are added.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="dbm.gnu.gdbm.sync"> +<code>gdbm.sync()</code> </dt> <dd> +<p>When the database has been opened in fast mode, this method forces any unwritten data to be written to the disk.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="dbm.gnu.gdbm.close"> +<code>gdbm.close()</code> </dt> <dd> +<p>Close the <code>gdbm</code> database.</p> </dd> +</dl> </dd> +</dl> </section> <section id="module-dbm.ndbm"> <span id="dbm-ndbm-interface-based-on-ndbm"></span><h2>dbm.ndbm — Interface based on ndbm</h2> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/ndbm.py">Lib/dbm/ndbm.py</a></p> <p>The <a class="reference internal" href="#module-dbm.ndbm" title='dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)'><code>dbm.ndbm</code></a> module provides an interface to the Unix “(n)dbm” library. Dbm objects behave like mappings (dictionaries), except that keys and values are always stored as bytes. Printing a <code>dbm</code> object doesn’t print the keys and values, and the <code>items()</code> and <code>values()</code> methods are not supported.</p> <p>This module can be used with the “classic” ndbm interface or the GNU GDBM compatibility interface. On Unix, the <strong class="program">configure</strong> script will attempt to locate the appropriate header file to simplify building this module.</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>The ndbm library shipped as part of macOS has an undocumented limitation on the 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).</p> </div> <dl class="py exception"> <dt class="sig sig-object py" id="dbm.ndbm.error"> +<code>exception dbm.ndbm.error</code> </dt> <dd> +<p>Raised on <a class="reference internal" href="#module-dbm.ndbm" title='dbm.ndbm: The standard "database" interface, based on ndbm. (Unix)'><code>dbm.ndbm</code></a>-specific errors, such as I/O errors. <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a> is raised for general mapping errors like specifying an incorrect key.</p> </dd> +</dl> <dl class="py data"> <dt class="sig sig-object py" id="dbm.ndbm.library"> +<code>dbm.ndbm.library</code> </dt> <dd> +<p>Name of the <code>ndbm</code> implementation library used.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="dbm.ndbm.open"> +<code>dbm.ndbm.open(filename[, flag[, mode]])</code> </dt> <dd> +<p>Open a dbm database and return a <code>ndbm</code> object. The <em>filename</em> argument is the name of the database file (without the <code>.dir</code> or <code>.pag</code> extensions).</p> <p>The optional <em>flag</em> argument must be one of these values:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Value</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr> +<td><p><code>'r'</code></p></td> <td><p>Open existing database for reading only (default)</p></td> </tr> <tr> +<td><p><code>'w'</code></p></td> <td><p>Open existing database for reading and writing</p></td> </tr> <tr> +<td><p><code>'c'</code></p></td> <td><p>Open database for reading and writing, creating it if it doesn’t exist</p></td> </tr> <tr> +<td><p><code>'n'</code></p></td> <td><p>Always create a new, empty database, open for reading and writing</p></td> </tr> </table> <p>The optional <em>mode</em> argument is the Unix mode of the file, used only when the database has to be created. It defaults to octal <code>0o666</code> (and will be modified by the prevailing umask).</p> <p>In addition to the dictionary-like methods, <code>ndbm</code> objects provide the following method:</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for filename.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="dbm.ndbm.ndbm.close"> +<code>ndbm.close()</code> </dt> <dd> +<p>Close the <code>ndbm</code> database.</p> </dd> +</dl> </dd> +</dl> </section> <section id="module-dbm.dumb"> <span id="dbm-dumb-portable-dbm-implementation"></span><h2>dbm.dumb — Portable DBM implementation</h2> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/dbm/dumb.py">Lib/dbm/dumb.py</a></p> <div class="admonition note" id="index-0"> <p class="admonition-title">Note</p> <p>The <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code>dbm.dumb</code></a> module is intended as a last resort fallback for the <a class="reference internal" href="#module-dbm" title='dbm: Interfaces to various Unix "database" formats.'><code>dbm</code></a> module when a more robust module is not available. The <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code>dbm.dumb</code></a> module is not written for speed and is not nearly as heavily used as the other database modules.</p> </div> <p>The <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code>dbm.dumb</code></a> module provides a persistent dictionary-like interface which is written entirely in Python. Unlike other modules such as <a class="reference internal" href="#module-dbm.gnu" title="dbm.gnu: GNU's reinterpretation of dbm. (Unix)"><code>dbm.gnu</code></a> no external library is required. As with other persistent mappings, the keys and values are always stored as bytes.</p> <p>The module defines the following:</p> <dl class="py exception"> <dt class="sig sig-object py" id="dbm.dumb.error"> +<code>exception dbm.dumb.error</code> </dt> <dd> +<p>Raised on <a class="reference internal" href="#module-dbm.dumb" title="dbm.dumb: Portable implementation of the simple DBM interface."><code>dbm.dumb</code></a>-specific errors, such as I/O errors. <a class="reference internal" href="exceptions#KeyError" title="KeyError"><code>KeyError</code></a> is raised for general mapping errors like specifying an incorrect key.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="dbm.dumb.open"> +<code>dbm.dumb.open(filename[, flag[, mode]])</code> </dt> <dd> +<p>Open a <code>dumbdbm</code> database and return a dumbdbm object. The <em>filename</em> argument is the basename of the database file (without any specific extensions). When a dumbdbm database is created, files with <code>.dat</code> and <code>.dir</code> extensions are created.</p> <p>The optional <em>flag</em> argument can be:</p> <table class="docutils align-default"> <thead> <tr> +<th class="head"><p>Value</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tr> +<td><p><code>'r'</code></p></td> <td><p>Open existing database for reading only (default)</p></td> </tr> <tr> +<td><p><code>'w'</code></p></td> <td><p>Open existing database for reading and writing</p></td> </tr> <tr> +<td><p><code>'c'</code></p></td> <td><p>Open database for reading and writing, creating it if it doesn’t exist</p></td> </tr> <tr> +<td><p><code>'n'</code></p></td> <td><p>Always create a new, empty database, open for reading and writing</p></td> </tr> </table> <p>The optional <em>mode</em> argument is the Unix mode of the file, used only when the database has to be created. It defaults to octal <code>0o666</code> (and will be modified by the prevailing umask).</p> <div class="admonition warning"> <p class="admonition-title">Warning</p> <p>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.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span><a class="reference internal" href="#dbm.dumb.open" title="dbm.dumb.open"><code>open()</code></a> always creates a new database when the flag has the value <code>'n'</code>.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>A database opened with flags <code>'r'</code> is now read-only. Opening with flags <code>'r'</code> and <code>'w'</code> no longer creates a database if it does not exist.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Accepts <a class="reference internal" href="../glossary#term-path-like-object"><span class="xref std std-term">path-like object</span></a> for filename.</p> </div> <p>In addition to the methods provided by the <a class="reference internal" href="collections.abc#collections.abc.MutableMapping" title="collections.abc.MutableMapping"><code>collections.abc.MutableMapping</code></a> class, <code>dumbdbm</code> objects provide the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="dbm.dumb.dumbdbm.sync"> +<code>dumbdbm.sync()</code> </dt> <dd> +<p>Synchronize the on-disk directory and data files. This method is called by the <code>Shelve.sync()</code> method.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="dbm.dumb.dumbdbm.close"> +<code>dumbdbm.close()</code> </dt> <dd> +<p>Close the <code>dumbdbm</code> database.</p> </dd> +</dl> </dd> +</dl> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/dbm.html" class="_attribution-link">https://docs.python.org/3.12/library/dbm.html</a> + </p> +</div> |
