blob: e3862903f6076ecff3f68452911489c8c3a67076 [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
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +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
32 cached in memory, and written back on :meth:`sync` and :meth:`close`; this
33 can make it handier to mutate mutable entries in the persistent dictionary,
34 but, if many entries are accessed, it can consume vast amounts of memory for
35 the cache, and it can make the close operation very slow since all accessed
36 entries are written back (there is no way to determine which accessed entries
37 are mutable, nor which ones were actually mutated).
38
39 .. note::
40
41 Do not rely on the shelf being closed automatically; always call
42 :meth:`close` explicitly when you don't need it any more, or use a
43 :keyword:`with` statement with :func:`contextlib.closing`.
44
Georg Brandl116aa622007-08-15 14:28:22 +000045
Benjamin Peterson25c95f12009-05-08 20:42:26 +000046Shelf objects support all methods supported by dictionaries. This eases the
Georg Brandl116aa622007-08-15 14:28:22 +000047transition from dictionary based scripts to those requiring persistent storage.
48
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000049Two additional methods are supported:
Georg Brandl116aa622007-08-15 14:28:22 +000050
51.. method:: Shelf.sync()
52
Benjamin Petersonf3d7dbe2009-10-04 14:54:52 +000053 Write back all entries in the cache if the shelf was opened with *writeback*
54 set to :const:`True`. Also empty the cache and synchronize the persistent
55 dictionary on disk, if feasible. This is called automatically when the shelf
56 is closed with :meth:`close`.
57
58.. method:: Shelf.close()
59
60 Synchronize and close the persistent *dict* object. Operations on a closed
61 shelf will fail with a :exc:`ValueError`.
62
Georg Brandl116aa622007-08-15 14:28:22 +000063
Raymond Hettinger65c9eb22009-04-04 05:39:52 +000064.. seealso::
65
66 `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_
67 with widely supported storage formats and having the speed of native
68 dictionaries.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070
71Restrictions
72------------
73
74 .. index::
Georg Brandl0a7ac7d2008-05-26 10:29:35 +000075 module: dbm.ndbm
76 module: dbm.gnu
Georg Brandl116aa622007-08-15 14:28:22 +000077
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000078* The choice of which database package will be used (such as :mod:`dbm.ndbm` or
79 :mod:`dbm.gnu`) depends on which interface is available. Therefore it is not
80 safe to open the database directly using :mod:`dbm`. The database is also
81 (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
Georg Brandl116aa622007-08-15 14:28:22 +000082 this means that (the pickled representation of) the objects stored in the
Benjamin Peterson9a46cab2008-09-08 02:49:30 +000083 database should be fairly small, and in rare cases key collisions may cause
84 the database to refuse updates.
Georg Brandl116aa622007-08-15 14:28:22 +000085
Georg Brandl116aa622007-08-15 14:28:22 +000086* The :mod:`shelve` module does not support *concurrent* read/write access to
87 shelved objects. (Multiple simultaneous read accesses are safe.) When a
88 program has a shelf open for writing, no other program should have it open for
89 reading or writing. Unix file locking can be used to solve this, but this
90 differs across Unix versions and requires knowledge about the database
91 implementation used.
92
93
94.. class:: Shelf(dict[, protocol=None[, writeback=False]])
95
Georg Brandlc7723722008-05-26 17:47:11 +000096 A subclass of :class:`collections.MutableMapping` which stores pickled values
97 in the *dict* object.
Georg Brandl116aa622007-08-15 14:28:22 +000098
99 By default, version 0 pickles are used to serialize values. The version of the
100 pickle protocol can be specified with the *protocol* parameter. See the
101 :mod:`pickle` documentation for a discussion of the pickle protocols.
102
Georg Brandl116aa622007-08-15 14:28:22 +0000103 If the *writeback* parameter is ``True``, the object will hold a cache of all
104 entries accessed and write them back to the *dict* at sync and close times.
105 This allows natural operations on mutable entries, but can consume much more
106 memory and make sync and close take a long time.
107
108
109.. class:: BsdDbShelf(dict[, protocol=None[, writeback=False]])
110
Georg Brandlc5605df2009-08-13 08:26:44 +0000111 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`,
Georg Brandl1158a332009-06-04 09:30:30 +0000112 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available
113 in the third-party :mod:`bsddb` module from `pybsddb
114 <http://www.jcea.es/programacion/pybsddb.htm>`_ but not in other database
115 modules. The *dict* object passed to the constructor must support those
116 methods. This is generally accomplished by calling one of
117 :func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The
118 optional *protocol* and *writeback* parameters have the same interpretation
119 as for the :class:`Shelf` class.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
122.. class:: DbfilenameShelf(filename[, flag='c'[, protocol=None[, writeback=False]]])
123
124 A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000125 object. The underlying file will be opened using :func:`dbm.open`. By
Georg Brandl116aa622007-08-15 14:28:22 +0000126 default, the file will be created and opened for both read and write. The
Georg Brandlc5605df2009-08-13 08:26:44 +0000127 optional *flag* parameter has the same interpretation as for the :func:`.open`
Georg Brandl116aa622007-08-15 14:28:22 +0000128 function. The optional *protocol* and *writeback* parameters have the same
129 interpretation as for the :class:`Shelf` class.
130
131
R. David Murrayff85bca2009-05-12 01:40:16 +0000132.. _shelve-example:
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134Example
135-------
136
137To summarize the interface (``key`` is a string, ``data`` is an arbitrary
138object)::
139
140 import shelve
141
142 d = shelve.open(filename) # open -- file may get suffix added by low-level
143 # library
144
145 d[key] = data # store data at key (overwrites old data if
146 # using an existing key)
147 data = d[key] # retrieve a COPY of data at key (raise KeyError if no
148 # such key)
149 del d[key] # delete data stored at key (raises KeyError
150 # if no such key)
Ezio Melotti2befd9a2009-09-13 08:08:32 +0000151 flag = key in d # true if the key exists
152 klist = list(d.keys()) # a list of all existing keys (slow!)
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154 # as d was opened WITHOUT writeback=True, beware:
155 d['xx'] = range(4) # this works as expected, but...
Benjamin Petersond23f8222009-04-05 19:13:16 +0000156 d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158 # having opened d without writeback=True, you need to code carefully:
159 temp = d['xx'] # extracts the copy
160 temp.append(5) # mutates the copy
161 d['xx'] = temp # stores the copy right back, to persist it
162
163 # or, d=shelve.open(filename,writeback=True) would let you just code
164 # d['xx'].append(5) and have it work as expected, BUT it would also
165 # consume more memory and make the d.close() operation slower.
166
167 d.close() # close it
168
169
170.. seealso::
171
Georg Brandl0a7ac7d2008-05-26 10:29:35 +0000172 Module :mod:`dbm`
173 Generic interface to ``dbm``-style databases.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
Georg Brandl116aa622007-08-15 14:28:22 +0000175 Module :mod:`pickle`
176 Object serialization used by :mod:`shelve`.
177
178 Module :mod:`cPickle`
179 High-performance version of :mod:`pickle`.
180