]> khamidou — GitHub mirrors - cabinet.git/commitdiff
fixed an embarassing assignment bug
authorkhamidou <khamidou@gmail.com>
Tue, 9 Mar 2010 06:38:43 +0000 (07:38 +0100)
committerkhamidou <khamidou@gmail.com>
Tue, 9 Mar 2010 06:38:43 +0000 (07:38 +0100)
README
cabinet.py

diff --git a/README b/README
index a4f0c67e2f4d05e57d1239389572ed9ce3c3b2d4..1237dcfb05318f99c52b008e4752e7ab8998b93e 100644 (file)
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
 Cabinet is a very-thin layer other berkeley db. It serializes python objects to json using simplejson and then saves them to a bsddb backend.
-The library is really simple (at the moment it's less than 30 loc!) so don't hesitate to modify it and send me back patches!
+The library is really simple (at the moment it's less than 100 loc!) so don't hesitate to modify it and send me back patches!
 
 Example session
 ================
index c6aaba052b59a46dc6acbdda4432111e4b273f97..af05ca70811b0d11a3e1f15e0557d09bd3d3b9a4 100644 (file)
@@ -10,18 +10,25 @@ except ImportError:
 class Cabinet(object):
     def __init__(self, filename, *args):
         self.db = bsddb.hashopen(filename)
-    
+        self.cache = {}
+
     def __getitem__(self, key):
-        if self.db.has_key(key):
-            return json.loads(self.db[key])
+        if self.cache.has_key(key):
+            return self.cache[key]
+        elif self.db.has_key(key):
+            self.cache[key] = json.loads(self.db[key])
+            return self.cache[key]
         else:
             raise KeyError
 
     def __setitem__(self, key, item):
-        self.db[key] = json.dumps(item)
+        self.cache[key] = item
 
     def __delitem__(self, key):
         del self.db[key]
 
     def sync(self):
+        for key in self.cache:
+            self.db[key] = json.dumps(self.cache[key])
+
         self.db.sync()