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