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.
36 """Base class for shelf implementations.
38 This is initialized with a dictionary-like object.
39 See the module's __doc__ string for an overview of the interface.
42 def __init__(self
, dict):
46 return self
.dict.keys()
49 return self
.dict.len()
51 def has_key(self
, key
):
52 return self
.dict.has_key(key
)
54 def __getitem__(self
, key
):
55 f
= StringIO
.StringIO(self
.dict[key
])
56 return pickle
.Unpickler(f
).load()
58 def __setitem__(self
, key
, value
):
59 f
= StringIO
.StringIO()
62 self
.dict[key
] = f
.getvalue()
64 def __delitem__(self
, key
):
68 if hasattr(self
.dict, 'close'):
76 class BsdDbShelf(Shelf
):
77 """Shelf implementation using the "BSD" db interface.
79 The actual database is opened using one of thethe "bsddb" modules
80 "open" routines (i.e. bsddb.hashopen, bsddb.btopen or bsddb.rnopen.)
82 This class is initialized with the the database object
83 returned from one of the bsddb open functions.
85 See the module's __doc__ string for an overview of the interface.
88 def __init__(self
, dict):
89 Shelf
.__init
__(self
, dict)
91 def set_location(self
, key
):
92 (key
, value
) = self
.dict.set_location(key
)
93 f
= StringIO
.StringIO(value
)
94 return (key
, pickle
.Unpickler(f
).load())
97 (key
, value
) = self
.dict.next()
98 f
= StringIO
.StringIO(value
)
99 return (key
, pickle
.Unpickler(f
).load())
102 (key
, value
) = self
.dict.previous()
103 f
= StringIO
.StringIO(value
)
104 return (key
, pickle
.Unpickler(f
).load())
107 (key
, value
) = self
.dict.first()
108 f
= StringIO
.StringIO(value
)
109 return (key
, pickle
.Unpickler(f
).load())
112 (key
, value
) = self
.dict.last()
113 f
= StringIO
.StringIO(value
)
114 return (key
, pickle
.Unpickler(f
).load())
117 class DbfilenameShelf(Shelf
):
118 """Shelf implementation using the "anydbm" generic dbm interface.
120 This is initialized with the filename for the dbm database.
121 See the module's __doc__ string for an overview of the interface.
124 def __init__(self
, filename
, flag
='c'):
126 Shelf
.__init
__(self
, anydbm
.open(filename
, flag
))
129 def open(filename
, flag
='c'):
130 """Open a persistent dictionary for reading and writing.
132 Argument is the filename for the dbm database.
133 See the module's __doc__ string for an overview of the interface.
136 return DbfilenameShelf(filename
, flag
)