1 """Manage shelves of pickled objects.
3 A "shelf" is a persistent, dictionary-like object. The difference
4 with dbm databases is that the values (not the keys!) in a shelf can
5 be essentially arbitrary Python objects -- anything that the "pickle"
6 module can handle. This includes most class instances, recursive data
7 types, and objects containing lots of shared sub-objects. The keys
10 To summarize the interface (key is a string, data is an arbitrary
14 d = shelve.open(filename) # open, with (g)dbm filename -- no suffix
16 d[key] = data # store data at key (overwrites old data if
17 # using an existing key)
18 data = d[key] # retrieve a COPY of the data at key (raise
19 # KeyError if no such key) -- NOTE that this
20 # access returns a *copy* of the entry!
21 del d[key] # delete data stored at key (raises KeyError
23 flag = d.has_key(key) # true if the key exists; same as "key in d"
24 list = d.keys() # a list of all existing keys (slow!)
28 Dependent on the implementation, closing a persistent dictionary may
29 or may not be necessary to flush changes to disk.
31 Normally, d[key] returns a COPY of the entry. This needs care when
32 mutable entries are mutated: for example, if d[key] is a list,
34 does NOT modify the entry d[key] itself, as stored in the persistent
35 mapping -- it only modifies the copy, which is then immediately
36 discarded, so that the append has NO effect whatsoever. To append an
37 item to d[key] in a way that will affect the persistent mapping, use:
42 To avoid the problem with mutable entries, you may pass the keyword
43 argument writeback=True in the call to shelve.open. When you use:
44 d = shelve.open(filename, writeback=True)
45 then d keeps a cache of all entries you access, and writes them all back
46 to the persistent mapping when you call d.close(). This ensures that
47 such usage as d[key].append(anitem) works as intended.
49 However, using keyword argument writeback=True may consume vast amount
50 of memory for the cache, and it may make d.close() very slow, if you
51 access many of d's entries after opening it in this way: d has no way to
52 check which of the entries you access are mutable and/or which ones you
53 actually mutate, so it must cache, and write back at close, all of the
54 entries that you access. You can call d.sync() to write back all the
55 entries in the cache, and empty the cache (d.sync() also synchronizes
56 the persistent dictionary on disk, if feasible).
59 # Try using cPickle and cStringIO if available.
62 from cPickle
import Pickler
, Unpickler
64 from pickle
import Pickler
, Unpickler
67 from cStringIO
import StringIO
69 from StringIO
import StringIO
74 __all__
= ["Shelf","BsdDbShelf","DbfilenameShelf","open"]
76 class Shelf(UserDict
.DictMixin
):
77 """Base class for shelf implementations.
79 This is initialized with a dictionary-like object.
80 See the module's __doc__ string for an overview of the interface.
83 def __init__(self
, dict, protocol
=None, writeback
=False, binary
=None):
85 if protocol
is not None and binary
is not None:
86 raise ValueError, "can't specify both 'protocol' and 'binary'"
87 if binary
is not None:
88 warnings
.warn("The 'binary' argument to Shelf() is deprecated",
89 PendingDeprecationWarning
)
90 protocol
= int(binary
)
93 self
._protocol
= protocol
94 self
.writeback
= writeback
98 return self
.dict.keys()
101 return len(self
.dict)
103 def has_key(self
, key
):
104 return self
.dict.has_key(key
)
106 def __contains__(self
, key
):
107 return self
.dict.has_key(key
)
109 def get(self
, key
, default
=None):
110 if self
.dict.has_key(key
):
114 def __getitem__(self
, key
):
116 value
= self
.cache
[key
]
118 f
= StringIO(self
.dict[key
])
119 value
= Unpickler(f
).load()
121 self
.cache
[key
] = value
124 def __setitem__(self
, key
, value
):
126 self
.cache
[key
] = value
128 p
= Pickler(f
, self
._protocol
)
130 self
.dict[key
] = f
.getvalue()
132 def __delitem__(self
, key
):
143 except AttributeError:
151 if self
.writeback
and self
.cache
:
152 self
.writeback
= False
153 for key
, entry
in self
.cache
.iteritems():
155 self
.writeback
= True
157 if hasattr(self
.dict, 'sync'):
161 class BsdDbShelf(Shelf
):
162 """Shelf implementation using the "BSD" db interface.
164 This adds methods first(), next(), previous(), last() and
165 set_location() that have no counterpart in [g]dbm databases.
167 The actual database must be opened using one of the "bsddb"
168 modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or
169 bsddb.rnopen) and passed to the constructor.
171 See the module's __doc__ string for an overview of the interface.
174 def __init__(self
, dict, protocol
=None, writeback
=False, binary
=None):
175 Shelf
.__init
__(self
, dict, protocol
, writeback
, binary
)
177 def set_location(self
, key
):
178 (key
, value
) = self
.dict.set_location(key
)
180 return (key
, Unpickler(f
).load())
183 (key
, value
) = self
.dict.next()
185 return (key
, Unpickler(f
).load())
188 (key
, value
) = self
.dict.previous()
190 return (key
, Unpickler(f
).load())
193 (key
, value
) = self
.dict.first()
195 return (key
, Unpickler(f
).load())
198 (key
, value
) = self
.dict.last()
200 return (key
, Unpickler(f
).load())
203 class DbfilenameShelf(Shelf
):
204 """Shelf implementation using the "anydbm" generic dbm interface.
206 This is initialized with the filename for the dbm database.
207 See the module's __doc__ string for an overview of the interface.
210 def __init__(self
, filename
, flag
='c', protocol
=None, writeback
=False, binary
=None):
212 Shelf
.__init
__(self
, anydbm
.open(filename
, flag
), protocol
, writeback
, binary
)
215 def open(filename
, flag
='c', protocol
=None, writeback
=False, binary
=None):
216 """Open a persistent dictionary for reading and writing.
218 The filename parameter is the base filename for the underlying
219 database. As a side-effect, an extension may be added to the
220 filename and more than one file may be created. The optional flag
221 parameter has the same interpretation as the flag parameter of
222 anydbm.open(). The optional protocol parameter specifies the
223 version of the pickle protocol (0, 1, or 2).
225 The optional binary parameter is deprecated and may be set to True
226 to force the use of binary pickles for serializing data values.
228 See the module's __doc__ string for an overview of the interface.
231 return DbfilenameShelf(filename
, flag
, protocol
, writeback
, binary
)