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