Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{shelve}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-shelve} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 3 | \stmodindex{shelve} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 4 | |
| 5 | A ``shelf'' is a persistent, dictionary-like object. The difference |
| 6 | with ``dbm'' databases is that the values (not the keys!) in a shelf |
| 7 | can be essentially arbitrary Python objects --- anything that the |
| 8 | \code{pickle} module can handle. This includes most class instances, |
| 9 | recursive data types, and objects containing lots of shared |
| 10 | sub-objects. The keys are ordinary strings. |
Fred Drake | 9ab2b2ec | 1997-12-15 21:59:33 +0000 | [diff] [blame^] | 11 | \refstmodindex{pickle} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 12 | |
| 13 | To summarize the interface (\code{key} is a string, \code{data} is an |
| 14 | arbitrary object): |
| 15 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 16 | \bcode\begin{verbatim} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 17 | import shelve |
| 18 | |
| 19 | d = shelve.open(filename) # open, with (g)dbm filename -- no suffix |
| 20 | |
| 21 | d[key] = data # store data at key (overwrites old data if |
| 22 | # using an existing key) |
| 23 | data = d[key] # retrieve data at key (raise KeyError if no |
| 24 | # such key) |
| 25 | del d[key] # delete data stored at key (raises KeyError |
| 26 | # if no such key) |
| 27 | flag = d.has_key(key) # true if the key exists |
| 28 | list = d.keys() # a list of all existing keys (slow!) |
| 29 | |
| 30 | d.close() # close it |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 31 | \end{verbatim}\ecode |
| 32 | % |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 33 | Restrictions: |
| 34 | |
| 35 | \begin{itemize} |
| 36 | |
| 37 | \item |
Fred Drake | 9ab2b2ec | 1997-12-15 21:59:33 +0000 | [diff] [blame^] | 38 | The choice of which database package will be used (e.g. \code{dbm} or |
| 39 | \code{gdbm}) |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 40 | depends on which interface is available. Therefore it isn't safe to |
Fred Drake | 9ab2b2ec | 1997-12-15 21:59:33 +0000 | [diff] [blame^] | 41 | open 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 Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 43 | this means that (the pickled representation of) the objects stored in |
| 44 | the database should be fairly small, and in rare cases key collisions |
| 45 | may cause the database to refuse updates. |
Fred Drake | 9ab2b2ec | 1997-12-15 21:59:33 +0000 | [diff] [blame^] | 46 | \refbimodindex{dbm} |
| 47 | \refbimodindex{gdbm} |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 48 | |
| 49 | \item |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 50 | Dependent on the implementation, closing a persistent dictionary may |
| 51 | or may not be necessary to flush changes to disk. |
| 52 | |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 53 | \item |
Fred Drake | 9ab2b2ec | 1997-12-15 21:59:33 +0000 | [diff] [blame^] | 54 | The \code{shelve} module does not support \emph{concurrent} read/write |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 55 | access to shelved objects. (Multiple simultaneous read accesses are |
| 56 | safe.) When a program has a shelf open for writing, no other program |
| 57 | should have it open for reading or writing. \UNIX{} file locking can |
| 58 | be used to solve this, but this differs across \UNIX{} versions and |
| 59 | requires knowledge about the database implementation used. |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 60 | |
| 61 | \end{itemize} |