Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +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 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 10 | **Source code:** :source:`Lib/shelve.py` |
| 11 | |
| 12 | -------------- |
| 13 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 14 | A "shelf" is a persistent, dictionary-like object. The difference with "dbm" |
| 15 | databases is that the values (not the keys!) in a shelf can be essentially |
| 16 | arbitrary Python objects --- anything that the :mod:`pickle` module can handle. |
| 17 | This includes most class instances, recursive data types, and objects containing |
| 18 | lots of shared sub-objects. The keys are ordinary strings. |
| 19 | |
| 20 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 21 | .. function:: open(filename, flag='c', protocol=None, writeback=False) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 22 | |
| 23 | Open a persistent dictionary. The filename specified is the base filename for |
| 24 | the underlying database. As a side-effect, an extension may be added to the |
| 25 | filename and more than one file may be created. By default, the underlying |
| 26 | database file is opened for reading and writing. The optional *flag* parameter |
| 27 | has the same interpretation as the *flag* parameter of :func:`anydbm.open`. |
| 28 | |
| 29 | By default, version 0 pickles are used to serialize values. The version of the |
| 30 | pickle protocol can be specified with the *protocol* parameter. |
| 31 | |
| 32 | .. versionchanged:: 2.3 |
| 33 | The *protocol* parameter was added. |
| 34 | |
R. David Murray | 7118954 | 2009-05-12 01:36:57 +0000 | [diff] [blame] | 35 | Because of Python semantics, a shelf cannot know when a mutable |
| 36 | persistent-dictionary entry is modified. By default modified objects are |
R. David Murray | 7c29f07 | 2010-02-11 01:38:42 +0000 | [diff] [blame] | 37 | written *only* when assigned to the shelf (see :ref:`shelve-example`). If the |
| 38 | optional *writeback* parameter is set to *True*, all entries accessed are also |
| 39 | cached in memory, and written back on :meth:`~Shelf.sync` and |
| 40 | :meth:`~Shelf.close`; this can make it handier to mutate mutable entries in |
| 41 | the persistent dictionary, but, if many entries are accessed, it can consume |
| 42 | vast amounts of memory for the cache, and it can make the close operation |
| 43 | very slow since all accessed entries are written back (there is no way to |
| 44 | determine which accessed entries are mutable, nor which ones were actually |
| 45 | mutated). |
Georg Brandl | 304d396 | 2009-09-17 16:15:53 +0000 | [diff] [blame] | 46 | |
Raymond Hettinger | 5d19b9a | 2011-11-04 14:05:52 -0700 | [diff] [blame] | 47 | Like file objects, shelve objects should be closed explicitly to ensure |
| 48 | that the persistent data is flushed to disk. |
Georg Brandl | 304d396 | 2009-09-17 16:15:53 +0000 | [diff] [blame] | 49 | |
Georg Brandl | 110953d | 2013-09-22 11:43:10 +0200 | [diff] [blame] | 50 | .. warning:: |
| 51 | |
| 52 | Because the :mod:`shelve` module is backed by :mod:`pickle`, it is insecure |
| 53 | to load a shelf from an untrusted source. Like with pickle, loading a shelf |
| 54 | can execute arbitrary code. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 55 | |
Georg Brandl | d8282ee | 2009-05-08 12:17:34 +0000 | [diff] [blame] | 56 | Shelf objects support all methods supported by dictionaries. This eases the |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | transition from dictionary based scripts to those requiring persistent storage. |
| 58 | |
Georg Brandl | 304d396 | 2009-09-17 16:15:53 +0000 | [diff] [blame] | 59 | Two additional methods are supported: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 60 | |
| 61 | .. method:: Shelf.sync() |
| 62 | |
Georg Brandl | 304d396 | 2009-09-17 16:15:53 +0000 | [diff] [blame] | 63 | Write back all entries in the cache if the shelf was opened with *writeback* |
| 64 | set to :const:`True`. Also empty the cache and synchronize the persistent |
| 65 | dictionary on disk, if feasible. This is called automatically when the shelf |
| 66 | is closed with :meth:`close`. |
| 67 | |
| 68 | .. method:: Shelf.close() |
| 69 | |
| 70 | Synchronize and close the persistent *dict* object. Operations on a closed |
| 71 | shelf will fail with a :exc:`ValueError`. |
| 72 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 73 | |
Raymond Hettinger | 64906bb | 2009-04-04 05:37:47 +0000 | [diff] [blame] | 74 | .. seealso:: |
| 75 | |
| 76 | `Persistent dictionary recipe <http://code.activestate.com/recipes/576642/>`_ |
| 77 | with widely supported storage formats and having the speed of native |
| 78 | dictionaries. |
| 79 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 80 | |
| 81 | Restrictions |
| 82 | ------------ |
| 83 | |
| 84 | .. index:: |
| 85 | module: dbm |
| 86 | module: gdbm |
| 87 | module: bsddb |
| 88 | |
| 89 | * The choice of which database package will be used (such as :mod:`dbm`, |
| 90 | :mod:`gdbm` or :mod:`bsddb`) depends on which interface is available. Therefore |
| 91 | it is not safe to open the database directly using :mod:`dbm`. The database is |
| 92 | also (unfortunately) subject to the limitations of :mod:`dbm`, if it is used --- |
| 93 | this means that (the pickled representation of) the objects stored in the |
| 94 | database should be fairly small, and in rare cases key collisions may cause the |
| 95 | database to refuse updates. |
| 96 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 97 | * The :mod:`shelve` module does not support *concurrent* read/write access to |
| 98 | shelved objects. (Multiple simultaneous read accesses are safe.) When a |
| 99 | program has a shelf open for writing, no other program should have it open for |
| 100 | reading or writing. Unix file locking can be used to solve this, but this |
| 101 | differs across Unix versions and requires knowledge about the database |
| 102 | implementation used. |
| 103 | |
| 104 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 105 | .. class:: Shelf(dict, protocol=None, writeback=False) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 106 | |
| 107 | A subclass of :class:`UserDict.DictMixin` which stores pickled values in the |
| 108 | *dict* object. |
| 109 | |
| 110 | By default, version 0 pickles are used to serialize values. The version of the |
| 111 | pickle protocol can be specified with the *protocol* parameter. See the |
| 112 | :mod:`pickle` documentation for a discussion of the pickle protocols. |
| 113 | |
| 114 | .. versionchanged:: 2.3 |
| 115 | The *protocol* parameter was added. |
| 116 | |
| 117 | If the *writeback* parameter is ``True``, the object will hold a cache of all |
| 118 | entries accessed and write them back to the *dict* at sync and close times. |
| 119 | This allows natural operations on mutable entries, but can consume much more |
| 120 | memory and make sync and close take a long time. |
| 121 | |
| 122 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 123 | .. class:: BsdDbShelf(dict, protocol=None, writeback=False) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 124 | |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 125 | A subclass of :class:`Shelf` which exposes :meth:`first`, :meth:`!next`, |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 126 | :meth:`previous`, :meth:`last` and :meth:`set_location` which are available in |
| 127 | the :mod:`bsddb` module but not in other database modules. The *dict* object |
| 128 | passed to the constructor must support those methods. This is generally |
| 129 | accomplished by calling one of :func:`bsddb.hashopen`, :func:`bsddb.btopen` or |
| 130 | :func:`bsddb.rnopen`. The optional *protocol* and *writeback* parameters have |
| 131 | the same interpretation as for the :class:`Shelf` class. |
| 132 | |
| 133 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 134 | .. class:: DbfilenameShelf(filename, flag='c', protocol=None, writeback=False) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 135 | |
| 136 | A subclass of :class:`Shelf` which accepts a *filename* instead of a dict-like |
| 137 | object. The underlying file will be opened using :func:`anydbm.open`. By |
| 138 | default, the file will be created and opened for both read and write. The |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 139 | optional *flag* parameter has the same interpretation as for the :func:`.open` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | function. The optional *protocol* and *writeback* parameters have the same |
| 141 | interpretation as for the :class:`Shelf` class. |
| 142 | |
| 143 | |
R. David Murray | 7118954 | 2009-05-12 01:36:57 +0000 | [diff] [blame] | 144 | .. _shelve-example: |
| 145 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 146 | Example |
| 147 | ------- |
| 148 | |
| 149 | To summarize the interface (``key`` is a string, ``data`` is an arbitrary |
| 150 | object):: |
| 151 | |
| 152 | import shelve |
| 153 | |
| 154 | d = shelve.open(filename) # open -- file may get suffix added by low-level |
| 155 | # library |
| 156 | |
| 157 | d[key] = data # store data at key (overwrites old data if |
| 158 | # using an existing key) |
| 159 | data = d[key] # retrieve a COPY of data at key (raise KeyError if no |
| 160 | # such key) |
| 161 | del d[key] # delete data stored at key (raises KeyError |
| 162 | # if no such key) |
| 163 | flag = d.has_key(key) # true if the key exists |
| 164 | klist = d.keys() # a list of all existing keys (slow!) |
| 165 | |
| 166 | # as d was opened WITHOUT writeback=True, beware: |
| 167 | d['xx'] = range(4) # this works as expected, but... |
Georg Brandl | 75f1107 | 2009-04-05 10:32:26 +0000 | [diff] [blame] | 168 | d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)! |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 169 | |
| 170 | # having opened d without writeback=True, you need to code carefully: |
| 171 | temp = d['xx'] # extracts the copy |
| 172 | temp.append(5) # mutates the copy |
| 173 | d['xx'] = temp # stores the copy right back, to persist it |
| 174 | |
| 175 | # or, d=shelve.open(filename,writeback=True) would let you just code |
| 176 | # d['xx'].append(5) and have it work as expected, BUT it would also |
| 177 | # consume more memory and make the d.close() operation slower. |
| 178 | |
| 179 | d.close() # close it |
| 180 | |
| 181 | |
| 182 | .. seealso:: |
| 183 | |
| 184 | Module :mod:`anydbm` |
| 185 | Generic interface to ``dbm``\ -style databases. |
| 186 | |
| 187 | Module :mod:`bsddb` |
| 188 | BSD ``db`` database interface. |
| 189 | |
| 190 | Module :mod:`dbhash` |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 191 | Thin layer around the :mod:`bsddb` which provides an :func:`~dbhash.open` |
| 192 | function like the other database modules. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 193 | |
| 194 | Module :mod:`dbm` |
| 195 | Standard Unix database interface. |
| 196 | |
| 197 | Module :mod:`dumbdbm` |
| 198 | Portable implementation of the ``dbm`` interface. |
| 199 | |
| 200 | Module :mod:`gdbm` |
| 201 | GNU database interface, based on the ``dbm`` interface. |
| 202 | |
| 203 | Module :mod:`pickle` |
| 204 | Object serialization used by :mod:`shelve`. |
| 205 | |
| 206 | Module :mod:`cPickle` |
| 207 | High-performance version of :mod:`pickle`. |
| 208 | |