blob: c0bcb80f19090a1257ee9adfbb6c2b241ebf338a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`shelve` --- Python object persistence
2===========================================
3
4.. module:: shelve
5 :synopsis: Python object persistence.
6
7
8.. index:: module: pickle
9
10A "shelf" is a persistent, dictionary-like object. The difference with "dbm"
11databases is that the values (not the keys!) in a shelf can be essentially
12arbitrary Python objects --- anything that the :mod:`pickle` module can handle.
13This includes most class instances, recursive data types, and objects containing
14lots of shared sub-objects. The keys are ordinary strings.
15
16
17.. function:: open(filename[, flag='c'[, protocol=None[, writeback=False]]])
18
19 Open a persistent dictionary. The filename specified is the base filename for
20 the underlying database. As a side-effect, an extension may be added to the
21 filename and more than one file may be created. By default, the underlying
22 database file is opened for reading and writing. The optional *flag* parameter
23 has the same interpretation as the *flag* parameter of :func:`anydbm.open`.
24
25 By default, version 0 pickles are used to serialize values. The version of the
26 pickle protocol can be specified with the *protocol* parameter.
27
28 .. versionchanged:: 2.3
29 The *protocol* parameter was added.
30
R. David Murray71189542009-05-12 01:36:57 +000031 Because of Python semantics, a shelf cannot know when a mutable
32 persistent-dictionary entry is modified. By default modified objects are
R. David Murray7c29f072010-02-11 01:38:42 +000033 written *only* when assigned to the shelf (see :ref:`shelve-example`). If the
34 optional *writeback* parameter is set to *True*, all entries accessed are also
35 cached in memory, and written back on :meth:`~Shelf.sync` and
36 :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in
37 the persistent dictionary, but, if many entries are accessed, it can consume
38 vast amounts of memory for the cache, and it can make the close operation
39 very slow since all accessed entries are written back (there is no way to
40 determine which accessed entries are mutable, nor which ones were actually
41 mutated).
Georg Brandl304d3962009-09-17 16:15:53 +000042
43 .. note::
44
45 Do not rely on the shelf being closed automatically; always call
46 :meth:`close` explicitly when you don't need it any more, or use a
47 :keyword:`with` statement with :func:`contextlib.closing`.
48
Georg Brandl8ec7f652007-08-15 14:28:01 +000049
Georg Brandld8282ee2009-05-08 12:17:34 +000050Shelf objects support all methods supported by dictionaries. This eases the
Georg Brandl8ec7f652007-08-15 14:28:01 +000051transition from dictionary based scripts to those requiring persistent storage.
52
Georg Brandl304d3962009-09-17 16:15:53 +000053Two additional methods are supported:
Georg Brandl8ec7f652007-08-15 14:28:01 +000054
55.. method:: Shelf.sync()
56
Georg Brandl304d3962009-09-17 16:15:53 +000057 Write back all entries in the cache if the shelf was opened with *writeback*
58 set to :const:`True`. Also empty the cache and synchronize the persistent
59 dictionary on disk, if feasible. This is called automatically when the shelf
60 is closed with :meth:`close`.
61
62.. method:: Shelf.close()
63
64 Synchronize and close the persistent *dict* object. Operations on a closed
65 shelf will fail with a :exc:`ValueError`.
66
Georg Brandl8ec7f652007-08-15 14:28:01 +000067
Raymond Hettinger64906bb2009-04-04 05:37:47 +000068.. seealso::
69
70 `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_
71 with widely supported storage formats and having the speed of native
72 dictionaries.
73
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75Restrictions
76------------
77
78 .. index::
79 module: dbm
80 module: gdbm
81 module: bsddb
82
83* The choice of which database package will be used (such as :mod:`dbm`,
84 :mod:`gdbm` or :mod:`bsddb`) depends on which interface is available. Therefore
85 it is not safe to open the database directly using :mod:`dbm`. The database is
86 also (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
87 this means that (the pickled representation of) the objects stored in the
88 database should be fairly small, and in rare cases key collisions may cause the
89 database to refuse updates.
90
Georg Brandl8ec7f652007-08-15 14:28:01 +000091* The :mod:`shelve` module does not support *concurrent* read/write access to
92 shelved objects. (Multiple simultaneous read accesses are safe.) When a
93 program has a shelf open for writing, no other program should have it open for
94 reading or writing. Unix file locking can be used to solve this, but this
95 differs across Unix versions and requires knowledge about the database
96 implementation used.
97
98
99.. class:: Shelf(dict[, protocol=None[, writeback=False]])
100
101 A subclass of :class:`UserDict.DictMixin` which stores pickled values in the
102 *dict* object.
103
104 By default, version 0 pickles are used to serialize values. The version of the
105 pickle protocol can be specified with the *protocol* parameter. See the
106 :mod:`pickle` documentation for a discussion of the pickle protocols.
107
108 .. versionchanged:: 2.3
109 The *protocol* parameter was added.
110
111 If the *writeback* parameter is ``True``, the object will hold a cache of all
112 entries accessed and write them back to the *dict* at sync and close times.
113 This allows natural operations on mutable entries, but can consume much more
114 memory and make sync and close take a long time.
115
116
117.. class:: BsdDbShelf(dict[, protocol=None[, writeback=False]])
118
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000119 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available in
121 the :mod:`bsddb` module but not in other database modules. The *dict* object
122 passed to the constructor must support those methods. This is generally
123 accomplished by calling one of :func:`bsddb.hashopen`, :func:`bsddb.btopen` or
124 :func:`bsddb.rnopen`. The optional *protocol* and *writeback* parameters have
125 the same interpretation as for the :class:`Shelf` class.
126
127
128.. class:: DbfilenameShelf(filename[, flag='c'[, protocol=None[, writeback=False]]])
129
130 A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
131 object. The underlying file will be opened using :func:`anydbm.open`. By
132 default, the file will be created and opened for both read and write. The
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000133 optional *flag* parameter has the same interpretation as for the :func:`.open`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134 function. The optional *protocol* and *writeback* parameters have the same
135 interpretation as for the :class:`Shelf` class.
136
137
R. David Murray71189542009-05-12 01:36:57 +0000138.. _shelve-example:
139
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140Example
141-------
142
143To summarize the interface (``key`` is a string, ``data`` is an arbitrary
144object)::
145
146 import shelve
147
148 d = shelve.open(filename) # open -- file may get suffix added by low-level
149 # library
150
151 d[key] = data # store data at key (overwrites old data if
152 # using an existing key)
153 data = d[key] # retrieve a COPY of data at key (raise KeyError if no
154 # such key)
155 del d[key] # delete data stored at key (raises KeyError
156 # if no such key)
157 flag = d.has_key(key) # true if the key exists
158 klist = d.keys() # a list of all existing keys (slow!)
159
160 # as d was opened WITHOUT writeback=True, beware:
161 d['xx'] = range(4) # this works as expected, but...
Georg Brandl75f11072009-04-05 10:32:26 +0000162 d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
164 # having opened d without writeback=True, you need to code carefully:
165 temp = d['xx'] # extracts the copy
166 temp.append(5) # mutates the copy
167 d['xx'] = temp # stores the copy right back, to persist it
168
169 # or, d=shelve.open(filename,writeback=True) would let you just code
170 # d['xx'].append(5) and have it work as expected, BUT it would also
171 # consume more memory and make the d.close() operation slower.
172
173 d.close() # close it
174
175
176.. seealso::
177
178 Module :mod:`anydbm`
179 Generic interface to ``dbm``\ -style databases.
180
181 Module :mod:`bsddb`
182 BSD ``db`` database interface.
183
184 Module :mod:`dbhash`
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000185 Thin layer around the :mod:`bsddb` which provides an :func:`~dbhash.open`
186 function like the other database modules.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000187
188 Module :mod:`dbm`
189 Standard Unix database interface.
190
191 Module :mod:`dumbdbm`
192 Portable implementation of the ``dbm`` interface.
193
194 Module :mod:`gdbm`
195 GNU database interface, based on the ``dbm`` interface.
196
197 Module :mod:`pickle`
198 Object serialization used by :mod:`shelve`.
199
200 Module :mod:`cPickle`
201 High-performance version of :mod:`pickle`.
202