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