Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / howto / dirdbm.html
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">
2   <head>
3 <title>Twisted Documentation: DirDBM: Directory-based Storage</title>
4 <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
5   </head>
6
7   <body bgcolor="white">
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>
10     <div class="content">
11 <span/>
12
13 <h2>dirdbm.DirDBM<a name="auto0"/></h2>
14
15 <p><code class="API"><a href="http://twistedmatrix.com/documents/12.1.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/12.1.0/api/twisted.persisted.dirdbm.DirDBM.html" title="twisted.persisted.dirdbm.DirDBM">DirDBM</a></code>
20 objects act just like Python dictionaries.</p>
21
22 <p><code class="API"><a href="http://twistedmatrix.com/documents/12.1.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>
27
28 <pre class="python-interpreter" xml:space="preserve">
29 &gt;&gt;&gt; from twisted.persisted import dirdbm
30 &gt;&gt;&gt; d = dirdbm.DirDBM(&quot;/tmp/dir&quot;)
31 &gt;&gt;&gt; d[&quot;librarian&quot;] = &quot;ook&quot;
32 &gt;&gt;&gt; d[&quot;librarian&quot;]        
33 'ook'
34 &gt;&gt;&gt; d.keys()
35 ['librarian']
36 &gt;&gt;&gt; del d[&quot;librarian&quot;]
37 &gt;&gt;&gt; d.items()
38 []
39 </pre>
40
41 <h2>dirdbm.Shelf<a name="auto1"/></h2>
42
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/12.1.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  <code>Shelf</code> has no effect on the Shelf.
49 When mutating objects, it is neccessary to explictly store them back in the <code>Shelf</code>
50 afterwards:</p>
51
52 <pre class="python-interpreter" xml:space="preserve">
53 &gt;&gt;&gt; from twisted.persisted import dirdbm
54 &gt;&gt;&gt; d = dirdbm.Shelf(&quot;/tmp/dir2&quot;)
55 &gt;&gt;&gt; d[&quot;key&quot;] = [1, 2]
56 &gt;&gt;&gt; d[&quot;key&quot;]
57 [1, 2]
58 &gt;&gt;&gt; l = d[&quot;key&quot;]
59 &gt;&gt;&gt; l.append(3)
60 &gt;&gt;&gt; d[&quot;key&quot;]
61 [1, 2]
62 &gt;&gt;&gt; d[&quot;key&quot;] = l
63 &gt;&gt;&gt; d[&quot;key&quot;]
64 [1, 2, 3]
65 </pre>
66
67
68
69
70
71   </div>
72
73     <p><a href="index.html">Index</a></p>
74     <span class="version">Version: 12.1.0</span>
75   </body>
76 </html>