blob: 7a463cd0094dfbe62a1e4af58cfc187e2de5fad6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{shelve} ---
2 Python object persistency.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{shelve}
4
5\modulesynopsis{Python object persistency.}
6
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
11\code{pickle} module can handle. This includes most class instances,
12recursive data types, and objects containing lots of shared
13sub-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}
Guido van Rossume47da0a1997-07-17 16:34:52 +000035%
Guido van Rossum5680b951995-02-16 16:29:01 +000036Restrictions:
37
38\begin{itemize}
39
40\item
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000041The choice of which database package will be used (e.g. \code{dbm} or
42\code{gdbm})
Guido van Rossum5680b951995-02-16 16:29:01 +000043depends on which interface is available. Therefore it isn't safe to
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000044open the database directly using \code{dbm}. The database is also
45(unfortunately) subject to the limitations of \code{dbm}, if it is used ---
Guido van Rossum5680b951995-02-16 16:29:01 +000046this means that (the pickled representation of) the objects stored in
47the database should be fairly small, and in rare cases key collisions
48may cause 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 Drake9ab2b2ec1997-12-15 21:59:33 +000057The \code{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}
68 \seemodule{pickle}{Object serialization used by \module{shelve}.}
69 \seemodule{cPickle}{High-performance version of \module{pickle}.}
70\end{seealso}