summaryrefslogtreecommitdiffstats
path: root/Lib/shelve.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-01-10 00:31:14 (GMT)
committerGuido van Rossum <guido@python.org>1995-01-10 00:31:14 (GMT)
commita48061a5804418a63aac24bfce444fd555e3ffe7 (patch)
treeb79aefe89c044651dc6ac1f9981c9c7530c0e666 /Lib/shelve.py
parent8a30adc33ffe494f464f1a30c42f28fb3088970a (diff)
downloadcpython-a48061a5804418a63aac24bfce444fd555e3ffe7.zip
cpython-a48061a5804418a63aac24bfce444fd555e3ffe7.tar.gz
cpython-a48061a5804418a63aac24bfce444fd555e3ffe7.tar.bz2
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)
Diffstat (limited to 'Lib/shelve.py')
-rw-r--r--Lib/shelve.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/Lib/shelve.py b/Lib/shelve.py
new file mode 100644
index 0000000..b90e641
--- /dev/null
+++ b/Lib/shelve.py
@@ -0,0 +1,43 @@
+"""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)