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