dirdbm.html   [plain text]


<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><title>Twisted Documentation: DirDBM: Directory-based Storage</title><link href="../howto/stylesheet.css" type="text/css" rel="stylesheet" /></head><body bgcolor="white"><h1 class="title">DirDBM: Directory-based Storage</h1><div class="toc"><ol><li><a href="#auto0">dirdbm.DirDBM</a></li><li><a href="#auto1">dirdbm.Shelf</a></li></ol></div><div class="content"><span></span><h2>dirdbm.DirDBM<a name="auto0"></a></h2><p><code class="API"><a href="http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-1.3.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">twisted.persisted.dirdbm.DirDBM</a></code> is a DBM-like storage system. 
That is, it stores mappings between keys
and values, like a Python dictionary, except that it stores the values in files
in a directory - each entry is a different file. The keys must always be strings,
as are the values. Other than that, <code class="API"><a href="http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-1.3.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">DirDBM</a></code>
objects act just like Python dictionaries.</p><p><code class="API"><a href="http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-1.3.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">DirDBM</a></code> is useful for cases
when you want to store small amounts of data in an organized fashion, without having
to deal with the complexity of a RDBMS or other sophisticated database. It is simple,
easy to use, cross-platform, and doesn't require any external C libraries, unlike
Python's built-in DBM modules.</p><pre class="python-interpreter">
&gt;&gt;&gt; from twisted.persisted import dirdbm
&gt;&gt;&gt; d = dirdbm.DirDBM(&quot;/tmp/dir&quot;)
&gt;&gt;&gt; d[&quot;librarian&quot;] = &quot;ook&quot;
&gt;&gt;&gt; d[&quot;librarian&quot;]        
'ook'
&gt;&gt;&gt; d.keys()
['librarian']
&gt;&gt;&gt; del d[&quot;librarian&quot;]
&gt;&gt;&gt; d.items()
[]
</pre><h2>dirdbm.Shelf<a name="auto1"></a></h2><p>Sometimes it is neccessary to persist more complicated objects than strings.
With some care, <code class="API"><a href="http://twistedmatrix.com/documents/TwistedDocs/TwistedDocs-1.3.0/api/twisted.persisted.dirdbm.Shelf.html" title="twisted.persisted.dirdbm.Shelf">dirdbm.Shelf</a></code>
can transparently persist
them. <code>Shelf</code> works exactly like <code>DirDBM</code>, except that
the values (but not the keys) can be arbitrary picklable objects. However,
notice that mutating an object after it has been stored in the
<code>Shelf</code> has no effect on the Shelf.
When mutating objects, it is neccessary to explictly store them back in the <code>Shelf</code>
afterwards:</p><pre class="python-interpreter">
&gt;&gt;&gt; from twisted.persisted import dirdbm
&gt;&gt;&gt; d = dirdbm.Shelf(&quot;/tmp/dir2&quot;)
&gt;&gt;&gt; d[&quot;key&quot;] = [1, 2]
&gt;&gt;&gt; d[&quot;key&quot;]
[1, 2]
&gt;&gt;&gt; l = d[&quot;key&quot;]
&gt;&gt;&gt; l.append(3)
&gt;&gt;&gt; d[&quot;key&quot;]
[1, 2]
&gt;&gt;&gt; d[&quot;key&quot;] = l
&gt;&gt;&gt; d[&quot;key&quot;]
[1, 2, 3]
</pre></div><p><a href="../howto/index.html">Index</a></p><span class="version">Version: 1.3.0</span></body></html>