- Timestamp:
- 04/05/13 11:11:26 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/decorate/memoize.py
r655 r656 296 296 self._conn = db.connect(database) 297 297 self._curs = self._conn.cursor() 298 sql = "create table %s(argstr, fval)" % table298 sql = "create table if not exists %s(argstr, fval)" % table 299 299 self._curs.execute(sql) 300 300 return … … 305 305 return 306 306 def __getitem__(self, key): 307 sql = "select * from %s where argstr = ?" % self._table 308 res = tuple(self._curs.execute(sql, (key,))) 307 res = self._select_key_items(key) 309 308 if res: return res[-1][-1] # always get the last one 310 309 raise KeyError, key … … 316 315 return d 317 316 def has_key(self, key): 318 return key in self.keys()317 return bool(self._select_key_items(key)) 319 318 def get(self, key, value): 320 return self.__asdict__().get(key, value) 319 res = self._select_key_items(key) 320 rv = value 321 if res: rv = res[-1][-1] 322 return rv 321 323 def items(self): 322 324 return self.__asdict__().items() … … 327 329 def __repr__(self): 328 330 return "memo(%s)" % self.__asdict__() 331 def _select_key_items(self, key): 332 '''Return a tuple of (key, value) pairs that match the specified key. 333 ''' 334 sql = "select * from %s where argstr = ?" % self._table 335 res = tuple(self._curs.execute(sql, (key,))) 336 return res 329 337 pass 330 338
Note: See TracChangeset
for help on using the changeset viewer.