blob: de12420137a7836923b26d09e3240ef73e401612 [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
Raymond Hettinger5d19b9a2011-11-04 14:05:52 -070047 Like file objects, shelve objects should be closed explicitly to ensure
48 that the persistent data is flushed to disk.
Georg Brandl304d3962009-09-17 16:15:53 +000049
Raymond Hettinger23ca12a2011-11-04 13:07:52 -070050 Since the :mod:`shelve` module stores objects using :mod:`pickle`, the same
51 security precautions apply. Accordingly, you should avoid loading a shelf
52 from an untrusted source.
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Georg Brandld8282ee2009-05-08 12:17:34 +000054Shelf objects support all methods supported by dictionaries. This eases the
Georg Brandl8ec7f652007-08-15 14:28:01 +000055transition from dictionary based scripts to those requiring persistent storage.
56
Georg Brandl304d3962009-09-17 16:15:53 +000057Two additional methods are supported:
Georg Brandl8ec7f652007-08-15 14:28:01 +000058
59.. method:: Shelf.sync()
60
Georg Brandl304d3962009-09-17 16:15:53 +000061 Write back all entries in the cache if the shelf was opened with *writeback*
62 set to :const:`True`. Also empty the cache and synchronize the persistent
63 dictionary on disk, if feasible. This is called automatically when the shelf
64 is closed with :meth:`close`.
65
66.. method:: Shelf.close()
67
68 Synchronize and close the persistent *dict* object. Operations on a closed
69 shelf will fail with a :exc:`ValueError`.
70
Georg Brandl8ec7f652007-08-15 14:28:01 +000071
Raymond Hettinger64906bb2009-04-04 05:37:47 +000072.. seealso::
73
74 `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_
75 with widely supported storage formats and having the speed of native
76 dictionaries.
77
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
79Restrictions
80------------
81
82 .. index::
83 module: dbm
84 module: gdbm
85 module: bsddb
86
87* The choice of which database package will be used (such as :mod:`dbm`,
88 :mod:`gdbm` or :mod:`bsddb`) depends on which interface is available. Therefore
89 it is not safe to open the database directly using :mod:`dbm`. The database is
90 also (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
91 this means that (the pickled representation of) the objects stored in the
92 database should be fairly small, and in rare cases key collisions may cause the
93 database to refuse updates.
94
Georg Brandl8ec7f652007-08-15 14:28:01 +000095* The :mod:`shelve` module does not support *concurrent* read/write access to
96 shelved objects. (Multiple simultaneous read accesses are safe.) When a
97 program has a shelf open for writing, no other program should have it open for
98 reading or writing. Unix file locking can be used to solve this, but this
99 differs across Unix versions and requires knowledge about the database
100 implementation used.
101
102
103.. class:: Shelf(dict[, protocol=None[, writeback=False]])
104
105 A subclass of :class:`UserDict.DictMixin` which stores pickled values in the
106 *dict* object.
107
108 By default, version 0 pickles are used to serialize values. The version of the
109 pickle protocol can be specified with the *protocol* parameter. See the
110 :mod:`pickle` documentation for a discussion of the pickle protocols.
111
112 .. versionchanged:: 2.3
113 The *protocol* parameter was added.
114
115 If the *writeback* parameter is ``True``, the object will hold a cache of all
116 entries accessed and write them back to the *dict* at sync and close times.
117 This allows natural operations on mutable entries, but can consume much more
118 memory and make sync and close take a long time.
119
120
121.. class:: BsdDbShelf(dict[, protocol=None[, writeback=False]])
122
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000123 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available in
125 the :mod:`bsddb` module but not in other database modules. The *dict* object
126 passed to the constructor must support those methods. This is generally
127 accomplished by calling one of :func:`bsddb.hashopen`, :func:`bsddb.btopen` or
128 :func:`bsddb.rnopen`. The optional *protocol* and *writeback* parameters have
129 the same interpretation as for the :class:`Shelf` class.
130
131
132.. class:: DbfilenameShelf(filename[, flag='c'[, protocol=None[, writeback=False]]])
133
134 A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
135 object. The underlying file will be opened using :func:`anydbm.open`. By
136 default, the file will be created and opened for both read and write. The
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000137 optional *flag* parameter has the same interpretation as for the :func:`.open`
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138 function. The optional *protocol* and *writeback* parameters have the same
139 interpretation as for the :class:`Shelf` class.
140
141
R. David Murray71189542009-05-12 01:36:57 +0000142.. _shelve-example:
143
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144Example
145-------
146
147To summarize the interface (``key`` is a string, ``data`` is an arbitrary
148object)::
149
150 import shelve
151
152 d = shelve.open(filename) # open -- file may get suffix added by low-level
153 # library
154
155 d[key] = data # store data at key (overwrites old data if
156 # using an existing key)
157 data = d[key] # retrieve a COPY of data at key (raise KeyError if no
158 # such key)
159 del d[key] # delete data stored at key (raises KeyError
160 # if no such key)
161 flag = d.has_key(key) # true if the key exists
162 klist = d.keys() # a list of all existing keys (slow!)
163
164 # as d was opened WITHOUT writeback=True, beware:
165 d['xx'] = range(4) # this works as expected, but...
Georg Brandl75f11072009-04-05 10:32:26 +0000166 d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!
Georg Brandl8ec7f652007-08-15 14:28:01 +0000167
168 # having opened d without writeback=True, you need to code carefully:
169 temp = d['xx'] # extracts the copy
170 temp.append(5) # mutates the copy
171 d['xx'] = temp # stores the copy right back, to persist it
172
173 # or, d=shelve.open(filename,writeback=True) would let you just code
174 # d['xx'].append(5) and have it work as expected, BUT it would also
175 # consume more memory and make the d.close() operation slower.
176
177 d.close() # close it
178
179
180.. seealso::
181
182 Module :mod:`anydbm`
183 Generic interface to ``dbm``\ -style databases.
184
185 Module :mod:`bsddb`
186 BSD ``db`` database interface.
187
188 Module :mod:`dbhash`
Georg Brandl9fa61bb2009-07-26 14:19:57 +0000189 Thin layer around the :mod:`bsddb` which provides an :func:`~dbhash.open`
190 function like the other database modules.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000191
192 Module :mod:`dbm`
193 Standard Unix database interface.
194
195 Module :mod:`dumbdbm`
196 Portable implementation of the ``dbm`` interface.
197
198 Module :mod:`gdbm`
199 GNU database interface, based on the ``dbm`` interface.
200
201 Module :mod:`pickle`
202 Object serialization used by :mod:`shelve`.
203
204 Module :mod:`cPickle`
205 High-performance version of :mod:`pickle`.
206