Changeset 656 for branches


Ignore:
Timestamp:
04/05/13 11:11:26 (3 years ago)
Author:
juhas
Message:

The memo table is created only if it does not yet exist in the database file.
The database file could not be reused otherwise.

Optimized the has_key and get methods to work without calling asdict.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/decorate/memoize.py

    r655 r656  
    296296        self._conn = db.connect(database) 
    297297        self._curs = self._conn.cursor() 
    298         sql = "create table %s(argstr, fval)" % table 
     298        sql = "create table if not exists %s(argstr, fval)" % table 
    299299        self._curs.execute(sql) 
    300300        return 
     
    305305        return 
    306306    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) 
    309308        if res: return res[-1][-1] # always get the last one 
    310309        raise KeyError, key 
     
    316315        return d 
    317316    def has_key(self, key): 
    318         return key in self.keys() 
     317        return bool(self._select_key_items(key)) 
    319318    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 
    321323    def items(self): 
    322324        return self.__asdict__().items() 
     
    327329    def __repr__(self): 
    328330        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 
    329337    pass 
    330338 
Note: See TracChangeset for help on using the changeset viewer.