Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="node76.html" /> | |
13 | <link rel="parent" href="module-shelve.html" /> | |
14 | <link rel="next" href="module-copy.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>3.17.2 Example</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="3.17.1 Restrictions" | |
24 | href="node76.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="3.17 shelve " | |
27 | href="module-shelve.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="3.18 copy " | |
30 | href="module-copy.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="node76.html">3.17.1 Restrictions</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-shelve.html">3.17 shelve </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-copy.html">3.18 copy </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0051720000000000000000"> | |
55 | 3.17.2 Example</A> | |
56 | </H2> | |
57 | ||
58 | <P> | |
59 | To summarize the interface (<code>key</code> is a string, <code>data</code> is an | |
60 | arbitrary object): | |
61 | ||
62 | <P> | |
63 | <div class="verbatim"><pre> | |
64 | import shelve | |
65 | ||
66 | d = shelve.open(filename) # open -- file may get suffix added by low-level | |
67 | # library | |
68 | ||
69 | d[key] = data # store data at key (overwrites old data if | |
70 | # using an existing key) | |
71 | data = d[key] # retrieve a COPY of data at key (raise KeyError if no | |
72 | # such key) | |
73 | del d[key] # delete data stored at key (raises KeyError | |
74 | # if no such key) | |
75 | flag = d.has_key(key) # true if the key exists | |
76 | list = d.keys() # a list of all existing keys (slow!) | |
77 | ||
78 | # as d was opened WITHOUT writeback=True, beware: | |
79 | d['xx'] = range(4) # this works as expected, but... | |
80 | d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!!! | |
81 | # having opened d without writeback=True, you need to code carefully: | |
82 | temp = d['xx'] # extracts the copy | |
83 | temp.append(5) # mutates the copy | |
84 | d['xx'] = temp # stores the copy right back, to persist it | |
85 | # or, d=shelve.open(filename,writeback=True) would let you just code | |
86 | # d['xx'].append(5) and have it work as expected, BUT it would also | |
87 | # consume more memory and make the d.close() operation slower. | |
88 | ||
89 | d.close() # close it | |
90 | </pre></div> | |
91 | ||
92 | <P> | |
93 | <div class="seealso"> | |
94 | <p class="heading">See Also:</p> | |
95 | ||
96 | <dl compact="compact" class="seemodule"> | |
97 | <dt>Module <b><tt class="module"><a href="module-anydbm.html">anydbm</a></tt>:</b> | |
98 | <dd>Generic interface to <code>dbm</code>-style databases. | |
99 | </dl> | |
100 | <dl compact="compact" class="seemodule"> | |
101 | <dt>Module <b><tt class="module"><a href="module-bsddb.html">bsddb</a></tt>:</b> | |
102 | <dd>BSD <code>db</code> database interface. | |
103 | </dl> | |
104 | <dl compact="compact" class="seemodule"> | |
105 | <dt>Module <b><tt class="module"><a href="module-dbhash.html">dbhash</a></tt>:</b> | |
106 | <dd>Thin layer around the <tt class="module">bsddb</tt> which provides an | |
107 | <tt class="function">open</tt> function like the other database modules. | |
108 | </dl> | |
109 | <dl compact="compact" class="seemodule"> | |
110 | <dt>Module <b><tt class="module"><a href="module-dbm.html">dbm</a></tt>:</b> | |
111 | <dd>Standard <span class="Unix">Unix</span> database interface. | |
112 | </dl> | |
113 | <dl compact="compact" class="seemodule"> | |
114 | <dt>Module <b><tt class="module"><a href="module-dumbdbm.html">dumbdbm</a></tt>:</b> | |
115 | <dd>Portable implementation of the <code>dbm</code> interface. | |
116 | </dl> | |
117 | <dl compact="compact" class="seemodule"> | |
118 | <dt>Module <b><tt class="module"><a href="module-gdbm.html">gdbm</a></tt>:</b> | |
119 | <dd>GNU database interface, based on the <code>dbm</code> interface. | |
120 | </dl> | |
121 | <dl compact="compact" class="seemodule"> | |
122 | <dt>Module <b><tt class="module"><a href="module-pickle.html">pickle</a></tt>:</b> | |
123 | <dd>Object serialization used by <tt class="module">shelve</tt>. | |
124 | </dl> | |
125 | <dl compact="compact" class="seemodule"> | |
126 | <dt>Module <b><tt class="module"><a href="module-cPickle.html">cPickle</a></tt>:</b> | |
127 | <dd>High-performance version of <tt class="module"><a href="module-pickle.html">pickle</a></tt>. | |
128 | </dl> | |
129 | </div> | |
130 | ||
131 | <DIV CLASS="navigation"> | |
132 | <div class='online-navigation'> | |
133 | <p></p><hr /> | |
134 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
135 | <tr> | |
136 | <td class='online-navigation'><a rel="prev" title="3.17.1 Restrictions" | |
137 | href="node76.html"><img src='../icons/previous.png' | |
138 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
139 | <td class='online-navigation'><a rel="parent" title="3.17 shelve " | |
140 | href="module-shelve.html"><img src='../icons/up.png' | |
141 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
142 | <td class='online-navigation'><a rel="next" title="3.18 copy " | |
143 | href="module-copy.html"><img src='../icons/next.png' | |
144 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
145 | <td align="center" width="100%">Python Library Reference</td> | |
146 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
147 | href="contents.html"><img src='../icons/contents.png' | |
148 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
149 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
150 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
151 | <td class='online-navigation'><a rel="index" title="Index" | |
152 | href="genindex.html"><img src='../icons/index.png' | |
153 | border='0' height='32' alt='Index' width='32' /></A></td> | |
154 | </tr></table> | |
155 | <div class='online-navigation'> | |
156 | <b class="navlabel">Previous:</b> | |
157 | <a class="sectref" rel="prev" href="node76.html">3.17.1 Restrictions</A> | |
158 | <b class="navlabel">Up:</b> | |
159 | <a class="sectref" rel="parent" href="module-shelve.html">3.17 shelve </A> | |
160 | <b class="navlabel">Next:</b> | |
161 | <a class="sectref" rel="next" href="module-copy.html">3.18 copy </A> | |
162 | </div> | |
163 | </div> | |
164 | <hr /> | |
165 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
166 | </DIV> | |
167 | <!--End of Navigation Panel--> | |
168 | <ADDRESS> | |
169 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
170 | </ADDRESS> | |
171 | </BODY> | |
172 | </HTML> |