blob: 56cb40710a32b15ae1c6d2120167094c4436ce2c [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`shelve` --- Python object persistence
3===========================================
4
5.. module:: shelve
6 :synopsis: Python object persistence.
7
8
9.. index:: module: pickle
10
11A "shelf" is a persistent, dictionary-like object. The difference with "dbm"
12databases is that the values (not the keys!) in a shelf can be essentially
13arbitrary Python objects --- anything that the :mod:`pickle` module can handle.
14This includes most class instances, recursive data types, and objects containing
15lots of shared sub-objects. The keys are ordinary strings.
16
17
18.. function:: open(filename[, flag='c'[, protocol=None[, writeback=False]]])
19
20 Open a persistent dictionary. The filename specified is the base filename for
21 the underlying database. As a side-effect, an extension may be added to the
22 filename and more than one file may be created. By default, the underlying
23 database file is opened for reading and writing. The optional *flag* parameter
24 has the same interpretation as the *flag* parameter of :func:`anydbm.open`.
25
26 By default, version 0 pickles are used to serialize values. The version of the
27 pickle protocol can be specified with the *protocol* parameter.
28
29 .. versionchanged:: 2.3
30 The *protocol* parameter was added.
31
R. David Murray28d09d62009-05-12 01:38:25 +000032 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
35 the optional *writeback* parameter is set to *True*, all entries accessed
36 are cached in memory, and written back at close time; this can make it
37 handier to mutate mutable entries in the persistent dictionary, but, if
38 many entries are accessed, it can consume vast amounts of memory for the
39 cache, and it can make the close operation very slow since all accessed
40 entries are written back (there is no way to determine which accessed
Georg Brandl8ec7f652007-08-15 14:28:01 +000041 entries are mutable, nor which ones were actually mutated).
42
43Shelve objects support all methods supported by dictionaries. This eases the
44transition from dictionary based scripts to those requiring persistent storage.
45
46One additional method is supported:
47
48
49.. method:: Shelf.sync()
50
51 Write back all entries in the cache if the shelf was opened with *writeback* set
52 to *True*. Also empty the cache and synchronize the persistent dictionary on
53 disk, if feasible. This is called automatically when the shelf is closed with
54 :meth:`close`.
55
Raymond Hettinger8de95922009-04-04 05:39:34 +000056.. seealso::
57
58 `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_
59 with widely supported storage formats and having the speed of native
60 dictionaries.
61
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63Restrictions
64------------
65
66 .. index::
67 module: dbm
68 module: gdbm
69 module: bsddb
70
71* The choice of which database package will be used (such as :mod:`dbm`,
72 :mod:`gdbm` or :mod:`bsddb`) depends on which interface is available. Therefore
73 it is not safe to open the database directly using :mod:`dbm`. The database is
74 also (unfortunately) subject to the limitations of :mod:`dbm`, if it is used ---
75 this means that (the pickled representation of) the objects stored in the
76 database should be fairly small, and in rare cases key collisions may cause the
77 database to refuse updates.
78
79* Depending on the implementation, closing a persistent dictionary may or may
80 not be necessary to flush changes to disk. The :meth:`__del__` method of the
81 :class:`Shelf` class calls the :meth:`close` method, so the programmer generally
82 need not do this explicitly.
83
84* The :mod:`shelve` module does not support *concurrent* read/write access to
85 shelved objects. (Multiple simultaneous read accesses are safe.) When a
86 program has a shelf open for writing, no other program should have it open for
87 reading or writing. Unix file locking can be used to solve this, but this
88 differs across Unix versions and requires knowledge about the database
89 implementation used.
90
91
92.. class:: Shelf(dict[, protocol=None[, writeback=False]])
93
94 A subclass of :class:`UserDict.DictMixin` which stores pickled values in the
95 *dict* object.
96
97 By default, version 0 pickles are used to serialize values. The version of the
98 pickle protocol can be specified with the *protocol* parameter. See the
99 :mod:`pickle` documentation for a discussion of the pickle protocols.
100
101 .. versionchanged:: 2.3
102 The *protocol* parameter was added.
103
104 If the *writeback* parameter is ``True``, the object will hold a cache of all
105 entries accessed and write them back to the *dict* at sync and close times.
106 This allows natural operations on mutable entries, but can consume much more
107 memory and make sync and close take a long time.
108
109
110.. class:: BsdDbShelf(dict[, protocol=None[, writeback=False]])
111
112 A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`next`,
113 :meth:`previous`, :meth:`last` and :meth:`set_location` which are available in
114 the :mod:`bsddb` module but not in other database modules. The *dict* object
115 passed to the constructor must support those methods. This is generally
116 accomplished by calling one of :func:`bsddb.hashopen`, :func:`bsddb.btopen` or
117 :func:`bsddb.rnopen`. The optional *protocol* and *writeback* parameters have
118 the same interpretation as for the :class:`Shelf` class.
119
120
121.. class:: DbfilenameShelf(filename[, flag='c'[, protocol=None[, writeback=False]]])
122
123 A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like
124 object. The underlying file will be opened using :func:`anydbm.open`. By
125 default, the file will be created and opened for both read and write. The
126 optional *flag* parameter has the same interpretation as for the :func:`open`
127 function. The optional *protocol* and *writeback* parameters have the same
128 interpretation as for the :class:`Shelf` class.
129
130
R. David Murray28d09d62009-05-12 01:38:25 +0000131.. _shelve-example:
132
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133Example
134-------
135
136To summarize the interface (``key`` is a string, ``data`` is an arbitrary
137object)::
138
139 import shelve
140
141 d = shelve.open(filename) # open -- file may get suffix added by low-level
142 # library
143
144 d[key] = data # store data at key (overwrites old data if
145 # using an existing key)
146 data = d[key] # retrieve a COPY of data at key (raise KeyError if no
147 # such key)
148 del d[key] # delete data stored at key (raises KeyError
149 # if no such key)
150 flag = d.has_key(key) # true if the key exists
151 klist = d.keys() # a list of all existing keys (slow!)
152
153 # as d was opened WITHOUT writeback=True, beware:
154 d['xx'] = range(4) # this works as expected, but...
Georg Brandle9b91212009-04-05 21:26:31 +0000155 d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!
Georg Brandl8ec7f652007-08-15 14:28:01 +0000156
157 # having opened d without writeback=True, you need to code carefully:
158 temp = d['xx'] # extracts the copy
159 temp.append(5) # mutates the copy
160 d['xx'] = temp # stores the copy right back, to persist it
161
162 # or, d=shelve.open(filename,writeback=True) would let you just code
163 # d['xx'].append(5) and have it work as expected, BUT it would also
164 # consume more memory and make the d.close() operation slower.
165
166 d.close() # close it
167
168
169.. seealso::
170
171 Module :mod:`anydbm`
172 Generic interface to ``dbm``\ -style databases.
173
174 Module :mod:`bsddb`
175 BSD ``db`` database interface.
176
177 Module :mod:`dbhash`
178 Thin layer around the :mod:`bsddb` which provides an :func:`open` function like
179 the other database modules.
180
181 Module :mod:`dbm`
182 Standard Unix database interface.
183
184 Module :mod:`dumbdbm`
185 Portable implementation of the ``dbm`` interface.
186
187 Module :mod:`gdbm`
188 GNU database interface, based on the ``dbm`` interface.
189
190 Module :mod:`pickle`
191 Object serialization used by :mod:`shelve`.
192
193 Module :mod:`cPickle`
194 High-performance version of :mod:`pickle`.
195