blob: 47f7ef67df3c8be17900ff8999e2334f31b1fe7d [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{shelve} ---
Thomas Woutersf8316632000-07-16 19:01:10 +00002 Python object persistence}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{standard}{shelve}
Thomas Woutersf8316632000-07-16 19:01:10 +00005\modulesynopsis{Python object persistence.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossumd1883581995-02-15 15:53:08 +00007
8A ``shelf'' is a persistent, dictionary-like object. The difference
9with ``dbm'' databases is that the values (not the keys!) in a shelf
10can be essentially arbitrary Python objects --- anything that the
Fred Drake01553701999-04-05 19:46:21 +000011\refmodule{pickle} module can handle. This includes most class
12instances, recursive data types, and objects containing lots of shared
Guido van Rossumd1883581995-02-15 15:53:08 +000013sub-objects. The keys are ordinary strings.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000014\refstmodindex{pickle}
Guido van Rossumd1883581995-02-15 15:53:08 +000015
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000016\begin{funcdesc}{open}{filename\optional{,flag='c'\optional{,protocol=\code{None}\optional{,writeback=\code{False}}}}}
Skip Montanaro1f7a2712003-01-21 01:52:39 +000017Open a persistent dictionary. The filename specified is the base filename
18for the underlying database. As a side-effect, an extension may be added to
19the filename and more than one file may be created. By default, the
20underlying database file is opened for reading and writing. The optional
21{}\var{flag} pararameter has the same interpretation as the \var{flag}
Martin v. Löwis153c9e42003-04-19 20:59:03 +000022parameter of \function{anydbm.open}.
23
24By default, version 0 pickles are used to serialize values.
25The version of the pickle protocol can be specified with the
26\var{protocol} parameter. \versionchanged[The \var{protocol}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000027parameter was added]{2.3}
Martin v. Löwis153c9e42003-04-19 20:59:03 +000028
29By default, mutations to persistent-dictionary mutable entries are not
30automatically written back. If the optional \var{writeback} parameter
31is set to {}\var{True}, all entries accessed are cached in memory, and
32written back at close time; this can make it handier to mutate mutable
33entries in the persistent dictionary, but, if many entries are
34accessed, it can consume vast amounts of memory for the cache, and it
35can make the close operation very slow since all accessed entries are
36written back (there is no way to determine which accessed entries are
37mutable, nor which ones were actually mutated).
38
Skip Montanaro190613c2003-01-21 01:38:47 +000039\end{funcdesc}
Guido van Rossumd1883581995-02-15 15:53:08 +000040
Skip Montanaro190613c2003-01-21 01:38:47 +000041Shelve objects support all methods supported by dictionaries. This eases
42the transition from dictionary based scripts to those requiring persistent
43storage.
Guido van Rossumd1883581995-02-15 15:53:08 +000044
Skip Montanaro190613c2003-01-21 01:38:47 +000045\subsection{Restrictions}
Guido van Rossum5680b951995-02-16 16:29:01 +000046
47\begin{itemize}
48
49\item
Fred Drake01553701999-04-05 19:46:21 +000050The choice of which database package will be used
Skip Montanaro190613c2003-01-21 01:38:47 +000051(such as \refmodule{dbm}, \refmodule{gdbm} or \refmodule{bsddb}) depends on
52which interface is available. Therefore it is not safe to open the database
53directly using \refmodule{dbm}. The database is also (unfortunately) subject
Fred Drake01553701999-04-05 19:46:21 +000054to the limitations of \refmodule{dbm}, if it is used --- this means
55that (the pickled representation of) the objects stored in the
56database should be fairly small, and in rare cases key collisions may
57cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000058\refbimodindex{dbm}
59\refbimodindex{gdbm}
Skip Montanaro190613c2003-01-21 01:38:47 +000060\refbimodindex{bsddb}
Guido van Rossum5680b951995-02-16 16:29:01 +000061
62\item
Skip Montanaro3bf99e32002-12-08 18:36:24 +000063Depending on the implementation, closing a persistent dictionary may
64or may not be necessary to flush changes to disk. The \method{__del__}
65method of the \class{Shelf} class calls the \method{close} method, so the
66programmer generally need not do this explicitly.
Guido van Rossumd1883581995-02-15 15:53:08 +000067
Guido van Rossum5680b951995-02-16 16:29:01 +000068\item
Fred Drake01553701999-04-05 19:46:21 +000069The \module{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000070access to shelved objects. (Multiple simultaneous read accesses are
71safe.) When a program has a shelf open for writing, no other program
72should have it open for reading or writing. \UNIX{} file locking can
73be used to solve this, but this differs across \UNIX{} versions and
74requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000075
76\end{itemize}
Fred Drakec8593501998-08-24 18:46:14 +000077
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000078\begin{classdesc}{Shelf}{dict\optional{, protocol=None\optional{, writeback=False}}}
Skip Montanaro3bf99e32002-12-08 18:36:24 +000079A subclass of \class{UserDict.DictMixin} which stores pickled values in the
Martin v. Löwis153c9e42003-04-19 20:59:03 +000080\var{dict} object.
81
82By default, version 0 pickles are used to serialize values. The
83version of the pickle protocol can be specified with the
84\var{protocol} parameter. See the \module{pickle} documentation for a
85discussion of the pickle protocols. \versionchanged[The \var{protocol}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000086parameter was added]{2.3}
Martin v. Löwis153c9e42003-04-19 20:59:03 +000087
88If the \var{writeback} parameter is \code{True}, the object will hold a
89cache of all entries accessed and write them back to the \var{dict} at
90sync and close times. This allows natural operations on mutable entries,
91but can consume much more memory and make sync and close take a long time.
Skip Montanaro3bf99e32002-12-08 18:36:24 +000092\end{classdesc}
93
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000094\begin{classdesc}{BsdDbShelf}{dict\optional{, protocol=None\optional{, writeback=False}}}
Martin v. Löwis153c9e42003-04-19 20:59:03 +000095
96A subclass of \class{Shelf} which exposes \method{first},
97\method{next}, \method{previous}, \method{last} and
98\method{set_location} which are available in the \module{bsddb} module
99but not in other database modules. The \var{dict} object passed to
100the constructor must support those methods. This is generally
101accomplished by calling one of \function{bsddb.hashopen},
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000102\function{bsddb.btopen} or \function{bsddb.rnopen}. The optional
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000103\var{protocol} and \var{writeback} parameters have the
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000104same interpretation as for the \class{Shelf} class.
105
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000106\end{classdesc}
107
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000108\begin{classdesc}{DbfilenameShelf}{filename\optional{, flag='c'\optional{, protocol=None\optional{, writeback=False}}}}
Skip Montanaro1f7a2712003-01-21 01:52:39 +0000109
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000110A subclass of \class{Shelf} which accepts a \var{filename} instead of
111a dict-like object. The underlying file will be opened using
112{}\function{anydbm.open}. By default, the file will be created and
113opened for both read and write. The optional \var{flag} parameter has
114the same interpretation as for the \function{open} function. The
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000115optional \var{protocol} and \var{writeback} parameters
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000116have the same interpretation as for the \class{Shelf} class.
117
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000118\end{classdesc}
Fred Drakec8593501998-08-24 18:46:14 +0000119
Skip Montanaro190613c2003-01-21 01:38:47 +0000120\subsection{Example}
121
122To summarize the interface (\code{key} is a string, \code{data} is an
123arbitrary object):
124
125\begin{verbatim}
126import shelve
127
128d = shelve.open(filename) # open -- file may get suffix added by low-level
129 # library
130
131d[key] = data # store data at key (overwrites old data if
132 # using an existing key)
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000133data = d[key] # retrieve a COPY of data at key (raise KeyError if no
Skip Montanaro190613c2003-01-21 01:38:47 +0000134 # such key)
135del d[key] # delete data stored at key (raises KeyError
136 # if no such key)
137flag = d.has_key(key) # true if the key exists
138list = d.keys() # a list of all existing keys (slow!)
139
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000140# as d was opened WITHOUT writeback=True, beware:
141d['xx'] = range(4) # this works as expected, but...
142d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!!!
143# having opened d without writeback=True, you need to code carefully:
144temp = d['xx'] # extracts the copy
145temp.append(5) # mutates the copy
146d['xx'] = temp # stores the copy right back, to persist it
147# or, d=shelve.open(filename,writeback=True) would let you just code
148# d['xx'].append(5) and have it work as expected, BUT it would also
149# consume more memory and make the d.close() operation slower.
150
Skip Montanaro190613c2003-01-21 01:38:47 +0000151d.close() # close it
152\end{verbatim}
153
Fred Drakec8593501998-08-24 18:46:14 +0000154\begin{seealso}
Fred Drake01553701999-04-05 19:46:21 +0000155 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000156 \seemodule{bsddb}{BSD \code{db} database interface.}
157 \seemodule{dbhash}{Thin layer around the \module{bsddb} which provides an
158 \function{open} function like the other database modules.}
Fred Drake01553701999-04-05 19:46:21 +0000159 \seemodule{dbm}{Standard \UNIX{} database interface.}
160 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
161 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
Fred Drakec8593501998-08-24 18:46:14 +0000162 \seemodule{pickle}{Object serialization used by \module{shelve}.}
Fred Drakeffbe6871999-04-22 21:23:22 +0000163 \seemodule{cPickle}{High-performance version of \refmodule{pickle}.}
Fred Drakec8593501998-08-24 18:46:14 +0000164\end{seealso}