def call(self, bot, msg, target, exclude): time = datetime.now() cached = Cache.get([self.cache_key, self.type, self.id]) if cached: last_activity = datetime.fromtimestamp(cached) else: last_activity = datetime.fromtimestamp(0) if (last_activity + self.timedelta) <= time: Cache.put([self.cache_key, self.type, self.id], time.timestamp()) return self.propagate(bot, msg, target, exclude) else: raise FilterException()
def _select_random_id(self, exclude): ''' Select a random id excluding the ones in `exclude`. ''' options = Cache.get(self._id) size = len(options) if size == 1: return next(iter(options)) if len(exclude) is size: exclude = [] keys = list(options.keys()) while True: rand = random.randrange(size) id = keys[rand] if id not in exclude: return id
def list_options(self): vals = Cache.get(self._id) return [vals[key] for key in vals]
def select_random_option(self, exclude=None): id = self._select_random_id(exclude if exclude else []) val = Cache.get([self._id, id]) return (id, val)