Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`shelve` --- Python object persistence |
| 2 | =========================================== |
| 3 | |
| 4 | .. module:: shelve |
| 5 | :synopsis: Python object persistence. |
| 6 | |
| 7 | |
| 8 | .. index:: module: pickle |
| 9 | |
| 10 | A "shelf" is a persistent, dictionary-like object. The difference with "dbm" |
| 11 | databases is that the values (not the keys!) in a shelf can be essentially |
| 12 | arbitrary Python objects --- anything that the :mod:`pickle` module can handle. |
| 13 | This includes most class instances, recursive data types, and objects containing |
| 14 | lots of shared sub-objects. The keys are ordinary strings. |
| 15 | |
| 16 | |
| 17 | .. function:: open(filename[, flag='c'[, protocol=None[, writeback=False]]]) |
| 18 | |
| 19 | Open a persistent dictionary. The filename specified is the base filename for |
| 20 | the underlying database. As a side-effect, an extension may be added to the |
| 21 | filename and more than one file may be created. By default, the underlying |
| 22 | database file is opened for reading and writing. The optional *flag* parameter |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 23 | has the same interpretation as the *flag* parameter of :func:`dbm.open`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Raymond Hettinger | 8560226 | 2009-02-03 04:19:10 +0000 | [diff] [blame] | 25 | By default, version 3 pickles are used to serialize values. The version of the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | pickle protocol can be specified with the *protocol* parameter. |
| 27 | |
R. David Murray | ff85bca | 2009-05-12 01:40:16 +0000 | [diff] [blame] | 28 | Because of Python semantics, a shelf cannot know when a mutable |
| 29 | persistent-dictionary entry is modified. By default modified objects are |
| 30 | written only when assigned to the shelf (see :ref:`shelve-example`). If |
| 31 | the optional *writeback* parameter is set to *True*, all entries accessed |
| 32 | are cached in memory, and written back at close time; this can make it |
| 33 | handier to mutate mutable entries in the persistent dictionary, but, if |
| 34 | many entries are accessed, it can consume vast amounts of memory for the |
| 35 | cache, and it can make the close operation very slow since all accessed |
| 36 | entries are written back (there is no way to determine which accessed |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | entries are mutable, nor which ones were actually mutated). |
| 38 | |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 39 | Shelf objects support all methods supported by dictionaries. This eases the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | transition from dictionary based scripts to those requiring persistent storage. |
| 41 | |
| 42 | One additional method is supported: |
| 43 | |
| 44 | |
| 45 | .. method:: Shelf.sync() |
| 46 | |
| 47 | Write back all entries in the cache if the shelf was opened with *writeback* set |
| 48 | to *True*. Also empty the cache and synchronize the persistent dictionary on |
| 49 | disk, if feasible. This is called automatically when the shelf is closed with |
| 50 | :meth:`close`. |
| 51 | |
Raymond Hettinger | 65c9eb2 | 2009-04-04 05:39:52 +0000 | [diff] [blame] | 52 | .. seealso:: |
| 53 | |
| 54 | `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_ |
| 55 | with widely supported storage formats and having the speed of native |
| 56 | dictionaries. |
| 57 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
| 59 | Restrictions |
| 60 | ------------ |
| 61 | |
| 62 | .. index:: |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 63 | module: dbm.ndbm |
| 64 | module: dbm.gnu |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 65 | |
Benjamin Peterson | 9a46cab | 2008-09-08 02:49:30 +0000 | [diff] [blame] | 66 | * The choice of which database package will be used (such as :mod:`dbm.ndbm` or |
| 67 | :mod:`dbm.gnu`) depends on which interface is available. Therefore it is not |
| 68 | safe to open the database directly using :mod:`dbm`. The database is also |
| 69 | (unfortunately) subject to the limitations of :mod:`dbm`, if it is used --- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | this means that (the pickled representation of) the objects stored in the |
Benjamin Peterson | 9a46cab | 2008-09-08 02:49:30 +0000 | [diff] [blame] | 71 | database should be fairly small, and in rare cases key collisions may cause |
| 72 | the database to refuse updates. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
| 74 | * Depending on the implementation, closing a persistent dictionary may or may |
| 75 | not be necessary to flush changes to disk. The :meth:`__del__` method of the |
| 76 | :class:`Shelf` class calls the :meth:`close` method, so the programmer generally |
| 77 | need not do this explicitly. |
| 78 | |
| 79 | * The :mod:`shelve` module does not support *concurrent* read/write access to |
| 80 | shelved objects. (Multiple simultaneous read accesses are safe.) When a |
| 81 | program has a shelf open for writing, no other program should have it open for |
| 82 | reading or writing. Unix file locking can be used to solve this, but this |
| 83 | differs across Unix versions and requires knowledge about the database |
| 84 | implementation used. |
| 85 | |
| 86 | |
| 87 | .. class:: Shelf(dict[, protocol=None[, writeback=False]]) |
| 88 | |
Georg Brandl | c772372 | 2008-05-26 17:47:11 +0000 | [diff] [blame] | 89 | A subclass of :class:`collections.MutableMapping` which stores pickled values |
| 90 | in the *dict* object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | By default, version 0 pickles are used to serialize values. The version of the |
| 93 | pickle protocol can be specified with the *protocol* parameter. See the |
| 94 | :mod:`pickle` documentation for a discussion of the pickle protocols. |
| 95 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 96 | If the *writeback* parameter is ``True``, the object will hold a cache of all |
| 97 | entries accessed and write them back to the *dict* at sync and close times. |
| 98 | This allows natural operations on mutable entries, but can consume much more |
| 99 | memory and make sync and close take a long time. |
| 100 | |
| 101 | |
| 102 | .. class:: BsdDbShelf(dict[, protocol=None[, writeback=False]]) |
| 103 | |
| 104 | A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`next`, |
Georg Brandl | 1158a33 | 2009-06-04 09:30:30 +0000 | [diff] [blame] | 105 | :meth:`previous`, :meth:`last` and :meth:`set_location` which are available |
| 106 | in the third-party :mod:`bsddb` module from `pybsddb |
| 107 | <http://www.jcea.es/programacion/pybsddb.htm>`_ but not in other database |
| 108 | modules. The *dict* object passed to the constructor must support those |
| 109 | methods. This is generally accomplished by calling one of |
| 110 | :func:`bsddb.hashopen`, :func:`bsddb.btopen` or :func:`bsddb.rnopen`. The |
| 111 | optional *protocol* and *writeback* parameters have the same interpretation |
| 112 | as for the :class:`Shelf` class. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | .. class:: DbfilenameShelf(filename[, flag='c'[, protocol=None[, writeback=False]]]) |
| 116 | |
| 117 | A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 118 | object. The underlying file will be opened using :func:`dbm.open`. By |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | default, the file will be created and opened for both read and write. The |
| 120 | optional *flag* parameter has the same interpretation as for the :func:`open` |
| 121 | function. The optional *protocol* and *writeback* parameters have the same |
| 122 | interpretation as for the :class:`Shelf` class. |
| 123 | |
| 124 | |
R. David Murray | ff85bca | 2009-05-12 01:40:16 +0000 | [diff] [blame] | 125 | .. _shelve-example: |
| 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | Example |
| 128 | ------- |
| 129 | |
| 130 | To summarize the interface (``key`` is a string, ``data`` is an arbitrary |
| 131 | object):: |
| 132 | |
| 133 | import shelve |
| 134 | |
| 135 | d = shelve.open(filename) # open -- file may get suffix added by low-level |
| 136 | # library |
| 137 | |
| 138 | d[key] = data # store data at key (overwrites old data if |
| 139 | # using an existing key) |
| 140 | data = d[key] # retrieve a COPY of data at key (raise KeyError if no |
| 141 | # such key) |
| 142 | del d[key] # delete data stored at key (raises KeyError |
| 143 | # if no such key) |
Collin Winter | c79461b | 2007-09-01 23:34:30 +0000 | [diff] [blame] | 144 | flag = key in d # true if the key exists |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 145 | klist = d.keys() # a list of all existing keys (slow!) |
| 146 | |
| 147 | # as d was opened WITHOUT writeback=True, beware: |
| 148 | d['xx'] = range(4) # this works as expected, but... |
Benjamin Peterson | d23f822 | 2009-04-05 19:13:16 +0000 | [diff] [blame] | 149 | d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)! |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
| 151 | # having opened d without writeback=True, you need to code carefully: |
| 152 | temp = d['xx'] # extracts the copy |
| 153 | temp.append(5) # mutates the copy |
| 154 | d['xx'] = temp # stores the copy right back, to persist it |
| 155 | |
| 156 | # or, d=shelve.open(filename,writeback=True) would let you just code |
| 157 | # d['xx'].append(5) and have it work as expected, BUT it would also |
| 158 | # consume more memory and make the d.close() operation slower. |
| 159 | |
| 160 | d.close() # close it |
| 161 | |
| 162 | |
| 163 | .. seealso:: |
| 164 | |
Georg Brandl | 0a7ac7d | 2008-05-26 10:29:35 +0000 | [diff] [blame] | 165 | Module :mod:`dbm` |
| 166 | Generic interface to ``dbm``-style databases. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | Module :mod:`pickle` |
| 169 | Object serialization used by :mod:`shelve`. |
| 170 | |
| 171 | Module :mod:`cPickle` |
| 172 | High-performance version of :mod:`pickle`. |
| 173 | |