blob: b2818141ec8d8148f570bc511a3b7fc166279e68 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000023 has the same interpretation as the *flag* parameter of :func:`dbm.open`.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Raymond Hettinger85602262009-02-03 04:19:10 +000025 By default, version 3 pickles are used to serialize values. The version of the
Georg Brandl116aa622007-08-15 14:28:22 +000026 pickle protocol can be specified with the *protocol* parameter.
27
R. David Murrayff85bca2009-05-12 01:40:16 +000028 Because of Python semantics, a shelf cannot know when a mutable
29 persistent-dictionary entry is modified. By default modified objects are
R. David Murray733ecdc2010-02-11 03:06:51 +000030 written *only* when assigned to the shelf (see :ref:`shelve-example`). If the
31 optional *writeback* parameter is set to *True*, all entries accessed are also
32 cached in memory, and written back on :meth:`~Shelf.sync` and
33 :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in
34 the persistent dictionary, but, if many entries are accessed, it can consume
35 vast amounts of memory for the cache, and it can make the close operation
36 very slow since all accessed entries are written back (there is no way to
37 determine which accessed entries are mutable, nor which ones were actually
38 mutated).
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000039
40 .. note::
41
42 Do not rely on the shelf being closed automatically; always call
43 :meth:`close` explicitly when you don't need it any more, or use a
44 :keyword:`with` statement with :func:`contextlib.closing`.
45
Georg Brandlab32fec2010-11-26 08:49:15 +000046.. warning::
47
48 Because the :mod:`shelve` module is backed by :mod:`pickle`, it is insecure
49 to load a shelf from an untrusted source. Like with pickle, loading a shelf
50 can execute arbitrary code.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Benjamin Peterson25c95f12009-05-08 20:42:26 +000052Shelf objects support all methods supported by dictionaries. This eases the
Georg Brandl116aa622007-08-15 14:28:22 +000053transition from dictionary based scripts to those requiring persistent storage.
54
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000055Two additional methods are supported:
Georg Brandl116aa622007-08-15 14:28:22 +000056
57.. method:: Shelf.sync()
58
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000059 Write back all entries in the cache if the shelf was opened with *writeback*
60 set to :const:`True`. Also empty the cache and synchronize the persistent
61 dictionary on disk, if feasible. This is called automatically when the shelf
62 is closed with :meth:`close`.
63
64.. method:: Shelf.close()
65
66 Synchronize and close the persistent *dict* object. Operations on a closed
67 shelf will fail with a :exc:`ValueError`.
68
Georg Brandl116aa622007-08-15 14:28:22 +000069
Raymond Hettinger65c9eb22009-04-04 05:39:52 +000070.. seealso::
71
72 `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_
73 with widely supported storage formats and having the speed of native
74 dictionaries.
75
Georg Brandl116aa622007-08-15 14:28:22 +000076
77Restrictions
78------------
79
80 .. index::
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000081 module: dbm.ndbm
82 module: dbm.gnu
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000084* The choice of which database package will be used (such as :mod:`dbm.ndbm` or
85 :mod:`dbm.gnu`) depends on which interface is available. Therefore it is not
86 safe to open the database directly using :mod:`dbm`. The database is also
87 (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
Georg Brandl116aa622007-08-15 14:28:22 +000088 this means that (the pickled representation of) the objects stored in the
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000089 database should be fairly small, and in rare cases key collisions may cause
90 the database to refuse updates.
Georg Brandl116aa622007-08-15 14:28:22 +000091
Georg Brandl116aa622007-08-15 14:28:22 +000092* The :mod:`shelve` module does not support *concurrent* read/write access to
93 shelved objects. (Multiple simultaneous read accesses are safe.) When a
94 program has a shelf open for writing, no other program should have it open for
95 reading or writing. Unix file locking can be used to solve this, but this
96 differs across Unix versions and requires knowledge about the database
97 implementation used.
98
99
100.. class:: Shelf(dict[, protocol=None[, writeback=False]])
101
Georg Brandlc7723722008-05-26 17:47:11 +0000102 A subclass of :class:`collections.MutableMapping` which stores pickled values
103 in the *dict* object.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105 By default, version 0 pickles are used to serialize values. The version of the
106 pickle protocol can be specified with the *protocol* parameter. See the
107 :mod:`pickle` documentation for a discussion of the pickle protocols.
108
Georg Brandl116aa622007-08-15 14:28:22 +0000109 If the *writeback* parameter is ``True``, the object will hold a cache of all
110 entries accessed and write them back to the *dict* at sync and close times.
111 This allows natural operations on mutable entries, but can consume much more
112 memory and make sync and close take a long time.
113
114
115.. class:: BsdDbShelf(dict[, protocol=None[, writeback=False]])
116
Georg Brandlc5605df2009-08-13 08:26:44 +0000117 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
Georg Brandl1158a332009-06-04 09:30:30 +0000118 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available
119 in the third-party :mod:`bsddb` module from `pybsddb
120 <http://www.jcea.es/programacion/pybsddb.htm>`_ but not in other database
121 modules. The *dict* object passed to the constructor must support those
122 methods. This is generally accomplished by calling one of
123 :func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
124 optional *protocol* and *writeback* parameters have the same interpretation
125 as for the :class:`Shelf` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
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
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000131 object. The underlying file will be opened using :func:`dbm.open`. By
Georg Brandl116aa622007-08-15 14:28:22 +0000132 default, the file will be created and opened for both read and write. The
Georg Brandlc5605df2009-08-13 08:26:44 +0000133 optional *flag* parameter has the same interpretation as for the :func:`.open`
Georg Brandl116aa622007-08-15 14:28:22 +0000134 function. The optional *protocol* and *writeback* parameters have the same
135 interpretation as for the :class:`Shelf` class.
136
137
R. David Murrayff85bca2009-05-12 01:40:16 +0000138.. _shelve-example:
139
Georg Brandl116aa622007-08-15 14:28:22 +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)
Ezio Melotti2befd9a2009-09-13 08:08:32 +0000157 flag = key in d # true if the key exists
158 klist = list(d.keys()) # a list of all existing keys (slow!)
Georg Brandl116aa622007-08-15 14:28:22 +0000159
160 # as d was opened WITHOUT writeback=True, beware:
Antoine Pitrouddcacf32011-02-07 23:18:52 +0000161 d['xx'] = [0, 1, 2] # this works as expected, but...
162 d['xx'].append(3) # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000178 Module :mod:`dbm`
179 Generic interface to ``dbm``-style databases.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Georg Brandl116aa622007-08-15 14:28:22 +0000181 Module :mod:`pickle`
182 Object serialization used by :mod:`shelve`.
183