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