mirror of
https://github.com/python/cpython.git
synced 2024-11-27 23:47:29 +01:00
a48061a580
pickle.py: new low-level persistency module (used to be called flatten) dbmac.py: stupid dbm clone for the Mac anydbm.py: generic dbm interface (should be extended to support gdbm)
44 lines
775 B
Python
44 lines
775 B
Python
"""Manage shelves of pickled objects."""
|
|
|
|
import pickle
|
|
import StringIO
|
|
|
|
class Shelf:
|
|
|
|
def __init__(self, dict):
|
|
self.dict = dict
|
|
|
|
def keys(self):
|
|
return self.dict.keys()
|
|
|
|
def __len__(self):
|
|
return self.dict.len()
|
|
|
|
def has_key(self, key):
|
|
return self.dict.has_key(key)
|
|
|
|
def __getitem__(self, key):
|
|
return pickle.Unpickler(StringIO.StringIO(self.dict[key])).load()
|
|
|
|
def __setitem__(self, key, value):
|
|
f = StringIO.StringIO()
|
|
p = pickle.Pickler(f)
|
|
p.dump(value)
|
|
self.dict[key] = f.getvalue()
|
|
|
|
def __delitem__(self, key):
|
|
del self.dict[key]
|
|
|
|
def close(self):
|
|
self.db.close()
|
|
self.db = None
|
|
|
|
class DbShelf(Shelf):
|
|
|
|
def __init__(self, file):
|
|
import anydbm
|
|
Shelf.__init__(self, anydbm.open(file))
|
|
|
|
def open(file):
|
|
return DbShelf(file)
|