def multi(self): """ Start a transactional block of the pipeline after WATCH commands are issued. End the transactional block with `execute`. """ if self.explicit_transaction: raise RedisError("Cannot issue nested calls to MULTI") if self.commands: raise RedisError("Commands without an initial WATCH have already been issued") self.explicit_transaction = True
def zrevrangebyscore(self, name, max_, min_, start=None, num=None, withscores=False, score_cast_func=float): if (start is None) ^ (num is None): raise RedisError('`start` and `num` must both be specified') zset = self._get_zset(name, "ZREVRANGEBYSCORE") if not zset: return [] func = self._range_func(withscores, score_cast_func) include_start, min_ = self._score_inclusive(min_) include_end, max_ = self._score_inclusive(max_) scorerange = [ x for x in reversed( zset.scorerange(float(min_), float(max_), start_inclusive=include_start, end_inclusive=include_end)) ] if start is not None and num is not None: start, num = self._translate_limit(len(scorerange), int(start), int(num)) scorerange = scorerange[start:start + num] return [func(item) for item in scorerange]
def watch(self, *keys): """ Put the pipeline into immediate execution mode. Does not actually watch any keys. """ if self.explicit_transaction: raise RedisError("Cannot issue a WATCH after a MULTI") self.watching = True
def evalsha(self, sha, numkeys, *keys_and_args): """Emulates evalsha""" if not self.script_exists(sha)[0]: raise RedisError("Sha not registered") script_callable = Script(self, self.shas[sha], self.load_lua_dependencies) numkeys = max(numkeys, 0) keys = keys_and_args[:numkeys] args = keys_and_args[numkeys:] return script_callable(keys, args)
def watch(self, *keys): """ Put the pipeline into immediate execution mode. Does not actually watch any keys. """ if self.explicit_transaction: raise RedisError("Cannot issue a WATCH after a MULTI") self.watching = True for key in keys: self._watched_keys[key] = deepcopy(self.mock_redis.redis.get(key))
def mset(self, *args, **kwargs): """ Sets key/values based on a mapping. Mapping can be supplied as a single dictionary argument or as kwargs. """ if args: if len(args) != 1 or not isinstance(args[0], dict): raise RedisError('MSET requires **kwargs or a single dict arg') mapping = args[0] else: mapping = kwargs for key, value in mapping.items(): self.set(key, value) return True
def msetnx(self, *args, **kwargs): """ Sets key/values based on a mapping if none of the keys are already set. Mapping can be supplied as a single dictionary argument or as kwargs. Returns a boolean indicating if the operation was successful. """ if args: if len(args) != 1 or not isinstance(args[0], dict): raise RedisError('MSETNX requires **kwargs or a single dict arg') mapping = args[0] else: mapping = kwargs for key in mapping.keys(): if self._encode(key) in self.redis: return False for key, value in mapping.items(): self.set(key, value) return True
def zadd(self, name, *args, **kwargs): zset = self._get_zset(name, "ZADD", create=True) pieces = [] # args if len(args) % 2 != 0: raise RedisError("ZADD requires an equal number of " "values and scores") for i in xrange(len(args) // 2): # interpretation of args order depends on whether Redis # or StrictRedis is used score = args[2 * i + (0 if self.strict else 1)] member = args[2 * i + (1 if self.strict else 0)] pieces.append((member, score)) # kwargs pieces.extend(kwargs.items()) insert_count = lambda member, score: 1 if zset.insert(self._encode(member), float(score)) else 0 # noqa return sum((insert_count(member, score) for member, score in pieces))