1
from shelve import Shelf
5
# Some python installations don't have zlib.
8
from cStringIO import StringIO
11
class DbfilenameShelf(Shelf):
12
"""Shelf implementation using the "anydbm" generic dbm interface.
14
This is initialized with the filename for the dbm database.
15
See the module's __doc__ string for an overview of the interface.
18
def __init__(self, filename, flag='c'):
19
import dumbdbm_patched
20
Shelf.__init__(self, dumbdbm_patched.open(filename, flag))
22
def __getitem__(self, key):
23
compressed = self.dict[key]
25
r = zlib.decompress(compressed)
31
return cPickle.loads(r)
33
def __setitem__(self, key, value):
34
s = cPickle.dumps(value,1)
36
self.dict[key] = zlib.compress(s)
38
#zlib doesn't exist, leave it uncompressed.
41
def open(filename, flag='c'):
42
"""Open a persistent dictionary for reading and writing.
44
Argument is the filename for the dbm database.
45
See the module's __doc__ string for an overview of the interface.
48
return DbfilenameShelf(filename, flag)