0
0
mirror of https://github.com/python/cpython.git synced 2024-11-27 23:47:29 +01:00
cpython/Lib/shelve.py
Guido van Rossum a48061a580 shelve.py: database of persistent objects, on top of pickle.py and anydbm.py
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)
1995-01-10 00:31:14 +00:00

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)