- Timestamp:
- 04/05/13 15:39:57 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/memoize.py
r656 r657 316 316 def has_key(self, key): 317 317 return bool(self._select_key_items(key)) 318 def get(self, key, value ):318 def get(self, key, value=None): 319 319 res = self._select_key_items(key) 320 rv = value 321 if res: rv = res[-1][-1] 322 return rv 320 if res: value = res[-1][-1] 321 return value 322 def pop(self, key, *value): 323 L = len(value) 324 if L > 1: 325 raise TypeError, "pop expected at most 2 arguments, got %s" % L+1 326 res = self._select_key_items(key) 327 if res: 328 _value = res[-1][-1] 329 else: 330 if not L: raise KeyError, key 331 _value = value[0] 332 sql = "delete from %s where argstr = ?" % self._table 333 self._curs.execute(sql, (key,)) 334 self._conn.commit() 335 return _value 336 def clear(self): 337 [self.pop(k) for k in self.keys()] # better delete table, add empty ? 338 return 339 def update(self, adict, **kwds): 340 _dict = adict.copy() 341 _dict.update(**kwds) 342 [self.__setitem__(k,v) for (k,v) in _dict.items()] 343 return 344 def iteritems(self): 345 return ((k,self.__getitem__(k)) for k in self.__iter__()) 323 346 def items(self): 324 return self.__asdict__().items() 347 #return self.__asdict__().items() 348 return list(self.iteritems()) 325 349 def keys(self): 326 return self.__asdict__().keys() 350 #return self.__asdict__().keys() 351 return list(self.__iter__()) 352 def itervalues(self): 353 return (self.__getitem__(k) for k in self.__iter__()) 327 354 def values(self): 328 return self.__asdict__().values() 355 #return self.__asdict__().values() 356 return list(self.itervalues()) 329 357 def __repr__(self): 330 return "memo(%s)" % self.__asdict__() 358 return "%s(%s)" % (self._table, self.__asdict__()) 359 def __iter__(self): 360 sql = "select argstr from %s" % self._table 361 return (k[-1] for k in set(self._curs.execute(sql))) 362 iterkeys = __iter__ 331 363 def _select_key_items(self, key): 332 364 '''Return a tuple of (key, value) pairs that match the specified key. 333 365 ''' 334 366 sql = "select * from %s where argstr = ?" % self._table 335 res = tuple(self._curs.execute(sql, (key,))) 336 return res 367 return tuple(self._curs.execute(sql, (key,))) 337 368 pass 338 369
Note: See TracChangeset
for help on using the changeset viewer.