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