“Python/docs/3.9/library/dbm”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/dbm
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:dbm — Unix “数据库”的接口 — Python 文档}}
 
<div id="module-dbm" class="section">
 
<div id="module-dbm" class="section">
  
 
<span id="dbm-interfaces-to-unix-databases"></span>
 
<span id="dbm-interfaces-to-unix-databases"></span>
= [[#module-dbm|<code>dbm</code>]] --- Interfaces to Unix &quot;databases&quot; =
+
= dbm — Unix“数据库”的接口 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/dbm/__init__.py Lib/dbm/__init__.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/dbm/__init__.py`</span>]]
  
[[#module-dbm|<code>dbm</code>]] is a generic interface to variants of the DBM database ---
 
[[#module-dbm.gnu|<code>dbm.gnu</code>]] or [[#module-dbm.ndbm|<code>dbm.ndbm</code>]]. If none of these modules is installed, the
 
slow-but-simple implementation in module [[#module-dbm.dumb|<code>dbm.dumb</code>]] will be used. There
 
is a [https://www.jcea.es/programacion/pybsddb.htm third party interface] to
 
the Oracle Berkeley DB.
 
  
; ''exception'' <code>dbm.</code><code>error</code>
+
-----
: A tuple containing the exceptions that can be raised by each of the supported modules, with a unique exception also named [[#dbm.error|<code>dbm.error</code>]] as the first item --- the latter is used when [[#dbm.error|<code>dbm.error</code>]] is raised.
+
 
 +
[[#module-dbm|dbm]] 是 DBM 数据库变体的通用接口 - [[#module-dbm.gnu|dbm.gnu]] 或 [[#module-dbm.ndbm|dbm.ndbm]]。 如果没有安装这些模块,将使用模块 [[#module-dbm.dumb|dbm.dumb]] 中缓慢但简单的实现。 Oracle Berkeley DB 有一个 [https://www.jcea.es/programacion/pybsddb.htm 第三方接口] 。
 +
 
 +
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">dbm.</span></span><span class="sig-name descname"><span class="pre">error</span></span>
 +
: 包含每个受支持模块可以引发的异常的元组,唯一的异常也命名为 [[#dbm.error|dbm.error]] 作为第一项——后者在 [[#dbm.error|dbm.error]] 时使用被提出。
  
 
<dl>
 
<dl>
<dt><code>dbm.</code><code>whichdb</code><span class="sig-paren">(</span>''<span class="n">filename</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dbm.</span></span><span class="sig-name descname"><span class="pre">whichdb</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">filename</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>This function attempts to guess which of the several simple database modules
+
<dd><p>此函数试图猜测可用的几个简单数据库模块中的哪一个——[[#module-dbm.gnu|dbm.gnu]][[#module-dbm.ndbm|dbm.ndbm]] [[#module-dbm.dumb|dbm.dumb]]——应该用于打开一个给定的文件。</p>
available --- [[#module-dbm.gnu|<code>dbm.gnu</code>]], [[#module-dbm.ndbm|<code>dbm.ndbm</code>]] or [[#module-dbm.dumb|<code>dbm.dumb</code>]] --- should
+
<p>返回以下值之一: <code>None</code> 如果文件因为不可读或不存在而无法打开; 如果无法猜测文件的格式,则为空字符串 (<code>''</code>); 或包含所需模块名称的字符串,例如 <code>'dbm.ndbm'</code> <code>'dbm.gnu'</code></p></dd></dl>
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>
 
  
 
<dl>
 
<dl>
<dt><code>dbm.</code><code>open</code><span class="sig-paren">(</span>''<span class="n">file</span>'', ''<span class="n">flag</span><span class="o">=</span><span class="default_value">'r'</span>'', ''<span class="n">mode</span><span class="o">=</span><span class="default_value">0o666</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dbm.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">file</span></span>'', ''<span class="n"><span class="pre">flag</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span>'', ''<span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0o666</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Open the database file ''file'' and return a corresponding object.</p>
+
<dd><p>打开数据库文件''file'',返回一个对应的对象。</p>
<p>If the database file already exists, the [[#dbm.whichdb|<code>whichdb()</code>]] function is used to
+
<p>如果数据库文件已经存在,则使用[[#dbm.whichdb|whichdb()]]函数来确定其类型并使用相应的模块; 如果它不存在,则使用上面列出的第一个可以导入的模块。</p>
determine its type and the appropriate module is used; if it does not exist,
+
<p>可选的 ''flag'' 参数可以是:</p>
the first module listed above that can be imported is used.</p>
 
<p>The optional ''flag'' argument can be:</p>
 
 
{|
 
{|
!width="17%"| <p>Value</p>
+
!width="17%"| <p>价值</p>
!width="83%"| <p>Meaning</p>
+
!width="83%"| <p>意义</p>
 
|-
 
|-
 
| <p><code>'r'</code></p>
 
| <p><code>'r'</code></p>
| <p>Open existing database for reading only
+
| <p>以只读方式打开现有数据库(默认)</p>
(default)</p>
 
 
|-
 
|-
 
| <p><code>'w'</code></p>
 
| <p><code>'w'</code></p>
| <p>Open existing database for reading and
+
| <p>打开现有数据库进行读写</p>
writing</p>
 
 
|-
 
|-
 
| <p><code>'c'</code></p>
 
| <p><code>'c'</code></p>
| <p>Open database for reading and writing,
+
| <p>打开数据库进行读写,如果不存在则创建</p>
creating it if it doesn't exist</p>
 
 
|-
 
|-
 
| <p><code>'n'</code></p>
 
| <p><code>'n'</code></p>
| <p>Always create a new, empty database, open
+
| <p>总是创建一个新的、空的数据库,以读写方式打开</p>
for reading and writing</p>
 
 
|}
 
|}
  
<p>The optional ''mode'' argument is the Unix mode of the file, used only when the
+
<p>可选的 ''mode'' 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 它默认为八进制 <code>0o666</code>(并将被流行的 umask 修改)。</p></dd></dl>
database has to be created. It defaults to octal <code>0o666</code> (and will be
 
modified by the prevailing umask).</p></dd></dl>
 
  
The object returned by [[#dbm.open|<code>open()</code>]] supports the same basic functionality as
+
[[#dbm.open|open()]] 返回的对象支持与字典相同的基本功能; 可以存储、检索和删除键及其对应的值,并且可以使用 [[../../reference/expressions#in|in]] 运算符和 <code>keys()</code> 方法,以及 <code>get()</code> 和 [ X267X]。
dictionaries; keys and their corresponding values can be stored, retrieved, and
 
deleted, and the [[../../reference/expressions#in|<code>in</code>]] operator and the <code>keys()</code> method are
 
available, as well as <code>get()</code> and <code>setdefault()</code>.
 
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">在 3.2 版更改: </span><code>get()</code> and <code>setdefault()</code> are now available in all database modules.
+
<span class="versionmodified changed">在 3.2 版更改:</span><code>get()</code> <code>setdefault()</code> 现在可用于所有数据库模块。
  
  
第70行: 第54行:
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">3.8 版更改: </span>Deleting a key from a read-only database raises database module specific error
+
<span class="versionmodified changed"> 3.8 版更改: </span> 从只读数据库中删除密钥会引发数据库模块特定错误,而不是 [[../exceptions#KeyError|KeyError]]
instead of [[../exceptions#KeyError|<code>KeyError</code>]].
 
  
  
 
</div>
 
</div>
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.
 
  
These objects also support being used in a [[../../reference/compound_stmts#with|<code>with</code>]] statement, which
+
这些对象还支持在 [[../../reference/compound_stmts#with|with]] 语句中使用,完成后会自动关闭它们。
will automatically close them when done.
 
  
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<span class="versionmodified changed">在 3.4 版更改: </span>Added native support for the context management protocol to the objects
+
<span class="versionmodified changed"> 在 3.4 版更改: </span> [[#dbm.open|open()]] 返回的对象添加了对上下文管理协议的本机支持。
returned by [[#dbm.open|<code>open()</code>]].
 
  
  
 
</div>
 
</div>
The following example records some hostnames and a corresponding title, and
+
下面的例子记录了一些主机名和一个对应的标题,然后打印出数据库的内容:
then prints out the contents of the database:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第96行: 第74行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import dbm
+
<syntaxhighlight lang="python3">import dbm
  
 
# Open database, creating it if necessary.
 
# Open database, creating it if necessary.
第118行: 第96行:
 
     db['www.yahoo.com'] = 4
 
     db['www.yahoo.com'] = 4
  
# db is automatically closed when leaving the with statement.</pre>
+
# db is automatically closed when leaving the with statement.</syntaxhighlight>
  
 
</div>
 
</div>
第125行: 第103行:
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
; Module [[../shelve#module-shelve|<code>shelve</code>]]
+
; 模块 [[../shelve#module-shelve|搁架]]
: Persistence module which stores non-string data.
+
: 存储非字符串数据的持久性模块。
  
  
 
</div>
 
</div>
The individual submodules are described in the following sections.
+
以下各节介绍了各个子模块。
  
 
<div id="module-dbm.gnu" class="section">
 
<div id="module-dbm.gnu" class="section">
  
 
<span id="dbm-gnu-gnu-s-reinterpretation-of-dbm"></span>
 
<span id="dbm-gnu-gnu-s-reinterpretation-of-dbm"></span>
== [[#module-dbm.gnu|<code>dbm.gnu</code>]] --- GNU's reinterpretation of dbm ==
+
== dbm.gnu — GNU 对 dbm 的重新解释 ==
 +
 
 +
'''源代码:''' [[#id3|<span id="id4" class="problematic">:source:`Lib/dbm/gnu.py`</span>]]
 +
 
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/dbm/gnu.py Lib/dbm/gnu.py]
+
-----
  
This module is quite similar to the [[#module-dbm|<code>dbm</code>]] module, but uses the GNU library
+
该模块与 [[#module-dbm|dbm]] 模块非常相似,但使用 GNU <code>gdbm</code> 来提供一些附加功能。 请注意 [[#module-dbm.gnu|dbm.gnu]] [[#module-dbm.ndbm|dbm.ndbm]] 创建的文件格式不兼容。
<code>gdbm</code> instead to provide some additional functionality. Please note that the
 
file formats created by [[#module-dbm.gnu|<code>dbm.gnu</code>]] and [[#module-dbm.ndbm|<code>dbm.ndbm</code>]] are incompatible.
 
  
The [[#module-dbm.gnu|<code>dbm.gnu</code>]] module provides an interface to the GNU DBM library.
+
[[#module-dbm.gnu|dbm.gnu]] 模块提供了一个到 GNU DBM 库的接口。 <code>dbm.gnu.gdbm</code> 对象的行为类似于映射(字典),除了键和值在存储之前总是转换为字节。 打印 <code>gdbm</code> 对象不会打印键和值,并且不支持 <code>items()</code> <code>values()</code> 方法。
<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.
 
  
; ''exception'' <code>dbm.gnu.</code><code>error</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">dbm.gnu.</span></span><span class="sig-name descname"><span class="pre">error</span></span>
: Raised on [[#module-dbm.gnu|<code>dbm.gnu</code>]]-specific errors, such as I/O errors. [[../exceptions#KeyError|<code>KeyError</code>]] is raised for general mapping errors like specifying an incorrect key.
+
: 引发 [[#module-dbm.gnu|dbm.gnu]] 特定错误,例如 I/O 错误。 [[../exceptions#KeyError|KeyError]] 针对一般映射错误(例如指定不正确的密钥)引发。
  
 
<dl>
 
<dl>
<dt><code>dbm.gnu.</code><code>open</code><span class="sig-paren">(</span>''filename''<span class="optional">[</span>, ''flag''<span class="optional">[</span>, ''mode''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dbm.gnu.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span>''<span class="pre">filename</span>''<span class="optional">[</span>, ''<span class="pre">flag</span>''<span class="optional">[</span>, ''<span class="pre">mode</span>''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Open a <code>gdbm</code> database and return a <code>gdbm</code> object. The ''filename''
+
<dd><p>打开一个 <code>gdbm</code> 数据库并返回一个 <code>gdbm</code> 对象。 ''filename'' 参数是数据库文件的名称。</p>
argument is the name of the database file.</p>
+
<p>可选的 ''flag'' 参数可以是:</p>
<p>The optional ''flag'' argument can be:</p>
 
 
{|
 
{|
!width="17%"| <p>Value</p>
+
!width="17%"| <p>价值</p>
!width="83%"| <p>Meaning</p>
+
!width="83%"| <p>意义</p>
 
|-
 
|-
 
| <p><code>'r'</code></p>
 
| <p><code>'r'</code></p>
| <p>Open existing database for reading only
+
| <p>以只读方式打开现有数据库(默认)</p>
(default)</p>
 
 
|-
 
|-
 
| <p><code>'w'</code></p>
 
| <p><code>'w'</code></p>
| <p>Open existing database for reading and
+
| <p>打开现有数据库进行读写</p>
writing</p>
 
 
|-
 
|-
 
| <p><code>'c'</code></p>
 
| <p><code>'c'</code></p>
| <p>Open database for reading and writing,
+
| <p>打开数据库进行读写,如果不存在则创建</p>
creating it if it doesn't exist</p>
 
 
|-
 
|-
 
| <p><code>'n'</code></p>
 
| <p><code>'n'</code></p>
| <p>Always create a new, empty database, open
+
| <p>总是创建一个新的、空的数据库,以读写方式打开</p>
for reading and writing</p>
 
 
|}
 
|}
  
<p>The following additional characters may be appended to the flag to control
+
<p>可以将以下附加字符附加到标志以控制数据库的打开方式:</p>
how the database is opened:</p>
 
 
{|
 
{|
!width="17%"| <p>Value</p>
+
!width="17%"| <p>价值</p>
!width="83%"| <p>Meaning</p>
+
!width="83%"| <p>意义</p>
 
|-
 
|-
 
| <p><code>'f'</code></p>
 
| <p><code>'f'</code></p>
| <p>Open the database in fast mode. Writes
+
| <p>以快速模式打开数据库。 对数据库的写入不会同步。</p>
to the database will not be synchronized.</p>
 
 
|-
 
|-
 
| <p><code>'s'</code></p>
 
| <p><code>'s'</code></p>
| <p>Synchronized mode. This will cause changes
+
| <p>同步模式。 这将导致对数据库的更改立即写入文件。</p>
to the database to be immediately written
 
to the file.</p>
 
 
|-
 
|-
 
| <p><code>'u'</code></p>
 
| <p><code>'u'</code></p>
| <p>Do not lock database.</p>
+
| <p>不要锁定数据库。</p>
 
|}
 
|}
  
<p>Not all flags are valid for all versions of <code>gdbm</code>. The module constant
+
<p>并非所有标志都对 <code>gdbm</code> 的所有版本都有效。 模块常量 <code>open_flags</code> 是一串支持的标志字符。 如果指定了无效标志,则会引发异常 [[#dbm.gnu.error|error]]</p>
<code>open_flags</code> is a string of supported flag characters. The exception
+
<p>可选的 ''mode'' 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 默认为八进制 <code>0o666</code></p>
[[#dbm.gnu.error|<code>error</code>]] is raised if an invalid flag is specified.</p>
+
<p>除了类似字典的方法,<code>gdbm</code> 对象还有以下方法:</p>
<p>The optional ''mode'' 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>
 
 
<dl>
 
<dl>
<dt><code>gdbm.</code><code>firstkey</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">firstkey</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>It's possible to loop over every key in the database using this method and the
+
<dd><p>可以使用此方法和 [[#dbm.gnu.gdbm.nextkey|nextkey()]] 方法遍历数据库中的每个键。 遍历按 <code>gdbm</code> 的内部哈希值排序,不会按键值排序。 此方法返回起始密钥。</p></dd></dl>
[[#dbm.gnu.gdbm.nextkey|<code>nextkey()</code>]] 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>
 
<dl>
<dt><code>gdbm.</code><code>nextkey</code><span class="sig-paren">(</span>''<span class="n">key</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">nextkey</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">key</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Returns the key that follows ''key'' in the traversal. The following code prints
+
<dd><p>返回遍历中''key''后面的key。 以下代码打印数据库 <code>db</code> 中的每个键,而无需在内存中创建包含所有键的列表:</p>
every key in the database <code>db</code>, without having to create a list in memory that
 
contains them all:</p>
 
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>k = db.firstkey()
+
<syntaxhighlight lang="python3">k = db.firstkey()
while k != None:
+
while k is not None:
 
     print(k)
 
     print(k)
     k = db.nextkey(k)</pre>
+
     k = db.nextkey(k)</syntaxhighlight>
  
 
</div>
 
</div>
第233行: 第189行:
  
 
<dl>
 
<dl>
<dt><code>gdbm.</code><code>reorganize</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">reorganize</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>If you have carried out a lot of deletions and would like to shrink the space
+
<dd><p>如果您进行了大量删除并希望缩小 <code>gdbm</code> 文件使用的空间,则此例程将重新组织数据库。 <code>gdbm</code> 对象不会缩短数据库文件的长度,除非使用这种重组; 否则,删除的文件空间将被保留并在添加新的(键,值)对时重新使用。</p></dd></dl>
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>
 
<dl>
<dt><code>gdbm.</code><code>sync</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">sync</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>When the database has been opened in fast mode, this method forces any
+
<dd><p>当以快速模式打开数据库时,此方法会强制将任何未写入的数据写入磁盘。</p></dd></dl>
unwritten data to be written to the disk.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>gdbm.</code><code>close</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">gdbm.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Close the <code>gdbm</code> database.</p></dd></dl>
+
<dd><p>关闭 <code>gdbm</code> 数据库。</p></dd></dl>
 
</dd></dl>
 
</dd></dl>
  
第255行: 第206行:
  
 
<span id="dbm-ndbm-interface-based-on-ndbm"></span>
 
<span id="dbm-ndbm-interface-based-on-ndbm"></span>
== [[#module-dbm.ndbm|<code>dbm.ndbm</code>]] --- Interface based on ndbm ==
+
== dbm.ndbm — 基于 ndbm 的接口 ==
 +
 
 +
'''源代码:''' [[#id5|<span id="id6" class="problematic">:source:`Lib/dbm/ndbm.py`</span>]]
 +
 
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/dbm/ndbm.py Lib/dbm/ndbm.py]
+
-----
  
The [[#module-dbm.ndbm|<code>dbm.ndbm</code>]] module provides an interface to the Unix &quot;(n)dbm&quot; library.
+
[[#module-dbm.ndbm|dbm.ndbm]] 模块提供了一个到 Unix“(n)dbm”库的接口。 Dbm 对象的行为类似于映射(字典),不同之处在于键和值始终存储为字节。 打印 <code>dbm</code> 对象不会打印键和值,并且不支持 <code>items()</code> <code>values()</code> 方法。
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.
 
  
This module can be used with the &quot;classic&quot; ndbm interface or the GNU GDBM
+
该模块可以与“经典”ndbm 接口或 GNU GDBM 兼容接口一起使用。 在 Unix 上,'''configure''' 脚本将尝试定位适当的头文件以简化构建此模块。
compatibility interface. On Unix, the '''configure''' script will attempt
 
to locate the appropriate header file to simplify building this module.
 
  
; ''exception'' <code>dbm.ndbm.</code><code>error</code>
+
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">dbm.ndbm.</span></span><span class="sig-name descname"><span class="pre">error</span></span>
: Raised on [[#module-dbm.ndbm|<code>dbm.ndbm</code>]]-specific errors, such as I/O errors. [[../exceptions#KeyError|<code>KeyError</code>]] is raised for general mapping errors like specifying an incorrect key.
+
: 引发 [[#module-dbm.ndbm|dbm.ndbm]] 特定错误,例如 I/O 错误。 [[../exceptions#KeyError|KeyError]] 针对一般映射错误(例如指定不正确的密钥)引发。
  
; <code>dbm.ndbm.</code><code>library</code>
+
; <span class="sig-prename descclassname"><span class="pre">dbm.ndbm.</span></span><span class="sig-name descname"><span class="pre">library</span></span>
: Name of the <code>ndbm</code> implementation library used.
+
: 使用的 <code>ndbm</code> 实现库的名称。
  
 
<dl>
 
<dl>
<dt><code>dbm.ndbm.</code><code>open</code><span class="sig-paren">(</span>''filename''<span class="optional">[</span>, ''flag''<span class="optional">[</span>, ''mode''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dbm.ndbm.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span>''<span class="pre">filename</span>''<span class="optional">[</span>, ''<span class="pre">flag</span>''<span class="optional">[</span>, ''<span class="pre">mode</span>''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Open a dbm database and return a <code>ndbm</code> object. The ''filename'' argument is the
+
<dd><p>打开一个 dbm 数据库并返回一个 <code>ndbm</code> 对象。 ''filename'' 参数是数据库文件的名称(没有 <code>.dir</code> <code>.pag</code> 扩展名)。</p>
name of the database file (without the <code>.dir</code> or <code>.pag</code> extensions).</p>
+
<p>可选的 ''flag'' 参数必须是以下值之一:</p>
<p>The optional ''flag'' argument must be one of these values:</p>
 
 
{|
 
{|
!width="17%"| <p>Value</p>
+
!width="17%"| <p>价值</p>
!width="83%"| <p>Meaning</p>
+
!width="83%"| <p>意义</p>
 
|-
 
|-
 
| <p><code>'r'</code></p>
 
| <p><code>'r'</code></p>
| <p>Open existing database for reading only
+
| <p>以只读方式打开现有数据库(默认)</p>
(default)</p>
 
 
|-
 
|-
 
| <p><code>'w'</code></p>
 
| <p><code>'w'</code></p>
| <p>Open existing database for reading and
+
| <p>打开现有数据库进行读写</p>
writing</p>
 
 
|-
 
|-
 
| <p><code>'c'</code></p>
 
| <p><code>'c'</code></p>
| <p>Open database for reading and writing,
+
| <p>打开数据库进行读写,如果不存在则创建</p>
creating it if it doesn't exist</p>
 
 
|-
 
|-
 
| <p><code>'n'</code></p>
 
| <p><code>'n'</code></p>
| <p>Always create a new, empty database, open
+
| <p>总是创建一个新的、空的数据库,以读写方式打开</p>
for reading and writing</p>
 
 
|}
 
|}
  
<p>The optional ''mode'' argument is the Unix mode of the file, used only when the
+
<p>可选的 ''mode'' 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 它默认为八进制 <code>0o666</code>(并将被流行的 umask 修改)。</p>
database has to be created. It defaults to octal <code>0o666</code> (and will be
+
<p>除了类似字典的方法外,<code>ndbm</code> 对象还提供以下方法:</p>
modified by the prevailing umask).</p>
 
<p>In addition to the dictionary-like methods, <code>ndbm</code> objects
 
provide the following method:</p>
 
 
<dl>
 
<dl>
<dt><code>ndbm.</code><code>close</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">ndbm.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Close the <code>ndbm</code> database.</p></dd></dl>
+
<dd><p>关闭 <code>ndbm</code> 数据库。</p></dd></dl>
 
</dd></dl>
 
</dd></dl>
  
第315行: 第256行:
  
 
<span id="dbm-dumb-portable-dbm-implementation"></span>
 
<span id="dbm-dumb-portable-dbm-implementation"></span>
== [[#module-dbm.dumb|<code>dbm.dumb</code>]] --- Portable DBM implementation ==
+
== dbm.dumb — 便携式 DBM 实现 ==
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/dbm/dumb.py Lib/dbm/dumb.py]
+
'''源代码:''' [[#id7|<span id="id8" class="problematic">:source:`Lib/dbm/dumb.py`</span>]]
  
 
<div id="index-0" class="admonition note">
 
<div id="index-0" class="admonition note">
  
注解
+
笔记
  
The [[#module-dbm.dumb|<code>dbm.dumb</code>]] module is intended as a last resort fallback for the
+
当更强大的模块不可用时,[[#module-dbm.dumb|dbm.dumb]] 模块旨在作为 [[#module-dbm|dbm]] 模块的最后手段。 [[#module-dbm.dumb|dbm.dumb]] 模块不是为了速度而编写的,并且不像其他数据库模块那样频繁使用。
[[#module-dbm|<code>dbm</code>]] module when a more robust module is not available. The [[#module-dbm.dumb|<code>dbm.dumb</code>]]
 
module is not written for speed and is not nearly as heavily used as the other
 
database modules.
 
  
  
 
</div>
 
</div>
The [[#module-dbm.dumb|<code>dbm.dumb</code>]] module provides a persistent dictionary-like interface which
 
is written entirely in Python. Unlike other modules such as [[#module-dbm.gnu|<code>dbm.gnu</code>]] no
 
external library is required. As with other persistent mappings, the keys and
 
values are always stored as bytes.
 
  
The module defines the following:
+
-----
 +
 
 +
[[#module-dbm.dumb|dbm.dumb]] 模块提供了一个完全用 Python 编写的类似字典的持久接口。 与 [[#module-dbm.gnu|dbm.gnu]] 等其他模块不同,不需要外部库。 与其他持久映射一样,键和值始终存储为字节。
  
; ''exception'' <code>dbm.dumb.</code><code>error</code>
+
该模块定义了以下内容:
: Raised on [[#module-dbm.dumb|<code>dbm.dumb</code>]]-specific errors, such as I/O errors. [[../exceptions#KeyError|<code>KeyError</code>]] is raised for general mapping errors like specifying an incorrect key.
+
 
 +
; ''<span class="pre">exception</span>'' <span class="sig-prename descclassname"><span class="pre">dbm.dumb.</span></span><span class="sig-name descname"><span class="pre">error</span></span>
 +
: 引发 [[#module-dbm.dumb|dbm.dumb]] 特定错误,例如 I/O 错误。 [[../exceptions#KeyError|KeyError]] 针对一般映射错误(例如指定不正确的密钥)引发。
  
 
<dl>
 
<dl>
<dt><code>dbm.dumb.</code><code>open</code><span class="sig-paren">(</span>''filename''<span class="optional">[</span>, ''flag''<span class="optional">[</span>, ''mode''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dbm.dumb.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span>''<span class="pre">filename</span>''<span class="optional">[</span>, ''<span class="pre">flag</span>''<span class="optional">[</span>, ''<span class="pre">mode</span>''<span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span></dt>
<dd><p>Open a <code>dumbdbm</code> database and return a dumbdbm object. The ''filename'' argument is
+
<dd><p>打开一个<code>dumbdbm</code> 数据库并返回一个dumbdbm 对象。 ''filename'' 参数是数据库文件的基本名称(没有任何特定的扩展名)。 当创建一个dumbdbm 数据库时,会创建带有<code>.dat</code> <code>.dir</code> 扩展名的文件。</p>
the basename of the database file (without any specific extensions). When a
+
<p>可选的 ''flag'' 参数可以是:</p>
dumbdbm database is created, files with <code>.dat</code> and <code>.dir</code> extensions
 
are created.</p>
 
<p>The optional ''flag'' argument can be:</p>
 
 
{|
 
{|
!width="17%"| <p>Value</p>
+
!width="17%"| <p>价值</p>
!width="83%"| <p>Meaning</p>
+
!width="83%"| <p>意义</p>
 
|-
 
|-
 
| <p><code>'r'</code></p>
 
| <p><code>'r'</code></p>
| <p>Open existing database for reading only
+
| <p>以只读方式打开现有数据库(默认)</p>
(default)</p>
 
 
|-
 
|-
 
| <p><code>'w'</code></p>
 
| <p><code>'w'</code></p>
| <p>Open existing database for reading and
+
| <p>打开现有数据库进行读写</p>
writing</p>
 
 
|-
 
|-
 
| <p><code>'c'</code></p>
 
| <p><code>'c'</code></p>
| <p>Open database for reading and writing,
+
| <p>打开数据库进行读写,如果不存在则创建</p>
creating it if it doesn't exist</p>
 
 
|-
 
|-
 
| <p><code>'n'</code></p>
 
| <p><code>'n'</code></p>
| <p>Always create a new, empty database, open
+
| <p>总是创建一个新的、空的数据库,以读写方式打开</p>
for reading and writing</p>
 
 
|}
 
|}
  
<p>The optional ''mode'' argument is the Unix mode of the file, used only when the
+
<p>可选的 ''mode'' 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 它默认为八进制 <code>0o666</code>(并将被流行的 umask 修改)。</p>
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">
 
<div class="admonition warning">
  
 
<p>警告</p>
 
<p>警告</p>
<p>It is possible to crash the Python interpreter when loading a database
+
<p>由于 Python 的 AST 编译器中的堆栈深度限制,在加载具有足够大/复杂条目的数据库时,可能会导致 Python 解释器崩溃。</p>
with a sufficiently large/complex entry due to stack depth limitations in
 
Python's AST compiler.</p>
 
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.5 版更改: </span>[[#dbm.dumb.open|<code>open()</code>]] always creates a new database when the flag has the value
+
<p><span class="versionmodified changed">在 3.5 版更改:</span>[[#dbm.dumb.open|open()]] 总是在标志具有值 <code>'n'</code> 时创建一个新数据库。</p>
<code>'n'</code>.</p>
 
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.8 版更改: </span>A database opened with flags <code>'r'</code> is now read-only. Opening with
+
<p><span class="versionmodified changed">在 3.8 版更改:</span>使用标志 <code>'r'</code> 打开的数据库现在是只读的。 如果数据库不存在,使用标志 <code>'r'</code> <code>'w'</code> 打开不再创建数据库。</p>
flags <code>'r'</code> and <code>'w'</code> no longer creates a database if it does not
 
exist.</p>
 
  
 
</div>
 
</div>
<p>In addition to the methods provided by the
+
<p>除了 [[../collections.abc#collections.abc|collections.abc.MutableMapping]] 类提供的方法之外,<code>dumbdbm</code> 对象还提供以下方法:</p>
[[../collections.abc#collections.abc|<code>collections.abc.MutableMapping</code>]] class, <code>dumbdbm</code> objects
 
provide the following methods:</p>
 
 
<dl>
 
<dl>
<dt><code>dumbdbm.</code><code>sync</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dumbdbm.</span></span><span class="sig-name descname"><span class="pre">sync</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Synchronize the on-disk directory and data files. This method is called
+
<dd><p>同步磁盘目录和数据文件。 该方法由 <code>Shelve.sync()</code> 方法调用。</p></dd></dl>
by the <code>Shelve.sync()</code> method.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>dumbdbm.</code><code>close</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">dumbdbm.</span></span><span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Close the <code>dumbdbm</code> database.</p></dd></dl>
+
<dd><p>关闭 <code>dumbdbm</code> 数据库。</p></dd></dl>
 
</dd></dl>
 
</dd></dl>
  
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

dbm — Unix“数据库”的接口

源代码: :source:`Lib/dbm/__init__.py`



dbm 是 DBM 数据库变体的通用接口 - dbm.gnudbm.ndbm。 如果没有安装这些模块,将使用模块 dbm.dumb 中缓慢但简单的实现。 Oracle Berkeley DB 有一个 第三方接口

exception dbm.error
包含每个受支持模块可以引发的异常的元组,唯一的异常也命名为 dbm.error 作为第一项——后者在 dbm.error 时使用被提出。
dbm.whichdb(filename)

此函数试图猜测可用的几个简单数据库模块中的哪一个——dbm.gnudbm.ndbmdbm.dumb——应该用于打开一个给定的文件。

返回以下值之一: None 如果文件因为不可读或不存在而无法打开; 如果无法猜测文件的格式,则为空字符串 (); 或包含所需模块名称的字符串,例如 'dbm.ndbm''dbm.gnu'

dbm.open(file, flag='r', mode=0o666)

打开数据库文件file,返回一个对应的对象。

如果数据库文件已经存在,则使用whichdb()函数来确定其类型并使用相应的模块; 如果它不存在,则使用上面列出的第一个可以导入的模块。

可选的 flag 参数可以是:

价值

意义

'r'

以只读方式打开现有数据库(默认)

'w'

打开现有数据库进行读写

'c'

打开数据库进行读写,如果不存在则创建

'n'

总是创建一个新的、空的数据库,以读写方式打开

可选的 mode 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 它默认为八进制 0o666(并将被流行的 umask 修改)。

open() 返回的对象支持与字典相同的基本功能; 可以存储、检索和删除键及其对应的值,并且可以使用 in 运算符和 keys() 方法,以及 get() 和 [ X267X]。

在 3.2 版更改:get()setdefault() 现在可用于所有数据库模块。


3.8 版更改: 从只读数据库中删除密钥会引发数据库模块特定错误,而不是 KeyError


键和值始终存储为字节。 这意味着当使用字符串时,它们在存储之前被隐式转换为默认编码。

这些对象还支持在 with 语句中使用,完成后会自动关闭它们。

在 3.4 版更改: open() 返回的对象添加了对上下文管理协议的本机支持。


下面的例子记录了一些主机名和一个对应的标题,然后打印出数据库的内容:

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.

也可以看看

模块 搁架
存储非字符串数据的持久性模块。


以下各节介绍了各个子模块。

dbm.gnu — GNU 对 dbm 的重新解释

源代码: :source:`Lib/dbm/gnu.py`



该模块与 dbm 模块非常相似,但使用 GNU 库 gdbm 来提供一些附加功能。 请注意 dbm.gnudbm.ndbm 创建的文件格式不兼容。

dbm.gnu 模块提供了一个到 GNU DBM 库的接口。 dbm.gnu.gdbm 对象的行为类似于映射(字典),除了键和值在存储之前总是转换为字节。 打印 gdbm 对象不会打印键和值,并且不支持 items()values() 方法。

exception dbm.gnu.error
引发 dbm.gnu 特定错误,例如 I/O 错误。 KeyError 针对一般映射错误(例如指定不正确的密钥)引发。
dbm.gnu.open(filename[, flag[, mode]])

打开一个 gdbm 数据库并返回一个 gdbm 对象。 filename 参数是数据库文件的名称。

可选的 flag 参数可以是:

价值

意义

'r'

以只读方式打开现有数据库(默认)

'w'

打开现有数据库进行读写

'c'

打开数据库进行读写,如果不存在则创建

'n'

总是创建一个新的、空的数据库,以读写方式打开

可以将以下附加字符附加到标志以控制数据库的打开方式:

价值

意义

'f'

以快速模式打开数据库。 对数据库的写入不会同步。

's'

同步模式。 这将导致对数据库的更改立即写入文件。

'u'

不要锁定数据库。

并非所有标志都对 gdbm 的所有版本都有效。 模块常量 open_flags 是一串支持的标志字符。 如果指定了无效标志,则会引发异常 error

可选的 mode 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 默认为八进制 0o666

除了类似字典的方法,gdbm 对象还有以下方法:

gdbm.firstkey()

可以使用此方法和 nextkey() 方法遍历数据库中的每个键。 遍历按 gdbm 的内部哈希值排序,不会按键值排序。 此方法返回起始密钥。

gdbm.nextkey(key)

返回遍历中key后面的key。 以下代码打印数据库 db 中的每个键,而无需在内存中创建包含所有键的列表:

k = db.firstkey()
while k is not None:
    print(k)
    k = db.nextkey(k)
gdbm.reorganize()

如果您进行了大量删除并希望缩小 gdbm 文件使用的空间,则此例程将重新组织数据库。 gdbm 对象不会缩短数据库文件的长度,除非使用这种重组; 否则,删除的文件空间将被保留并在添加新的(键,值)对时重新使用。

gdbm.sync()

当以快速模式打开数据库时,此方法会强制将任何未写入的数据写入磁盘。

gdbm.close()

关闭 gdbm 数据库。


dbm.ndbm — 基于 ndbm 的接口

源代码: :source:`Lib/dbm/ndbm.py`



dbm.ndbm 模块提供了一个到 Unix“(n)dbm”库的接口。 Dbm 对象的行为类似于映射(字典),不同之处在于键和值始终存储为字节。 打印 dbm 对象不会打印键和值,并且不支持 items()values() 方法。

该模块可以与“经典”ndbm 接口或 GNU GDBM 兼容接口一起使用。 在 Unix 上,configure 脚本将尝试定位适当的头文件以简化构建此模块。

exception dbm.ndbm.error
引发 dbm.ndbm 特定错误,例如 I/O 错误。 KeyError 针对一般映射错误(例如指定不正确的密钥)引发。
dbm.ndbm.library
使用的 ndbm 实现库的名称。
dbm.ndbm.open(filename[, flag[, mode]])

打开一个 dbm 数据库并返回一个 ndbm 对象。 filename 参数是数据库文件的名称(没有 .dir.pag 扩展名)。

可选的 flag 参数必须是以下值之一:

价值

意义

'r'

以只读方式打开现有数据库(默认)

'w'

打开现有数据库进行读写

'c'

打开数据库进行读写,如果不存在则创建

'n'

总是创建一个新的、空的数据库,以读写方式打开

可选的 mode 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 它默认为八进制 0o666(并将被流行的 umask 修改)。

除了类似字典的方法外,ndbm 对象还提供以下方法:

ndbm.close()

关闭 ndbm 数据库。


dbm.dumb — 便携式 DBM 实现

源代码: :source:`Lib/dbm/dumb.py`

笔记

当更强大的模块不可用时,dbm.dumb 模块旨在作为 dbm 模块的最后手段。 dbm.dumb 模块不是为了速度而编写的,并且不像其他数据库模块那样频繁使用。



dbm.dumb 模块提供了一个完全用 Python 编写的类似字典的持久接口。 与 dbm.gnu 等其他模块不同,不需要外部库。 与其他持久映射一样,键和值始终存储为字节。

该模块定义了以下内容:

exception dbm.dumb.error
引发 dbm.dumb 特定错误,例如 I/O 错误。 KeyError 针对一般映射错误(例如指定不正确的密钥)引发。
dbm.dumb.open(filename[, flag[, mode]])

打开一个dumbdbm 数据库并返回一个dumbdbm 对象。 filename 参数是数据库文件的基本名称(没有任何特定的扩展名)。 当创建一个dumbdbm 数据库时,会创建带有.dat.dir 扩展名的文件。

可选的 flag 参数可以是:

价值

意义

'r'

以只读方式打开现有数据库(默认)

'w'

打开现有数据库进行读写

'c'

打开数据库进行读写,如果不存在则创建

'n'

总是创建一个新的、空的数据库,以读写方式打开

可选的 mode 参数是文件的 Unix 模式,仅在必须创建数据库时使用。 它默认为八进制 0o666(并将被流行的 umask 修改)。

警告

由于 Python 的 AST 编译器中的堆栈深度限制,在加载具有足够大/复杂条目的数据库时,可能会导致 Python 解释器崩溃。

在 3.5 版更改:open() 总是在标志具有值 'n' 时创建一个新数据库。

在 3.8 版更改:使用标志 'r' 打开的数据库现在是只读的。 如果数据库不存在,使用标志 'r''w' 打开不再创建数据库。

除了 collections.abc.MutableMapping 类提供的方法之外,dumbdbm 对象还提供以下方法:

dumbdbm.sync()

同步磁盘目录和数据文件。 该方法由 Shelve.sync() 方法调用。

dumbdbm.close()

关闭 dumbdbm 数据库。