Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{shelve} --- |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 2 | Python object persistence} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{shelve} |
Thomas Wouters | f831663 | 2000-07-16 19:01:10 +0000 | [diff] [blame] | 5 | \modulesynopsis{Python object persistence.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 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 |
Fred Drake | 0155370 | 1999-04-05 19:46:21 +0000 | [diff] [blame] | 11 | \refmodule{pickle} module can handle. This includes most class |
| 12 | instances, recursive data types, and objects containing lots of shared |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 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 | |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 16 | \begin{funcdesc}{open}{filename\optional{,flag='c'\optional{,binary=\code{False}}}} |
Skip Montanaro | 1f7a271 | 2003-01-21 01:52:39 +0000 | [diff] [blame] | 17 | Open a persistent dictionary. The filename specified is the base filename |
| 18 | for the underlying database. As a side-effect, an extension may be added to |
| 19 | the filename and more than one file may be created. By default, the |
| 20 | underlying database file is opened for reading and writing. The optional |
| 21 | {}\var{flag} pararameter has the same interpretation as the \var{flag} |
| 22 | parameter of \function{anydbm.open}. By default, ASCII pickles are used to |
| 23 | serialize values. If the optional \var{binary} parameter is set to |
| 24 | {}\var{True}, binary pickles will be used instead. |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 25 | \end{funcdesc} |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 26 | |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 27 | Shelve objects support all methods supported by dictionaries. This eases |
| 28 | the transition from dictionary based scripts to those requiring persistent |
| 29 | storage. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 30 | |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 31 | \subsection{Restrictions} |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 32 | |
| 33 | \begin{itemize} |
| 34 | |
| 35 | \item |
Fred Drake | 0155370 | 1999-04-05 19:46:21 +0000 | [diff] [blame] | 36 | The choice of which database package will be used |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 37 | (such as \refmodule{dbm}, \refmodule{gdbm} or \refmodule{bsddb}) depends on |
| 38 | which interface is available. Therefore it is not safe to open the database |
| 39 | directly using \refmodule{dbm}. The database is also (unfortunately) subject |
Fred Drake | 0155370 | 1999-04-05 19:46:21 +0000 | [diff] [blame] | 40 | to the limitations of \refmodule{dbm}, if it is used --- this means |
| 41 | that (the pickled representation of) the objects stored in the |
| 42 | database should be fairly small, and in rare cases key collisions may |
| 43 | cause the database to refuse updates. |
Fred Drake | 9ab2b2ec | 1997-12-15 21:59:33 +0000 | [diff] [blame] | 44 | \refbimodindex{dbm} |
| 45 | \refbimodindex{gdbm} |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 46 | \refbimodindex{bsddb} |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 47 | |
| 48 | \item |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 49 | Depending on the implementation, closing a persistent dictionary may |
| 50 | or may not be necessary to flush changes to disk. The \method{__del__} |
| 51 | method of the \class{Shelf} class calls the \method{close} method, so the |
| 52 | programmer generally need not do this explicitly. |
Guido van Rossum | d188358 | 1995-02-15 15:53:08 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 54 | \item |
Fred Drake | 0155370 | 1999-04-05 19:46:21 +0000 | [diff] [blame] | 55 | The \module{shelve} module does not support \emph{concurrent} read/write |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 56 | access to shelved objects. (Multiple simultaneous read accesses are |
| 57 | safe.) When a program has a shelf open for writing, no other program |
| 58 | should have it open for reading or writing. \UNIX{} file locking can |
| 59 | be used to solve this, but this differs across \UNIX{} versions and |
| 60 | requires knowledge about the database implementation used. |
Guido van Rossum | 5680b95 | 1995-02-16 16:29:01 +0000 | [diff] [blame] | 61 | |
| 62 | \end{itemize} |
Fred Drake | c859350 | 1998-08-24 18:46:14 +0000 | [diff] [blame] | 63 | |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 64 | \begin{classdesc}{Shelf}{dict\optional{, binary=False}} |
| 65 | A subclass of \class{UserDict.DictMixin} which stores pickled values in the |
Raymond Hettinger | 2ca2433 | 2003-01-04 01:53:38 +0000 | [diff] [blame] | 66 | \var{dict} object. If the \var{binary} parameter is \code{True}, binary |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 67 | pickles will be used. This can provide much more compact storage than plain |
Raymond Hettinger | 2ca2433 | 2003-01-04 01:53:38 +0000 | [diff] [blame] | 68 | text pickles, depending on the nature of the objects stored in the database. |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 69 | \end{classdesc} |
| 70 | |
| 71 | \begin{classdesc}{BsdDbShelf}{dict\optional{, binary=False}} |
| 72 | A subclass of \class{Shelf} which exposes \method{first}, \method{next}, |
Raymond Hettinger | 2ca2433 | 2003-01-04 01:53:38 +0000 | [diff] [blame] | 73 | \method{previous}, \method{last} and \method{set_location} which are |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 74 | available in the \module{bsddb} module but not in other database modules. |
| 75 | The \var{dict} object passed to the constructor must support those methods. |
| 76 | This is generally accomplished by calling one of \function{bsddb.hashopen}, |
| 77 | \function{bsddb.btopen} or \function{bsddb.rnopen}. The optional |
| 78 | \var{binary} parameter has the same interpretation as for the \class{Shelf} |
| 79 | class. |
| 80 | \end{classdesc} |
| 81 | |
Raymond Hettinger | 2ca2433 | 2003-01-04 01:53:38 +0000 | [diff] [blame] | 82 | \begin{classdesc}{DbfilenameShelf}{filename\optional{, flag='c'\optional{, binary=False}}} |
Skip Montanaro | 1f7a271 | 2003-01-21 01:52:39 +0000 | [diff] [blame] | 83 | |
| 84 | A subclass of \class{Shelf} which accepts a \var{filename} instead of a |
| 85 | dict-like object. The underlying file will be opened using |
| 86 | {}\function{anydbm.open}. By default, the file will be created and opened |
| 87 | for both read and write. The optional \var{flag} parameter has the same |
| 88 | interpretation as for the \function{open} function. The optional |
| 89 | \var{binary} parameter has the same interpretation as for the |
| 90 | {}\class{Shelf} class. |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 91 | \end{classdesc} |
Fred Drake | c859350 | 1998-08-24 18:46:14 +0000 | [diff] [blame] | 92 | |
Skip Montanaro | 190613c | 2003-01-21 01:38:47 +0000 | [diff] [blame] | 93 | \subsection{Example} |
| 94 | |
| 95 | To summarize the interface (\code{key} is a string, \code{data} is an |
| 96 | arbitrary object): |
| 97 | |
| 98 | \begin{verbatim} |
| 99 | import shelve |
| 100 | |
| 101 | d = shelve.open(filename) # open -- file may get suffix added by low-level |
| 102 | # library |
| 103 | |
| 104 | d[key] = data # store data at key (overwrites old data if |
| 105 | # using an existing key) |
| 106 | data = d[key] # retrieve data at key (raise KeyError if no |
| 107 | # such key) |
| 108 | del d[key] # delete data stored at key (raises KeyError |
| 109 | # if no such key) |
| 110 | flag = d.has_key(key) # true if the key exists |
| 111 | list = d.keys() # a list of all existing keys (slow!) |
| 112 | |
| 113 | d.close() # close it |
| 114 | \end{verbatim} |
| 115 | |
Fred Drake | c859350 | 1998-08-24 18:46:14 +0000 | [diff] [blame] | 116 | \begin{seealso} |
Fred Drake | 0155370 | 1999-04-05 19:46:21 +0000 | [diff] [blame] | 117 | \seemodule{anydbm}{Generic interface to \code{dbm}-style databases.} |
Skip Montanaro | 3bf99e3 | 2002-12-08 18:36:24 +0000 | [diff] [blame] | 118 | \seemodule{bsddb}{BSD \code{db} database interface.} |
| 119 | \seemodule{dbhash}{Thin layer around the \module{bsddb} which provides an |
| 120 | \function{open} function like the other database modules.} |
Fred Drake | 0155370 | 1999-04-05 19:46:21 +0000 | [diff] [blame] | 121 | \seemodule{dbm}{Standard \UNIX{} database interface.} |
| 122 | \seemodule{dumbdbm}{Portable implementation of the \code{dbm} interface.} |
| 123 | \seemodule{gdbm}{GNU database interface, based on the \code{dbm} interface.} |
Fred Drake | c859350 | 1998-08-24 18:46:14 +0000 | [diff] [blame] | 124 | \seemodule{pickle}{Object serialization used by \module{shelve}.} |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 125 | \seemodule{cPickle}{High-performance version of \refmodule{pickle}.} |
Fred Drake | c859350 | 1998-08-24 18:46:14 +0000 | [diff] [blame] | 126 | \end{seealso} |