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 data at key (raise KeyError if no
20 del d[key] # delete data stored at key (raises KeyError
22 flag = d.has_key(key) # true if the key exists
23 list = d.keys() # a list of all existing keys (slow!)
27 Dependent on the implementation, closing a persistent dictionary may
28 or may not be necessary to flush changes to disk.
31 # Try using cPickle and cStringIO if available.
34 from cPickle
import Pickler
, Unpickler
36 from pickle
import Pickler
, Unpickler
39 from cStringIO
import StringIO
41 from StringIO
import StringIO
45 """Base class for shelf implementations.
47 This is initialized with a dictionary-like object.
48 See the module's __doc__ string for an overview of the interface.
51 def __init__(self
, dict):
55 return self
.dict.keys()
60 def has_key(self
, key
):
61 return self
.dict.has_key(key
)
63 def get(self
, key
, default
=None):
64 if self
.dict.has_key(key
):
68 def __getitem__(self
, key
):
69 f
= StringIO(self
.dict[key
])
70 return Unpickler(f
).load()
72 def __setitem__(self
, key
, value
):
76 self
.dict[key
] = f
.getvalue()
78 def __delitem__(self
, key
):
92 if hasattr(self
.dict, 'sync'):
96 class BsdDbShelf(Shelf
):
97 """Shelf implementation using the "BSD" db interface.
99 This adds methods first(), next(), previous(), last() and
100 set_location() that have no counterpart in [g]dbm databases.
102 The actual database must be opened using one of the "bsddb"
103 modules "open" routines (i.e. bsddb.hashopen, bsddb.btopen or
104 bsddb.rnopen) and passed to the constructor.
106 See the module's __doc__ string for an overview of the interface.
109 def __init__(self
, dict):
110 Shelf
.__init
__(self
, dict)
112 def set_location(self
, key
):
113 (key
, value
) = self
.dict.set_location(key
)
115 return (key
, Unpickler(f
).load())
118 (key
, value
) = self
.dict.next()
120 return (key
, Unpickler(f
).load())
123 (key
, value
) = self
.dict.previous()
125 return (key
, Unpickler(f
).load())
128 (key
, value
) = self
.dict.first()
130 return (key
, Unpickler(f
).load())
133 (key
, value
) = self
.dict.last()
135 return (key
, Unpickler(f
).load())
138 class DbfilenameShelf(Shelf
):
139 """Shelf implementation using the "anydbm" generic dbm interface.
141 This is initialized with the filename for the dbm database.
142 See the module's __doc__ string for an overview of the interface.
145 def __init__(self
, filename
, flag
='c'):
147 Shelf
.__init
__(self
, anydbm
.open(filename
, flag
))
150 def open(filename
, flag
='c'):
151 """Open a persistent dictionary for reading and writing.
153 Argument is the filename for the dbm database.
154 See the module's __doc__ string for an overview of the interface.
157 return DbfilenameShelf(filename
, flag
)