1
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3
<title>Twisted Documentation: DirDBM: Directory-based Storage</title>
4
<link href="stylesheet.css" rel="stylesheet" type="text/css"/>
8
<h1 class="title">DirDBM: Directory-based Storage</h1>
9
<div class="toc"><ol><li><a href="#auto0">dirdbm.DirDBM</a></li><li><a href="#auto1">dirdbm.Shelf</a></li></ol></div>
13
<h2>dirdbm.DirDBM<a name="auto0"/></h2>
15
<p><code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">twisted.persisted.dirdbm.DirDBM</a></code> is a DBM-like storage system.
16
That is, it stores mappings between keys
17
and values, like a Python dictionary, except that it stores the values in files
18
in a directory - each entry is a different file. The keys must always be strings,
19
as are the values. Other than that, <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">DirDBM</a></code>
20
objects act just like Python dictionaries.</p>
22
<p><code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">DirDBM</a></code> is useful for cases
23
when you want to store small amounts of data in an organized fashion, without having
24
to deal with the complexity of a RDBMS or other sophisticated database. It is simple,
25
easy to use, cross-platform, and doesn't require any external C libraries, unlike
26
Python's built-in DBM modules.</p>
28
<pre class="python-interpreter" xml:space="preserve">
29
>>> from twisted.persisted import dirdbm
30
>>> d = dirdbm.DirDBM("/tmp/dir")
31
>>> d["librarian"] = "ook"
32
>>> d["librarian"]
36
>>> del d["librarian"]
37
>>> d.items()
41
<h2>dirdbm.Shelf<a name="auto1"/></h2>
43
<p>Sometimes it is neccessary to persist more complicated objects than strings.
44
With some care, <code class="API"><a href="http://twistedmatrix.com/documents/10.0.0/api/twisted.persisted.dirdbm.Shelf.html" title="twisted.persisted.dirdbm.Shelf">dirdbm.Shelf</a></code>
45
can transparently persist
46
them. <code>Shelf</code> works exactly like <code>DirDBM</code>, except that
47
the values (but not the keys) can be arbitrary picklable objects. However,
48
notice that mutating an object after it has been stored in the
49
<code>Shelf</code> has no effect on the Shelf.
50
When mutating objects, it is neccessary to explictly store them back in the <code>Shelf</code>
53
<pre class="python-interpreter" xml:space="preserve">
54
>>> from twisted.persisted import dirdbm
55
>>> d = dirdbm.Shelf("/tmp/dir2")
56
>>> d["key"] = [1, 2]
57
>>> d["key"]
59
>>> l = d["key"]
60
>>> l.append(3)
61
>>> d["key"]
63
>>> d["key"] = l
64
>>> d["key"]
74
<p><a href="index.html">Index</a></p>
75
<span class="version">Version: 10.0.0</span>
b'\\ No newline at end of file'