blob: 9d7d5045e2031626b9e3ef0319bc66ba22a18dfc [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
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/shelve.py`
11
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl18244152009-09-02 20:34:52 +000021.. function:: open(filename, flag='c', protocol=None, writeback=False)
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000027 has the same interpretation as the *flag* parameter of :func:`dbm.open`.
Georg Brandl116aa622007-08-15 14:28:22 +000028
Raymond Hettinger85602262009-02-03 04:19:10 +000029 By default, version 3 pickles are used to serialize values. The version of the
Georg Brandl116aa622007-08-15 14:28:22 +000030 pickle protocol can be specified with the *protocol* parameter.
31
R. David Murrayff85bca2009-05-12 01:40:16 +000032 Because of Python semantics, a shelf cannot know when a mutable
33 persistent-dictionary entry is modified. By default modified objects are
R. David Murrayddb3ed02010-02-11 02:42:19 +000034 written *only* when assigned to the shelf (see :ref:`shelve-example`). If the
35 optional *writeback* parameter is set to *True*, all entries accessed are also
36 cached in memory, and written back on :meth:`~Shelf.sync` and
37 :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in
38 the persistent dictionary, but, if many entries are accessed, it can consume
39 vast amounts of memory for the cache, and it can make the close operation
40 very slow since all accessed entries are written back (there is no way to
41 determine which accessed entries are mutable, nor which ones were actually
42 mutated).
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000043
44 .. note::
45
46 Do not rely on the shelf being closed automatically; always call
47 :meth:`close` explicitly when you don't need it any more, or use a
48 :keyword:`with` statement with :func:`contextlib.closing`.
49
Georg Brandl7716ca62010-10-17 09:37:54 +000050.. 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 Brandl116aa622007-08-15 14:28:22 +000055
Benjamin Peterson25c95f12009-05-08 20:42:26 +000056Shelf objects support all methods supported by dictionaries. This eases the
Georg Brandl116aa622007-08-15 14:28:22 +000057transition from dictionary based scripts to those requiring persistent storage.
58
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000059Two additional methods are supported:
Georg Brandl116aa622007-08-15 14:28:22 +000060
61.. method:: Shelf.sync()
62
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +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 Brandl116aa622007-08-15 14:28:22 +000073
Raymond Hettinger65c9eb22009-04-04 05:39:52 +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 Brandl116aa622007-08-15 14:28:22 +000080
81Restrictions
82------------
83
84 .. index::
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000085 module: dbm.ndbm
86 module: dbm.gnu
Georg Brandl116aa622007-08-15 14:28:22 +000087
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000088* The choice of which database package will be used (such as :mod:`dbm.ndbm` or
89 :mod:`dbm.gnu`) depends on which interface is available. Therefore it is not
90 safe to open the database directly using :mod:`dbm`. The database is also
91 (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
Georg Brandl116aa622007-08-15 14:28:22 +000092 this means that (the pickled representation of) the objects stored in the
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000093 database should be fairly small, and in rare cases key collisions may cause
94 the database to refuse updates.
Georg Brandl116aa622007-08-15 14:28:22 +000095
Georg Brandl116aa622007-08-15 14:28:22 +000096* The :mod:`shelve` module does not support *concurrent* read/write access to
97 shelved objects. (Multiple simultaneous read accesses are safe.) When a
98 program has a shelf open for writing, no other program should have it open for
99 reading or writing. Unix file locking can be used to solve this, but this
100 differs across Unix versions and requires knowledge about the database
101 implementation used.
102
103
Georg Brandl732324a2010-12-04 11:12:43 +0000104.. class:: Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Georg Brandlc7723722008-05-26 17:47:11 +0000106 A subclass of :class:`collections.MutableMapping` which stores pickled values
107 in the *dict* object.
Georg Brandl116aa622007-08-15 14:28:22 +0000108
109 By default, version 0 pickles are used to serialize values. The version of the
110 pickle protocol can be specified with the *protocol* parameter. See the
111 :mod:`pickle` documentation for a discussion of the pickle protocols.
112
Georg Brandl116aa622007-08-15 14:28:22 +0000113 If the *writeback* parameter is ``True``, the object will hold a cache of all
114 entries accessed and write them back to the *dict* at sync and close times.
115 This allows natural operations on mutable entries, but can consume much more
116 memory and make sync and close take a long time.
117
Georg Brandl732324a2010-12-04 11:12:43 +0000118 The *keyencoding* parameter is the encoding used to encode keys before they
119 are used with the underlying dict.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Georg Brandl732324a2010-12-04 11:12:43 +0000121 .. versionadded:: 3.2
122 The *keyencoding* parameter; previously, keys were always encoded in
123 UTF-8.
124
125
126.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Georg Brandl502d9a52009-07-26 15:02:41 +0000128 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
Georg Brandl1158a332009-06-04 09:30:30 +0000129 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available
130 in the third-party :mod:`bsddb` module from `pybsddb
131 <http://www.jcea.es/programacion/pybsddb.htm>`_ but not in other database
132 modules. The *dict* object passed to the constructor must support those
133 methods. This is generally accomplished by calling one of
134 :func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
Georg Brandl732324a2010-12-04 11:12:43 +0000135 optional *protocol*, *writeback*, and *keyencoding* parameters have the same
136 interpretation as for the :class:`Shelf` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
Georg Brandl18244152009-09-02 20:34:52 +0000139.. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141 A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000142 object. The underlying file will be opened using :func:`dbm.open`. By
Georg Brandl116aa622007-08-15 14:28:22 +0000143 default, the file will be created and opened for both read and write. The
Georg Brandl502d9a52009-07-26 15:02:41 +0000144 optional *flag* parameter has the same interpretation as for the :func:`.open`
Georg Brandl116aa622007-08-15 14:28:22 +0000145 function. The optional *protocol* and *writeback* parameters have the same
146 interpretation as for the :class:`Shelf` class.
147
148
R. David Murrayff85bca2009-05-12 01:40:16 +0000149.. _shelve-example:
150
Georg Brandl116aa622007-08-15 14:28:22 +0000151Example
152-------
153
154To summarize the interface (``key`` is a string, ``data`` is an arbitrary
155object)::
156
157 import shelve
158
159 d = shelve.open(filename) # open -- file may get suffix added by low-level
160 # library
161
162 d[key] = data # store data at key (overwrites old data if
163 # using an existing key)
164 data = d[key] # retrieve a COPY of data at key (raise KeyError if no
165 # such key)
166 del d[key] # delete data stored at key (raises KeyError
167 # if no such key)
Ezio Melotti8f7649e2009-09-13 04:48:45 +0000168 flag = key in d # true if the key exists
169 klist = list(d.keys()) # a list of all existing keys (slow!)
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171 # as d was opened WITHOUT writeback=True, beware:
Antoine Pitrou631507d2011-02-07 23:10:33 +0000172 d['xx'] = [0, 1, 2] # this works as expected, but...
173 d['xx'].append(3) # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175 # having opened d without writeback=True, you need to code carefully:
176 temp = d['xx'] # extracts the copy
177 temp.append(5) # mutates the copy
178 d['xx'] = temp # stores the copy right back, to persist it
179
180 # or, d=shelve.open(filename,writeback=True) would let you just code
181 # d['xx'].append(5) and have it work as expected, BUT it would also
182 # consume more memory and make the d.close() operation slower.
183
184 d.close() # close it
185
186
187.. seealso::
188
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000189 Module :mod:`dbm`
190 Generic interface to ``dbm``-style databases.
Georg Brandl116aa622007-08-15 14:28:22 +0000191
Georg Brandl116aa622007-08-15 14:28:22 +0000192 Module :mod:`pickle`
193 Object serialization used by :mod:`shelve`.
194