blob: 499ab5181a41a2f04e4b318141ec21358ee39896 [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
Georg Brandl116aa622007-08-15 14:28:22 +000012A "shelf" is a persistent, dictionary-like object. The difference with "dbm"
13databases is that the values (not the keys!) in a shelf can be essentially
14arbitrary Python objects --- anything that the :mod:`pickle` module can handle.
15This includes most class instances, recursive data types, and objects containing
16lots of shared sub-objects. The keys are ordinary strings.
17
18
Georg Brandl18244152009-09-02 20:34:52 +000019.. function:: open(filename, flag='c', protocol=None, writeback=False)
Georg Brandl116aa622007-08-15 14:28:22 +000020
21 Open a persistent dictionary. The filename specified is the base filename for
22 the underlying database. As a side-effect, an extension may be added to the
23 filename and more than one file may be created. By default, the underlying
24 database file is opened for reading and writing. The optional *flag* parameter
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000025 has the same interpretation as the *flag* parameter of :func:`dbm.open`.
Georg Brandl116aa622007-08-15 14:28:22 +000026
Raymond Hettinger85602262009-02-03 04:19:10 +000027 By default, version 3 pickles are used to serialize values. The version of the
Georg Brandl116aa622007-08-15 14:28:22 +000028 pickle protocol can be specified with the *protocol* parameter.
29
R. David Murrayff85bca2009-05-12 01:40:16 +000030 Because of Python semantics, a shelf cannot know when a mutable
31 persistent-dictionary entry is modified. By default modified objects are
R. David Murrayddb3ed02010-02-11 02:42:19 +000032 written *only* when assigned to the shelf (see :ref:`shelve-example`). If the
33 optional *writeback* parameter is set to *True*, all entries accessed are also
34 cached in memory, and written back on :meth:`~Shelf.sync` and
35 :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in
36 the persistent dictionary, but, if many entries are accessed, it can consume
37 vast amounts of memory for the cache, and it can make the close operation
38 very slow since all accessed entries are written back (there is no way to
39 determine which accessed entries are mutable, nor which ones were actually
40 mutated).
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000041
42 .. note::
43
44 Do not rely on the shelf being closed automatically; always call
45 :meth:`close` explicitly when you don't need it any more, or use a
46 :keyword:`with` statement with :func:`contextlib.closing`.
47
Georg Brandl7716ca62010-10-17 09:37:54 +000048.. warning::
49
50 Because the :mod:`shelve` module is backed by :mod:`pickle`, it is insecure
51 to load a shelf from an untrusted source. Like with pickle, loading a shelf
52 can execute arbitrary code.
Georg Brandl116aa622007-08-15 14:28:22 +000053
Benjamin Peterson25c95f12009-05-08 20:42:26 +000054Shelf objects support all methods supported by dictionaries. This eases the
Georg Brandl116aa622007-08-15 14:28:22 +000055transition from dictionary based scripts to those requiring persistent storage.
56
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +000057Two additional methods are supported:
Georg Brandl116aa622007-08-15 14:28:22 +000058
59.. method:: Shelf.sync()
60
Benjamin Peterson4ac9ce42009-10-04 14:49:41 +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 Brandl116aa622007-08-15 14:28:22 +000071
Raymond Hettinger65c9eb22009-04-04 05:39:52 +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 Brandl116aa622007-08-15 14:28:22 +000078
79Restrictions
80------------
81
82 .. index::
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000083 module: dbm.ndbm
84 module: dbm.gnu
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000086* The choice of which database package will be used (such as :mod:`dbm.ndbm` or
87 :mod:`dbm.gnu`) depends on which interface is available. Therefore it is not
88 safe to open the database directly using :mod:`dbm`. The database is also
89 (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
Georg Brandl116aa622007-08-15 14:28:22 +000090 this means that (the pickled representation of) the objects stored in the
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000091 database should be fairly small, and in rare cases key collisions may cause
92 the database to refuse updates.
Georg Brandl116aa622007-08-15 14:28:22 +000093
Georg Brandl116aa622007-08-15 14:28:22 +000094* The :mod:`shelve` module does not support *concurrent* read/write access to
95 shelved objects. (Multiple simultaneous read accesses are safe.) When a
96 program has a shelf open for writing, no other program should have it open for
97 reading or writing. Unix file locking can be used to solve this, but this
98 differs across Unix versions and requires knowledge about the database
99 implementation used.
100
101
Georg Brandl732324a2010-12-04 11:12:43 +0000102.. class:: Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Georg Brandlc7723722008-05-26 17:47:11 +0000104 A subclass of :class:`collections.MutableMapping` which stores pickled values
105 in the *dict* object.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107 By default, version 0 pickles are used to serialize values. The version of the
108 pickle protocol can be specified with the *protocol* parameter. See the
109 :mod:`pickle` documentation for a discussion of the pickle protocols.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111 If the *writeback* parameter is ``True``, the object will hold a cache of all
112 entries accessed and write them back to the *dict* at sync and close times.
113 This allows natural operations on mutable entries, but can consume much more
114 memory and make sync and close take a long time.
115
Georg Brandl732324a2010-12-04 11:12:43 +0000116 The *keyencoding* parameter is the encoding used to encode keys before they
117 are used with the underlying dict.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
Georg Brandl732324a2010-12-04 11:12:43 +0000119 .. versionadded:: 3.2
120 The *keyencoding* parameter; previously, keys were always encoded in
121 UTF-8.
122
123
124.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
Georg Brandl116aa622007-08-15 14:28:22 +0000125
Georg Brandl502d9a52009-07-26 15:02:41 +0000126 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
Georg Brandl1158a332009-06-04 09:30:30 +0000127 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available
128 in the third-party :mod:`bsddb` module from `pybsddb
129 <http://www.jcea.es/programacion/pybsddb.htm>`_ but not in other database
130 modules. The *dict* object passed to the constructor must support those
131 methods. This is generally accomplished by calling one of
132 :func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
Georg Brandl732324a2010-12-04 11:12:43 +0000133 optional *protocol*, *writeback*, and *keyencoding* parameters have the same
134 interpretation as for the :class:`Shelf` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136
Georg Brandl18244152009-09-02 20:34:52 +0000137.. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139 A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000140 object. The underlying file will be opened using :func:`dbm.open`. By
Georg Brandl116aa622007-08-15 14:28:22 +0000141 default, the file will be created and opened for both read and write. The
Georg Brandl502d9a52009-07-26 15:02:41 +0000142 optional *flag* parameter has the same interpretation as for the :func:`.open`
Georg Brandl116aa622007-08-15 14:28:22 +0000143 function. The optional *protocol* and *writeback* parameters have the same
144 interpretation as for the :class:`Shelf` class.
145
146
R. David Murrayff85bca2009-05-12 01:40:16 +0000147.. _shelve-example:
148
Georg Brandl116aa622007-08-15 14:28:22 +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)
Ezio Melotti8f7649e2009-09-13 04:48:45 +0000166 flag = key in d # true if the key exists
167 klist = list(d.keys()) # a list of all existing keys (slow!)
Georg Brandl116aa622007-08-15 14:28:22 +0000168
169 # as d was opened WITHOUT writeback=True, beware:
170 d['xx'] = range(4) # this works as expected, but...
Benjamin Petersond23f8222009-04-05 19:13:16 +0000171 d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000187 Module :mod:`dbm`
188 Generic interface to ``dbm``-style databases.
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Georg Brandl116aa622007-08-15 14:28:22 +0000190 Module :mod:`pickle`
191 Object serialization used by :mod:`shelve`.
192