def _load(self): ''' Moves snapshots from db into the cache ''' logging.info("Loading game history from database ...") if Snapshot.by_id(1) is None: self.__now__() # Take starting snapshot self.epoch = Snapshot.by_id(1).created try: max_index = len(self) start_index = 1 if len(self) <= 10 else max_index - 9 for index in range(start_index, max_index + 1): snapshot = Snapshot.by_id(index) if not snapshot.key in self.cache: logging.info("Cached snapshot (%d of %d)" % (snapshot.id, max_index)) self.cache.set(snapshot.key, snapshot.to_dict()) logging.info("History load complete.") except KeyboardInterrupt: logging.info("History load stopped by user.")
def _load(self): ''' Moves snapshots from db into the cache ''' logging.info("Loading game history from database ...") if Snapshot.by_id(1) is None: self.__now__() # Take starting snapshot self.epoch = Snapshot.by_id(1).created try: max_index = len(self) start_index = 1 if len(self) <= 10 else max_index - 9 for index in range(start_index, max_index + 1): snapshot = Snapshot.by_id(index) if not snapshot.key in self.cache: logging.info("Cached snapshot (%d of %d)" % ( snapshot.id, max_index )) self.cache.set(snapshot.key, snapshot.to_dict()) logging.info("History load complete.") except KeyboardInterrupt: logging.info("History load stopped by user.")
def __at__(self, index): ''' Get snapshot at specific index ''' key = Snapshot.to_key(index + 1) if key in self.cache: return self.cache.get(key) else: snapshot = Snapshot.by_id(index + 1) if snapshot is not None: self.cache.set(snapshot.key, snapshot.to_dict()) return snapshot.to_dict() return None
def __contains__(self, index): return True if Snapshot.by_id(index) is not None else False