blob: 6b5aa76bb5c6634511321da58f786167f81847ac [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{shelve}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{shelve}
3
4\modulesynopsis{Python object persistency.}
5
Guido van Rossumd1883581995-02-15 15:53:08 +00006
7A ``shelf'' is a persistent, dictionary-like object. The difference
8with ``dbm'' databases is that the values (not the keys!) in a shelf
9can be essentially arbitrary Python objects --- anything that the
10\code{pickle} module can handle. This includes most class instances,
11recursive data types, and objects containing lots of shared
12sub-objects. The keys are ordinary strings.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000013\refstmodindex{pickle}
Guido van Rossumd1883581995-02-15 15:53:08 +000014
15To summarize the interface (\code{key} is a string, \code{data} is an
16arbitrary object):
17
Fred Drake19479911998-02-13 06:58:54 +000018\begin{verbatim}
Guido van Rossumd1883581995-02-15 15:53:08 +000019import shelve
20
21d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
22
23d[key] = data # store data at key (overwrites old data if
24 # using an existing key)
25data = d[key] # retrieve data at key (raise KeyError if no
26 # such key)
27del d[key] # delete data stored at key (raises KeyError
28 # if no such key)
29flag = d.has_key(key) # true if the key exists
30list = d.keys() # a list of all existing keys (slow!)
31
32d.close() # close it
Fred Drake19479911998-02-13 06:58:54 +000033\end{verbatim}
Guido van Rossume47da0a1997-07-17 16:34:52 +000034%
Guido van Rossum5680b951995-02-16 16:29:01 +000035Restrictions:
36
37\begin{itemize}
38
39\item
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000040The choice of which database package will be used (e.g. \code{dbm} or
41\code{gdbm})
Guido van Rossum5680b951995-02-16 16:29:01 +000042depends on which interface is available. Therefore it isn't safe to
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000043open the database directly using \code{dbm}. The database is also
44(unfortunately) subject to the limitations of \code{dbm}, if it is used ---
Guido van Rossum5680b951995-02-16 16:29:01 +000045this means that (the pickled representation of) the objects stored in
46the database should be fairly small, and in rare cases key collisions
47may cause the database to refuse updates.
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000048\refbimodindex{dbm}
49\refbimodindex{gdbm}
Guido van Rossum5680b951995-02-16 16:29:01 +000050
51\item
Guido van Rossumd1883581995-02-15 15:53:08 +000052Dependent on the implementation, closing a persistent dictionary may
53or may not be necessary to flush changes to disk.
54
Guido van Rossum5680b951995-02-16 16:29:01 +000055\item
Fred Drake9ab2b2ec1997-12-15 21:59:33 +000056The \code{shelve} module does not support \emph{concurrent} read/write
Guido van Rossum470be141995-03-17 16:07:09 +000057access to shelved objects. (Multiple simultaneous read accesses are
58safe.) When a program has a shelf open for writing, no other program
59should have it open for reading or writing. \UNIX{} file locking can
60be used to solve this, but this differs across \UNIX{} versions and
61requires knowledge about the database implementation used.
Guido van Rossum5680b951995-02-16 16:29:01 +000062
63\end{itemize}