1
:mod:`shelve` --- Python object persistence
2
===========================================
5
:synopsis: Python object persistence.
8
.. index:: module: pickle
10
**Source code:** :source:`Lib/shelve.py`
14
A "shelf" is a persistent, dictionary-like object. The difference with "dbm"
15
databases is that the values (not the keys!) in a shelf can be essentially
16
arbitrary Python objects --- anything that the :mod:`pickle` module can handle.
17
This includes most class instances, recursive data types, and objects containing
18
lots of shared sub-objects. The keys are ordinary strings.
21
.. function:: open(filename, flag='c', protocol=None, writeback=False)
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:`dbm.open`.
29
By default, version 3 pickles are used to serialize values. The version of the
30
pickle protocol can be specified with the *protocol* parameter.
32
Because of Python semantics, a shelf cannot know when a mutable
33
persistent-dictionary entry is modified. By default modified objects are
34
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
46
Do not rely on the shelf being closed automatically; always call
47
:meth:`~Shelf.close` explicitly when you don't need it any more, or
48
use :func:`shelve.open` as a context manager::
50
with shelve.open('spam') as db:
55
Because the :mod:`shelve` module is backed by :mod:`pickle`, it is insecure
56
to load a shelf from an untrusted source. Like with pickle, loading a shelf
57
can execute arbitrary code.
59
Shelf objects support all methods supported by dictionaries. This eases the
60
transition from dictionary based scripts to those requiring persistent storage.
62
Two additional methods are supported:
64
.. method:: Shelf.sync()
66
Write back all entries in the cache if the shelf was opened with *writeback*
67
set to :const:`True`. Also empty the cache and synchronize the persistent
68
dictionary on disk, if feasible. This is called automatically when the shelf
69
is closed with :meth:`close`.
71
.. method:: Shelf.close()
73
Synchronize and close the persistent *dict* object. Operations on a closed
74
shelf will fail with a :exc:`ValueError`.
79
`Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_
80
with widely supported storage formats and having the speed of native
91
* The choice of which database package will be used (such as :mod:`dbm.ndbm` or
92
:mod:`dbm.gnu`) depends on which interface is available. Therefore it is not
93
safe to open the database directly using :mod:`dbm`. The database is also
94
(unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
95
this means that (the pickled representation of) the objects stored in the
96
database should be fairly small, and in rare cases key collisions may cause
97
the database to refuse updates.
99
* The :mod:`shelve` module does not support *concurrent* read/write access to
100
shelved objects. (Multiple simultaneous read accesses are safe.) When a
101
program has a shelf open for writing, no other program should have it open for
102
reading or writing. Unix file locking can be used to solve this, but this
103
differs across Unix versions and requires knowledge about the database
107
.. class:: Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
109
A subclass of :class:`collections.abc.MutableMapping` which stores pickled
110
values in the *dict* object.
112
By default, version 0 pickles are used to serialize values. The version of the
113
pickle protocol can be specified with the *protocol* parameter. See the
114
:mod:`pickle` documentation for a discussion of the pickle protocols.
116
If the *writeback* parameter is ``True``, the object will hold a cache of all
117
entries accessed and write them back to the *dict* at sync and close times.
118
This allows natural operations on mutable entries, but can consume much more
119
memory and make sync and close take a long time.
121
The *keyencoding* parameter is the encoding used to encode keys before they
122
are used with the underlying dict.
124
A :class:`Shelf` object can also be used as a context manager, in which
125
case it will be automatically closed when the :keyword:`with` block ends.
127
.. versionchanged:: 3.2
128
Added the *keyencoding* parameter; previously, keys were always encoded in
131
.. versionchanged:: 3.4
132
Added context manager support.
135
.. class:: BsdDbShelf(dict, protocol=None, writeback=False, keyencoding='utf-8')
137
A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
138
:meth:`previous`, :meth:`last` and :meth:`set_location` which are available
139
in the third-party :mod:`bsddb` module from `pybsddb
140
<http://www.jcea.es/programacion/pybsddb.htm>`_ but not in other database
141
modules. The *dict* object passed to the constructor must support those
142
methods. This is generally accomplished by calling one of
143
:func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
144
optional *protocol*, *writeback*, and *keyencoding* parameters have the same
145
interpretation as for the :class:`Shelf` class.
148
.. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False)
150
A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
151
object. The underlying file will be opened using :func:`dbm.open`. By
152
default, the file will be created and opened for both read and write. The
153
optional *flag* parameter has the same interpretation as for the :func:`.open`
154
function. The optional *protocol* and *writeback* parameters have the same
155
interpretation as for the :class:`Shelf` class.
163
To summarize the interface (``key`` is a string, ``data`` is an arbitrary
168
d = shelve.open(filename) # open -- file may get suffix added by low-level
171
d[key] = data # store data at key (overwrites old data if
172
# using an existing key)
173
data = d[key] # retrieve a COPY of data at key (raise KeyError if no
175
del d[key] # delete data stored at key (raises KeyError
177
flag = key in d # true if the key exists
178
klist = list(d.keys()) # a list of all existing keys (slow!)
180
# as d was opened WITHOUT writeback=True, beware:
181
d['xx'] = [0, 1, 2] # this works as expected, but...
182
d['xx'].append(3) # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!
184
# having opened d without writeback=True, you need to code carefully:
185
temp = d['xx'] # extracts the copy
186
temp.append(5) # mutates the copy
187
d['xx'] = temp # stores the copy right back, to persist it
189
# or, d=shelve.open(filename,writeback=True) would let you just code
190
# d['xx'].append(5) and have it work as expected, BUT it would also
191
# consume more memory and make the d.close() operation slower.
199
Generic interface to ``dbm``-style databases.
202
Object serialization used by :mod:`shelve`.