blob: 8bd204e331ee3b3d3dcf5d57d2bdd22ed2498a42 [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
Raymond Hettinger68804312005-01-01 00:28:46 +000021{}\var{flag} parameter 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
Georg Brandlbd77da62005-08-25 22:40:16 +000045One additional method is supported:
46\begin{methoddesc}[Shelf]{sync}{}
47Write back all entries in the cache if the shelf was opened with
48\var{writeback} set to \var{True}. Also empty the cache and synchronize
49the persistent dictionary on disk, if feasible. This is called automatically
50when the shelf is closed with \method{close()}.
51\end{methoddesc}
52
Skip Montanaro190613c2003-01-21 01:38:47 +000053\subsection{Restrictions}
Guido van Rossum5680b951995-02-16 16:29:01 +000054
55\begin{itemize}
56
57\item
Fred Drake01553701999-04-05 19:46:21 +000058The choice of which database package will be used
Skip Montanaro190613c2003-01-21 01:38:47 +000059(such as \refmodule{dbm}, \refmodule{gdbm} or \refmodule{bsddb}) depends on
60which interface is available. Therefore it is not safe to open the database
61directly using \refmodule{dbm}. The database is also (unfortunately) subject
Fred Drake01553701999-04-05 19:46:21 +000062to the limitations of \refmodule{dbm}, if it is used --- this means
63that (the pickled representation of) the objects stored in the
64database should be fairly small, and in rare cases key collisions may
65cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000066\refbimodindex{dbm}
67\refbimodindex{gdbm}
Skip Montanaro190613c2003-01-21 01:38:47 +000068\refbimodindex{bsddb}
Guido van Rossum5680b951995-02-16 16:29:01 +000069
70\item
Skip Montanaro3bf99e32002-12-08 18:36:24 +000071Depending on the implementation, closing a persistent dictionary may
72or may not be necessary to flush changes to disk. The \method{__del__}
73method of the \class{Shelf} class calls the \method{close} method, so the
74programmer generally need not do this explicitly.
Guido van Rossumd1883581995-02-15 15:53:08 +000075
Guido van Rossum5680b951995-02-16 16:29:01 +000076\item
Fred Drake01553701999-04-05 19:46:21 +000077The \module{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000078access to shelved objects. (Multiple simultaneous read accesses are
79safe.) When a program has a shelf open for writing, no other program
80should have it open for reading or writing. \UNIX{} file locking can
81be used to solve this, but this differs across \UNIX{} versions and
82requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000083
84\end{itemize}
Fred Drakec8593501998-08-24 18:46:14 +000085
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000086\begin{classdesc}{Shelf}{dict\optional{, protocol=None\optional{, writeback=False}}}
Skip Montanaro3bf99e32002-12-08 18:36:24 +000087A subclass of \class{UserDict.DictMixin} which stores pickled values in the
Martin v. Löwis153c9e42003-04-19 20:59:03 +000088\var{dict} object.
89
90By default, version 0 pickles are used to serialize values. The
91version of the pickle protocol can be specified with the
92\var{protocol} parameter. See the \module{pickle} documentation for a
93discussion of the pickle protocols. \versionchanged[The \var{protocol}
Raymond Hettinger1bc82f82004-12-05 03:58:17 +000094parameter was added]{2.3}
Martin v. Löwis153c9e42003-04-19 20:59:03 +000095
96If the \var{writeback} parameter is \code{True}, the object will hold a
97cache of all entries accessed and write them back to the \var{dict} at
98sync and close times. This allows natural operations on mutable entries,
99but can consume much more memory and make sync and close take a long time.
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000100\end{classdesc}
101
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000102\begin{classdesc}{BsdDbShelf}{dict\optional{, protocol=None\optional{, writeback=False}}}
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000103
104A subclass of \class{Shelf} which exposes \method{first},
105\method{next}, \method{previous}, \method{last} and
106\method{set_location} which are available in the \module{bsddb} module
107but not in other database modules. The \var{dict} object passed to
108the constructor must support those methods. This is generally
109accomplished by calling one of \function{bsddb.hashopen},
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000110\function{bsddb.btopen} or \function{bsddb.rnopen}. The optional
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000111\var{protocol} and \var{writeback} parameters have the
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000112same interpretation as for the \class{Shelf} class.
113
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000114\end{classdesc}
115
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000116\begin{classdesc}{DbfilenameShelf}{filename\optional{, flag='c'\optional{, protocol=None\optional{, writeback=False}}}}
Skip Montanaro1f7a2712003-01-21 01:52:39 +0000117
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000118A subclass of \class{Shelf} which accepts a \var{filename} instead of
119a dict-like object. The underlying file will be opened using
120{}\function{anydbm.open}. By default, the file will be created and
121opened for both read and write. The optional \var{flag} parameter has
122the same interpretation as for the \function{open} function. The
Raymond Hettinger1bc82f82004-12-05 03:58:17 +0000123optional \var{protocol} and \var{writeback} parameters
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000124have the same interpretation as for the \class{Shelf} class.
125
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000126\end{classdesc}
Fred Drakec8593501998-08-24 18:46:14 +0000127
Skip Montanaro190613c2003-01-21 01:38:47 +0000128\subsection{Example}
129
130To summarize the interface (\code{key} is a string, \code{data} is an
131arbitrary object):
132
133\begin{verbatim}
134import shelve
135
136d = shelve.open(filename) # open -- file may get suffix added by low-level
137 # library
138
139d[key] = data # store data at key (overwrites old data if
140 # using an existing key)
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000141data = d[key] # retrieve a COPY of data at key (raise KeyError if no
Skip Montanaro190613c2003-01-21 01:38:47 +0000142 # such key)
143del d[key] # delete data stored at key (raises KeyError
144 # if no such key)
145flag = d.has_key(key) # true if the key exists
146list = d.keys() # a list of all existing keys (slow!)
147
Martin v. Löwis153c9e42003-04-19 20:59:03 +0000148# as d was opened WITHOUT writeback=True, beware:
149d['xx'] = range(4) # this works as expected, but...
150d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!!!
151# having opened d without writeback=True, you need to code carefully:
152temp = d['xx'] # extracts the copy
153temp.append(5) # mutates the copy
154d['xx'] = temp # stores the copy right back, to persist it
155# or, d=shelve.open(filename,writeback=True) would let you just code
156# d['xx'].append(5) and have it work as expected, BUT it would also
157# consume more memory and make the d.close() operation slower.
158
Skip Montanaro190613c2003-01-21 01:38:47 +0000159d.close() # close it
160\end{verbatim}
161
Fred Drakec8593501998-08-24 18:46:14 +0000162\begin{seealso}
Fred Drake01553701999-04-05 19:46:21 +0000163 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000164 \seemodule{bsddb}{BSD \code{db} database interface.}
165 \seemodule{dbhash}{Thin layer around the \module{bsddb} which provides an
166 \function{open} function like the other database modules.}
Fred Drake01553701999-04-05 19:46:21 +0000167 \seemodule{dbm}{Standard \UNIX{} database interface.}
168 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
169 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
Fred Drakec8593501998-08-24 18:46:14 +0000170 \seemodule{pickle}{Object serialization used by \module{shelve}.}
Fred Drakeffbe6871999-04-22 21:23:22 +0000171 \seemodule{cPickle}{High-performance version of \refmodule{pickle}.}
Fred Drakec8593501998-08-24 18:46:14 +0000172\end{seealso}