blob: 996c79bef228af40962ae39645caf5c499c24bc6 [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
Skip Montanaro190613c2003-01-21 01:38:47 +000016\begin{funcdesc}{open}{filename\optional{,flag='c'\optional{,binary=\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}
22parameter of \function{anydbm.open}. By default, ASCII pickles are used to
23serialize values. If the optional \var{binary} parameter is set to
24{}\var{True}, binary pickles will be used instead.
Skip Montanaro190613c2003-01-21 01:38:47 +000025\end{funcdesc}
Guido van Rossumd1883581995-02-15 15:53:08 +000026
Skip Montanaro190613c2003-01-21 01:38:47 +000027Shelve objects support all methods supported by dictionaries. This eases
28the transition from dictionary based scripts to those requiring persistent
29storage.
Guido van Rossumd1883581995-02-15 15:53:08 +000030
Skip Montanaro190613c2003-01-21 01:38:47 +000031\subsection{Restrictions}
Guido van Rossum5680b951995-02-16 16:29:01 +000032
33\begin{itemize}
34
35\item
Fred Drake01553701999-04-05 19:46:21 +000036The choice of which database package will be used
Skip Montanaro190613c2003-01-21 01:38:47 +000037(such as \refmodule{dbm}, \refmodule{gdbm} or \refmodule{bsddb}) depends on
38which interface is available. Therefore it is not safe to open the database
39directly using \refmodule{dbm}. The database is also (unfortunately) subject
Fred Drake01553701999-04-05 19:46:21 +000040to the limitations of \refmodule{dbm}, if it is used --- this means
41that (the pickled representation of) the objects stored in the
42database should be fairly small, and in rare cases key collisions may
43cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000044\refbimodindex{dbm}
45\refbimodindex{gdbm}
Skip Montanaro190613c2003-01-21 01:38:47 +000046\refbimodindex{bsddb}
Guido van Rossum5680b951995-02-16 16:29:01 +000047
48\item
Skip Montanaro3bf99e32002-12-08 18:36:24 +000049Depending on the implementation, closing a persistent dictionary may
50or may not be necessary to flush changes to disk. The \method{__del__}
51method of the \class{Shelf} class calls the \method{close} method, so the
52programmer generally need not do this explicitly.
Guido van Rossumd1883581995-02-15 15:53:08 +000053
Guido van Rossum5680b951995-02-16 16:29:01 +000054\item
Fred Drake01553701999-04-05 19:46:21 +000055The \module{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000056access to shelved objects. (Multiple simultaneous read accesses are
57safe.) When a program has a shelf open for writing, no other program
58should have it open for reading or writing. \UNIX{} file locking can
59be used to solve this, but this differs across \UNIX{} versions and
60requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000061
62\end{itemize}
Fred Drakec8593501998-08-24 18:46:14 +000063
Skip Montanaro3bf99e32002-12-08 18:36:24 +000064\begin{classdesc}{Shelf}{dict\optional{, binary=False}}
65A subclass of \class{UserDict.DictMixin} which stores pickled values in the
Raymond Hettinger2ca24332003-01-04 01:53:38 +000066\var{dict} object. If the \var{binary} parameter is \code{True}, binary
Skip Montanaro3bf99e32002-12-08 18:36:24 +000067pickles will be used. This can provide much more compact storage than plain
Raymond Hettinger2ca24332003-01-04 01:53:38 +000068text pickles, depending on the nature of the objects stored in the database.
Skip Montanaro3bf99e32002-12-08 18:36:24 +000069\end{classdesc}
70
71\begin{classdesc}{BsdDbShelf}{dict\optional{, binary=False}}
72A subclass of \class{Shelf} which exposes \method{first}, \method{next},
Raymond Hettinger2ca24332003-01-04 01:53:38 +000073\method{previous}, \method{last} and \method{set_location} which are
Skip Montanaro3bf99e32002-12-08 18:36:24 +000074available in the \module{bsddb} module but not in other database modules.
75The \var{dict} object passed to the constructor must support those methods.
76This is generally accomplished by calling one of \function{bsddb.hashopen},
77\function{bsddb.btopen} or \function{bsddb.rnopen}. The optional
78\var{binary} parameter has the same interpretation as for the \class{Shelf}
79class.
80\end{classdesc}
81
Raymond Hettinger2ca24332003-01-04 01:53:38 +000082\begin{classdesc}{DbfilenameShelf}{filename\optional{, flag='c'\optional{, binary=False}}}
Skip Montanaro1f7a2712003-01-21 01:52:39 +000083
84A subclass of \class{Shelf} which accepts a \var{filename} instead of a
85dict-like object. The underlying file will be opened using
86{}\function{anydbm.open}. By default, the file will be created and opened
87for both read and write. The optional \var{flag} parameter has the same
88interpretation as for the \function{open} function. The optional
89\var{binary} parameter has the same interpretation as for the
90{}\class{Shelf} class.
Skip Montanaro3bf99e32002-12-08 18:36:24 +000091\end{classdesc}
Fred Drakec8593501998-08-24 18:46:14 +000092
Skip Montanaro190613c2003-01-21 01:38:47 +000093\subsection{Example}
94
95To summarize the interface (\code{key} is a string, \code{data} is an
96arbitrary object):
97
98\begin{verbatim}
99import shelve
100
101d = shelve.open(filename) # open -- file may get suffix added by low-level
102 # library
103
104d[key] = data # store data at key (overwrites old data if
105 # using an existing key)
106data = d[key] # retrieve data at key (raise KeyError if no
107 # such key)
108del d[key] # delete data stored at key (raises KeyError
109 # if no such key)
110flag = d.has_key(key) # true if the key exists
111list = d.keys() # a list of all existing keys (slow!)
112
113d.close() # close it
114\end{verbatim}
115
Fred Drakec8593501998-08-24 18:46:14 +0000116\begin{seealso}
Fred Drake01553701999-04-05 19:46:21 +0000117 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000118 \seemodule{bsddb}{BSD \code{db} database interface.}
119 \seemodule{dbhash}{Thin layer around the \module{bsddb} which provides an
120 \function{open} function like the other database modules.}
Fred Drake01553701999-04-05 19:46:21 +0000121 \seemodule{dbm}{Standard \UNIX{} database interface.}
122 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
123 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
Fred Drakec8593501998-08-24 18:46:14 +0000124 \seemodule{pickle}{Object serialization used by \module{shelve}.}
Fred Drakeffbe6871999-04-22 21:23:22 +0000125 \seemodule{cPickle}{High-performance version of \refmodule{pickle}.}
Fred Drakec8593501998-08-24 18:46:14 +0000126\end{seealso}