blob: 76eaaf49c261fe0d344edbbbfc884115804e0304 [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
22d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
23
24d[key] = data # store data at key (overwrites old data if
25 # using an existing key)
26data = d[key] # retrieve data at key (raise KeyError if no
27 # such key)
28del d[key] # delete data stored at key (raises KeyError
29 # if no such key)
30flag = d.has_key(key) # true if the key exists
31list = d.keys() # a list of all existing keys (slow!)
32
33d.close() # close it
Fred Drake19479911998-02-13 06:58:54 +000034\end{verbatim}
Fred Drake01553701999-04-05 19:46:21 +000035
Guido van Rossum5680b951995-02-16 16:29:01 +000036Restrictions:
37
38\begin{itemize}
39
40\item
Fred Drake01553701999-04-05 19:46:21 +000041The choice of which database package will be used
42(e.g. \refmodule{dbm} or \refmodule{gdbm}) depends on which interface
43is available. Therefore it is not safe to open the database directly
44using \refmodule{dbm}. The database is also (unfortunately) subject
45to the limitations of \refmodule{dbm}, if it is used --- this means
46that (the pickled representation of) the objects stored in the
47database should be fairly small, and in rare cases key collisions may
48cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000049\refbimodindex{dbm}
50\refbimodindex{gdbm}
Guido van Rossum5680b951995-02-16 16:29:01 +000051
52\item
Guido van Rossumd1883581995-02-15 15:53:08 +000053Dependent on the implementation, closing a persistent dictionary may
54or may not be necessary to flush changes to disk.
55
Guido van Rossum5680b951995-02-16 16:29:01 +000056\item
Fred Drake01553701999-04-05 19:46:21 +000057The \module{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000058access to shelved objects. (Multiple simultaneous read accesses are
59safe.) When a program has a shelf open for writing, no other program
60should have it open for reading or writing. \UNIX{} file locking can
61be used to solve this, but this differs across \UNIX{} versions and
62requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000063
64\end{itemize}
Fred Drakec8593501998-08-24 18:46:14 +000065
66
67\begin{seealso}
Fred Drake01553701999-04-05 19:46:21 +000068 \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.}
Fred Drake666255f1999-04-15 15:11:40 +000069 \seemodule{dbhash}{BSD \code{db} database interface.}
Fred Drake01553701999-04-05 19:46:21 +000070 \seemodule{dbm}{Standard \UNIX{} database interface.}
71 \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.}
72 \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.}
Fred Drakec8593501998-08-24 18:46:14 +000073 \seemodule{pickle}{Object serialization used by \module{shelve}.}
Fred Drakeffbe6871999-04-22 21:23:22 +000074 \seemodule{cPickle}{High-performance version of \refmodule{pickle}.}
Fred Drakec8593501998-08-24 18:46:14 +000075\end{seealso}