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