[Assorted-commits] SF.net SVN: assorted:[1447] python-commons/trunk/src/commons/sqlhash.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-06-03 17:09:36
|
Revision: 1447 http://assorted.svn.sourceforge.net/assorted/?rev=1447&view=rev Author: yangzhang Date: 2009-06-03 17:09:35 +0000 (Wed, 03 Jun 2009) Log Message: ----------- fixes and tweaks to shelf Modified Paths: -------------- python-commons/trunk/src/commons/sqlhash.py Modified: python-commons/trunk/src/commons/sqlhash.py =================================================================== --- python-commons/trunk/src/commons/sqlhash.py 2009-06-03 17:08:19 UTC (rev 1446) +++ python-commons/trunk/src/commons/sqlhash.py 2009-06-03 17:09:35 UTC (rev 1447) @@ -64,7 +64,7 @@ item = self.conn.execute(GET_ITEM, (sqlite3.Binary(key),)).fetchone() if item is None: raise KeyError(key) - return item[0] + return str(item[0]) def __setitem__(self, key, value): ADD_ITEM = 'REPLACE INTO shelf (key, value) VALUES (?,?)' @@ -131,17 +131,40 @@ return SQLhash() class Shelf(shelve.Shelf): + def __init__(self, *args, **okwargs): + kwargs = okwargs.copy() + try: del kwargs['cache'] + except KeyError: pass + shelve.Shelf.__init__(self, *args, **kwargs) + if not okwargs.get('cache', True): self.cache = None + def __getitem__(self, k): + val = lambda: cPickle.loads(self.dict[k]) + if self.cache is None: + return val() + else: + try: return self.cache[k] + except KeyError: return self.cache.setdefault(k, val()) def __setitem__(self, key, value): - if self.writeback: self.cache[key] = value + if self.cache is not None and self.writeback: self.cache[key] = value else: self.dict[key] = cPickle.dumps(value, self._protocol) def __delitem__(self, key): try: del self.dict[key] except: + if not self.writeback: raise + # If writeback is enabled then it's OK if it's missing in the + # underlying dict but present in the cache; it just means that + # we only recently inserted it so it's only in the cache. If + # it's not in the cache either then we should end with a + # KeyError. del self.cache[key] else: + # Also make sure it's removed from the cache. try: del self.cache[key] except KeyError: pass + def iteritems(self): + for i,k in enumerate(self.keys()): + yield k, self[k] def sync(self): if self.cache: self.dict.update( (k, cPickle.dumps(v, self._protocol)) for k,v in self.cache.items() ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |