| 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="next" href="module-copy.html" /> |
| 13 | <link rel="prev" href="module-copyreg.html" /> |
| 14 | <link rel="parent" href="python.html" /> |
| 15 | <link rel="next" href="node76.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.17 shelve -- Python object persistence</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="3.16 copy_reg " |
| 25 | href="module-copyreg.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 28 | href="python.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="3.17.1 Restrictions" |
| 31 | href="node76.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-copyreg.html">3.16 copy_reg </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node76.html">3.17.1 Restrictions</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION0051700000000000000000"> |
| 56 | 3.17 <tt class="module">shelve</tt> -- |
| 57 | Python object persistence</A> |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | <A NAME="module-shelve"></A> |
| 62 | |
| 63 | <P> |
| 64 | A ``shelf'' is a persistent, dictionary-like object. The difference |
| 65 | with ``dbm'' databases is that the values (not the keys!) in a shelf |
| 66 | can be essentially arbitrary Python objects -- anything that the |
| 67 | <tt class="module"><a href="module-pickle.html">pickle</a></tt> module can handle. This includes most class |
| 68 | instances, recursive data types, and objects containing lots of shared |
| 69 | sub-objects. The keys are ordinary strings. |
| 70 | <a id='l2h-668' xml:id='l2h-668'></a> |
| 71 | |
| 72 | <P> |
| 73 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 74 | <td><nobr><b><tt id='l2h-666' xml:id='l2h-666' class="function">open</tt></b>(</nobr></td> |
| 75 | <td><var>filename</var><big>[</big><var>,flag='c'</var><big>[</big><var>,protocol=<code>None</code></var><big>[</big><var>,writeback=<code>False</code></var><big>[</big><var>,binary=<code>None</code></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 76 | <dd> |
| 77 | Open a persistent dictionary. The filename specified is the base filename |
| 78 | for the underlying database. As a side-effect, an extension may be added to |
| 79 | the filename and more than one file may be created. By default, the |
| 80 | underlying database file is opened for reading and writing. The optional |
| 81 | <var>flag</var> parameter has the same interpretation as the <var>flag</var> |
| 82 | parameter of <tt class="function">anydbm.open</tt>. |
| 83 | |
| 84 | <P> |
| 85 | By default, version 0 pickles are used to serialize values. |
| 86 | The version of the pickle protocol can be specified with the |
| 87 | <var>protocol</var> parameter. |
| 88 | <span class="versionnote">Changed in version 2.3: |
| 89 | The <var>protocol</var> |
| 90 | parameter was added. The <var>binary</var> parameter is deprecated |
| 91 | and provided for backwards compatibility only.</span> |
| 92 | |
| 93 | <P> |
| 94 | By default, mutations to persistent-dictionary mutable entries are not |
| 95 | automatically written back. If the optional <var>writeback</var> parameter |
| 96 | is set to <var>True</var>, all entries accessed are cached in memory, and |
| 97 | written back at close time; this can make it handier to mutate mutable |
| 98 | entries in the persistent dictionary, but, if many entries are |
| 99 | accessed, it can consume vast amounts of memory for the cache, and it |
| 100 | can make the close operation very slow since all accessed entries are |
| 101 | written back (there is no way to determine which accessed entries are |
| 102 | mutable, nor which ones were actually mutated). |
| 103 | |
| 104 | <P> |
| 105 | </dl> |
| 106 | |
| 107 | <P> |
| 108 | Shelve objects support all methods supported by dictionaries. This eases |
| 109 | the transition from dictionary based scripts to those requiring persistent |
| 110 | storage. |
| 111 | |
| 112 | <P> |
| 113 | One additional method is supported: |
| 114 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 115 | <td><nobr><b><tt id='l2h-667' xml:id='l2h-667' class="method">sync</tt></b>(</nobr></td> |
| 116 | <td><var></var>)</td></tr></table></dt> |
| 117 | <dd> |
| 118 | Write back all entries in the cache if the shelf was opened with |
| 119 | <var>writeback</var> set to <var>True</var>. Also empty the cache and synchronize |
| 120 | the persistent dictionary on disk, if feasible. This is called automatically |
| 121 | when the shelf is closed with <tt class="method">close()</tt>. |
| 122 | </dl> |
| 123 | |
| 124 | <P> |
| 125 | |
| 126 | <p><br /></p><hr class='online-navigation' /> |
| 127 | <div class='online-navigation'> |
| 128 | <!--Table of Child-Links--> |
| 129 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> |
| 130 | |
| 131 | <UL CLASS="ChildLinks"> |
| 132 | <LI><A href="node76.html">3.17.1 Restrictions</a> |
| 133 | <LI><A href="node77.html">3.17.2 Example</a> |
| 134 | </ul> |
| 135 | <!--End of Table of Child-Links--> |
| 136 | </div> |
| 137 | |
| 138 | <DIV CLASS="navigation"> |
| 139 | <div class='online-navigation'> |
| 140 | <p></p><hr /> |
| 141 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 142 | <tr> |
| 143 | <td class='online-navigation'><a rel="prev" title="3.16 copy_reg " |
| 144 | href="module-copyreg.html"><img src='../icons/previous.png' |
| 145 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 146 | <td class='online-navigation'><a rel="parent" title="3. Python Runtime Services" |
| 147 | href="python.html"><img src='../icons/up.png' |
| 148 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 149 | <td class='online-navigation'><a rel="next" title="3.17.1 Restrictions" |
| 150 | href="node76.html"><img src='../icons/next.png' |
| 151 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 152 | <td align="center" width="100%">Python Library Reference</td> |
| 153 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 154 | href="contents.html"><img src='../icons/contents.png' |
| 155 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 156 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 157 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 158 | <td class='online-navigation'><a rel="index" title="Index" |
| 159 | href="genindex.html"><img src='../icons/index.png' |
| 160 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 161 | </tr></table> |
| 162 | <div class='online-navigation'> |
| 163 | <b class="navlabel">Previous:</b> |
| 164 | <a class="sectref" rel="prev" href="module-copyreg.html">3.16 copy_reg </A> |
| 165 | <b class="navlabel">Up:</b> |
| 166 | <a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A> |
| 167 | <b class="navlabel">Next:</b> |
| 168 | <a class="sectref" rel="next" href="node76.html">3.17.1 Restrictions</A> |
| 169 | </div> |
| 170 | </div> |
| 171 | <hr /> |
| 172 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 173 | </DIV> |
| 174 | <!--End of Navigation Panel--> |
| 175 | <ADDRESS> |
| 176 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 177 | </ADDRESS> |
| 178 | </BODY> |
| 179 | </HTML> |