blob: 17ef3e50926a43bdd29215629dc8e58141dbbe0e [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
Martin v. Löwis153c9e42003-04-19 20:59:03 +000016\begin{funcdesc}{open}{filename\optional{,flag='c'\optional{,protocol=\code{None}\optional{,writeback=\code{False}\optional{,binary=\code{None}}}}}}
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}
27parameter was added. The \var{binary} parameter is deprecated
28and provided for backwards compatibility only]{2.3}
29
30By default, mutations to persistent-dictionary mutable entries are not
31automatically written back. If the optional \var{writeback} parameter
32is set to {}\var{True}, all entries accessed are cached in memory, and
33written back at close time; this can make it handier to mutate mutable
34entries in the persistent dictionary, but, if many entries are
35accessed, it can consume vast amounts of memory for the cache, and it
36can make the close operation very slow since all accessed entries are
37written back (there is no way to determine which accessed entries are
38mutable, nor which ones were actually mutated).
39
Skip Montanaro190613c2003-01-21 01:38:47 +000040\end{funcdesc}
Guido van Rossumd1883581995-02-15 15:53:08 +000041
Skip Montanaro190613c2003-01-21 01:38:47 +000042Shelve objects support all methods supported by dictionaries. This eases
43the transition from dictionary based scripts to those requiring persistent
44storage.
Guido van Rossumd1883581995-02-15 15:53:08 +000045
Skip Montanaro190613c2003-01-21 01:38:47 +000046\subsection{Restrictions}
Guido van Rossum5680b951995-02-16 16:29:01 +000047
48\begin{itemize}
49
50\item
Fred Drake01553701999-04-05 19:46:21 +000051The choice of which database package will be used
Skip Montanaro190613c2003-01-21 01:38:47 +000052(such as \refmodule{dbm}, \refmodule{gdbm} or \refmodule{bsddb}) depends on
53which interface is available. Therefore it is not safe to open the database
54directly using \refmodule{dbm}. The database is also (unfortunately) subject
Fred Drake01553701999-04-05 19:46:21 +000055to the limitations of \refmodule{dbm}, if it is used --- this means
56that (the pickled representation of) the objects stored in the
57database should be fairly small, and in rare cases key collisions may
58cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000059\refbimodindex{dbm}
60\refbimodindex{gdbm}
Skip Montanaro190613c2003-01-21 01:38:47 +000061\refbimodindex{bsddb}
Guido van Rossum5680b951995-02-16 16:29:01 +000062
63\item
Skip Montanaro3bf99e32002-12-08 18:36:24 +000064Depending on the implementation, closing a persistent dictionary may
65or may not be necessary to flush changes to disk. The \method{__del__}
66method of the \class{Shelf} class calls the \method{close} method, so the
67programmer generally need not do this explicitly.
Guido van Rossumd1883581995-02-15 15:53:08 +000068
Guido van Rossum5680b951995-02-16 16:29:01 +000069\item
Fred Drake01553701999-04-05 19:46:21 +000070The \module{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000071access to shelved objects. (Multiple simultaneous read accesses are
72safe.) When a program has a shelf open for writing, no other program
73should have it open for reading or writing. \UNIX{} file locking can
74be used to solve this, but this differs across \UNIX{} versions and
75requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000076
77\end{itemize}
Fred Drakec8593501998-08-24 18:46:14 +000078
Martin v. Löwis153c9e42003-04-19 20:59:03 +000079\begin{classdesc}{Shelf}{dict\optional{, protocol=None\optional{, writeback=False\optional{, binary=None}}}}
Skip Montanaro3bf99e32002-12-08 18:36:24 +000080A subclass of \class{UserDict.DictMixin} which stores pickled values in the
Martin v. Löwis153c9e42003-04-19 20:59:03 +000081\var{dict} object.
82
83By default, version 0 pickles are used to serialize values. The
84version of the pickle protocol can be specified with the
85\var{protocol} parameter. See the \module{pickle} documentation for a
86discussion of the pickle protocols. \versionchanged[The \var{protocol}
87parameter was added. The \var{binary} parameter is deprecated and
88provided for backwards compatibility only]{2.3}
89
90If the \var{writeback} parameter is \code{True}, the object will hold a
91cache of all entries accessed and write them back to the \var{dict} at
92sync and close times. This allows natural operations on mutable entries,
93but can consume much more memory and make sync and close take a long time.
Skip Montanaro3bf99e32002-12-08 18:36:24 +000094\end{classdesc}
95
Martin v. Löwis153c9e42003-04-19 20:59:03 +000096\begin{classdesc}{BsdDbShelf}{dict\optional{, protocol=None\optional{, writeback=False\optional{, binary=None}}}}
97
98A subclass of \class{Shelf} which exposes \method{first},
99\method{next}, \method{previous}, \method{last} and
100\method{set_location} which are available in the \module{bsddb} module
101but not in other database modules. The \var{dict} object passed to
102the constructor must support those methods. This is generally
103accomplished by calling one of \function{bsddb.hashopen},
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000104\function{bsddb.btopen} or \function{bsddb.rnopen}. The optional
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000105\var{protocol}, \var{writeback}, and \var{binary} parameters have the
106same interpretation as for the \class{Shelf} class.
107
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000108\end{classdesc}
109
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000110\begin{classdesc}{DbfilenameShelf}{filename\optional{, flag='c'\optional{, protocol=None\optional{, writeback=False\optional{, binary=None}}}}}
Skip Montanaro1f7a2712003-01-21 01:52:39 +0000111
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000112A subclass of \class{Shelf} which accepts a \var{filename} instead of
113a dict-like object. The underlying file will be opened using
114{}\function{anydbm.open}. By default, the file will be created and
115opened for both read and write. The optional \var{flag} parameter has
116the same interpretation as for the \function{open} function. The
117optional \var{protocol}, \var{writeback}, and \var{binary} parameters
118have the same interpretation as for the \class{Shelf} class.
119
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000120\end{classdesc}
Fred Drakec8593501998-08-24 18:46:14 +0000121
Skip Montanaro190613c2003-01-21 01:38:47 +0000122\subsection{Example}
123
124To summarize the interface (\code{key} is a string, \code{data} is an
125arbitrary object):
126
127\begin{verbatim}
128import shelve
129
130d = shelve.open(filename) # open -- file may get suffix added by low-level
131 # library
132
133d[key] = data # store data at key (overwrites old data if
134 # using an existing key)
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000135data = d[key] # retrieve a COPY of data at key (raise KeyError if no
Skip Montanaro190613c2003-01-21 01:38:47 +0000136 # such key)
137del d[key] # delete data stored at key (raises KeyError
138 # if no such key)
139flag = d.has_key(key) # true if the key exists
140list = d.keys() # a list of all existing keys (slow!)
141
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000142# as d was opened WITHOUT writeback=True, beware:
143d['xx'] = range(4) # this works as expected, but...
144d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!!!
145# having opened d without writeback=True, you need to code carefully:
146temp = d['xx'] # extracts the copy
147temp.append(5) # mutates the copy
148d['xx'] = temp # stores the copy right back, to persist it
149# or, d=shelve.open(filename,writeback=True) would let you just code
150# d['xx'].append(5) and have it work as expected, BUT it would also
151# consume more memory and make the d.close() operation slower.
152
Skip Montanaro190613c2003-01-21 01:38:47 +0000153d.close() # close it
154\end{verbatim}
155
Fred Drakec8593501998-08-24 18:46:14 +0000156\begin{seealso}
Fred Drake01553701999-04-05 19:46:21 +0000157 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000158 \seemodule{bsddb}{BSD \code{db} database interface.}
159 \seemodule{dbhash}{Thin layer around the \module{bsddb} which provides an
160 \function{open} function like the other database modules.}
Fred Drake01553701999-04-05 19:46:21 +0000161 \seemodule{dbm}{Standard \UNIX{} database interface.}
162 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
163 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
Fred Drakec8593501998-08-24 18:46:14 +0000164 \seemodule{pickle}{Object serialization used by \module{shelve}.}
Fred Drakeffbe6871999-04-22 21:23:22 +0000165 \seemodule{cPickle}{High-performance version of \refmodule{pickle}.}
Fred Drakec8593501998-08-24 18:46:14 +0000166\end{seealso}