blob: 74c71c4d96ce26f2d0a8f8d239af0ebb94763aa8 [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
16To summarize the interface (\code{key} is a string, \code{data} is an
17arbitrary object):
18
Fred Drake19479911998-02-13 06:58:54 +000019\begin{verbatim}
Guido van Rossumd1883581995-02-15 15:53:08 +000020import shelve
21
Skip Montanaro3bf99e32002-12-08 18:36:24 +000022d = shelve.open(filename) # open -- file may get suffix added by low-level
23 # library
Guido van Rossumd1883581995-02-15 15:53:08 +000024
25d[key] = data # store data at key (overwrites old data if
26 # using an existing key)
27data = d[key] # retrieve data at key (raise KeyError if no
28 # such key)
29del d[key] # delete data stored at key (raises KeyError
30 # if no such key)
31flag = d.has_key(key) # true if the key exists
32list = d.keys() # a list of all existing keys (slow!)
33
34d.close() # close it
Fred Drake19479911998-02-13 06:58:54 +000035\end{verbatim}
Fred Drake01553701999-04-05 19:46:21 +000036
Raymond Hettinger79947162002-11-15 06:46:14 +000037In addition to the above, shelve supports all methods that are
38supported by dictionaries. This eases the transition from dictionary
39based scripts to those requiring persistent storage.
40
Guido van Rossum5680b951995-02-16 16:29:01 +000041Restrictions:
42
43\begin{itemize}
44
45\item
Fred Drake01553701999-04-05 19:46:21 +000046The choice of which database package will be used
Raymond Hettinger2ca24332003-01-04 01:53:38 +000047(such as \refmodule{dbm} or \refmodule{gdbm}) depends on which interface
Fred Drake01553701999-04-05 19:46:21 +000048is available. Therefore it is not safe to open the database directly
49using \refmodule{dbm}. The database is also (unfortunately) subject
50to the limitations of \refmodule{dbm}, if it is used --- this means
51that (the pickled representation of) the objects stored in the
52database should be fairly small, and in rare cases key collisions may
53cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000054\refbimodindex{dbm}
55\refbimodindex{gdbm}
Guido van Rossum5680b951995-02-16 16:29:01 +000056
57\item
Skip Montanaro3bf99e32002-12-08 18:36:24 +000058Depending on the implementation, closing a persistent dictionary may
59or may not be necessary to flush changes to disk. The \method{__del__}
60method of the \class{Shelf} class calls the \method{close} method, so the
61programmer generally need not do this explicitly.
Guido van Rossumd1883581995-02-15 15:53:08 +000062
Guido van Rossum5680b951995-02-16 16:29:01 +000063\item
Fred Drake01553701999-04-05 19:46:21 +000064The \module{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000065access to shelved objects. (Multiple simultaneous read accesses are
66safe.) When a program has a shelf open for writing, no other program
67should have it open for reading or writing. \UNIX{} file locking can
68be used to solve this, but this differs across \UNIX{} versions and
69requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000070
71\end{itemize}
Fred Drakec8593501998-08-24 18:46:14 +000072
Skip Montanaro3bf99e32002-12-08 18:36:24 +000073\begin{classdesc}{Shelf}{dict\optional{, binary=False}}
74A subclass of \class{UserDict.DictMixin} which stores pickled values in the
Raymond Hettinger2ca24332003-01-04 01:53:38 +000075\var{dict} object. If the \var{binary} parameter is \code{True}, binary
Skip Montanaro3bf99e32002-12-08 18:36:24 +000076pickles will be used. This can provide much more compact storage than plain
Raymond Hettinger2ca24332003-01-04 01:53:38 +000077text pickles, depending on the nature of the objects stored in the database.
Skip Montanaro3bf99e32002-12-08 18:36:24 +000078\end{classdesc}
79
80\begin{classdesc}{BsdDbShelf}{dict\optional{, binary=False}}
81A subclass of \class{Shelf} which exposes \method{first}, \method{next},
Raymond Hettinger2ca24332003-01-04 01:53:38 +000082\method{previous}, \method{last} and \method{set_location} which are
Skip Montanaro3bf99e32002-12-08 18:36:24 +000083available in the \module{bsddb} module but not in other database modules.
84The \var{dict} object passed to the constructor must support those methods.
85This is generally accomplished by calling one of \function{bsddb.hashopen},
86\function{bsddb.btopen} or \function{bsddb.rnopen}. The optional
87\var{binary} parameter has the same interpretation as for the \class{Shelf}
88class.
89\end{classdesc}
90
Raymond Hettinger2ca24332003-01-04 01:53:38 +000091\begin{classdesc}{DbfilenameShelf}{filename\optional{, flag='c'\optional{, binary=False}}}
92A subclass of \class{Shelf} which accepts a \var{filename} instead of a dict-like
Skip Montanaro3bf99e32002-12-08 18:36:24 +000093object. The underlying file will be opened using \function{anydbm.open}.
94By default, the file will be created and opened for both read and write.
95The optional \var{binary} parameter has the same interpretation as for the
96\class{Shelf} class.
97\end{classdesc}
Fred Drakec8593501998-08-24 18:46:14 +000098
99\begin{seealso}
Fred Drake01553701999-04-05 19:46:21 +0000100 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Skip Montanaro3bf99e32002-12-08 18:36:24 +0000101 \seemodule{bsddb}{BSD \code{db} database interface.}
102 \seemodule{dbhash}{Thin layer around the \module{bsddb} which provides an
103 \function{open} function like the other database modules.}
Fred Drake01553701999-04-05 19:46:21 +0000104 \seemodule{dbm}{Standard \UNIX{} database interface.}
105 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
106 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
Fred Drakec8593501998-08-24 18:46:14 +0000107 \seemodule{pickle}{Object serialization used by \module{shelve}.}
Fred Drakeffbe6871999-04-22 21:23:22 +0000108 \seemodule{cPickle}{High-performance version of \refmodule{pickle}.}
Fred Drakec8593501998-08-24 18:46:14 +0000109\end{seealso}