From c1a53443c1a05295efbf1fcac875bd6bf132e7aa Mon Sep 17 00:00:00 2001 From: Christian Bodt Date: Tue, 20 Oct 2015 12:33:20 +0200 Subject: [PATCH 1/2] -added zrevrangebylex --- redis/client.py | 16 ++++++++++++++++ tests/test_commands.py | 11 +++++++++++ 2 files changed, 27 insertions(+) diff --git a/redis/client.py b/redis/client.py index 902ccc9bc2..6e4c9abe09 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1661,6 +1661,22 @@ def zrangebylex(self, name, min, max, start=None, num=None): pieces.extend([Token('LIMIT'), start, num]) return self.execute_command(*pieces) + def zrevrangebylex(self, name, upper, lower, start=None, num=None): + """ + Return the reversed lexicographical range of values from sorted set + ``name`` between ``upper`` and ``lower``. + + If ``start`` and ``num`` are specified, then return a slice of the + range. + """ + if (start is not None and num is None) or \ + (num is not None and start is None): + raise RedisError("``start`` and ``num`` must both be specified") + pieces = ['ZREVRANGEBYLEX', name, upper, lower] + if start is not None and num is not None: + pieces.extend([Token('LIMIT'), start, num]) + return self.execute_command(*pieces) + def zrangebyscore(self, name, min, max, start=None, num=None, withscores=False, score_cast_func=float): """ diff --git a/tests/test_commands.py b/tests/test_commands.py index c0e56b0390..45206b2e7f 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -959,6 +959,17 @@ def test_zrangebylex(self, r): assert r.zrangebylex('a', '[f', '+') == [b('f'), b('g')] assert r.zrangebylex('a', '-', '+', start=3, num=2) == [b('d'), b('e')] + @skip_if_server_version_lt('2.9.9') + def test_zrevrangebylex(self, r): + r.zadd('a', a=0, b=0, c=0, d=0, e=0, f=0, g=0) + assert r.zrevrangebylex('a', '[c', '-') == [b('c'), b('b'), b('a')] + assert r.zrevrangebylex('a', '(c', '-') == [b('b'), b('a')] + assert r.zrevrangebylex('a', '(g', '[aaa') == \ + [b('f'), b('e'), b('d'), b('c'), b('b')] + assert r.zrevrangebylex('a', '+', '[f') == [b('g'), b('f')] + assert r.zrevrangebylex('a', '+', '-', start=3, num=2) == \ + [b('d'), b('c')] + def test_zrangebyscore(self, r): r.zadd('a', a1=1, a2=2, a3=3, a4=4, a5=5) assert r.zrangebyscore('a', 2, 4) == [b('a2'), b('a3'), b('a4')] From 18893f74950ba957f7ca746296121e2948713374 Mon Sep 17 00:00:00 2001 From: Christian Bodt Date: Tue, 20 Oct 2015 13:33:31 +0200 Subject: [PATCH 2/2] -Add CHANGES message. -Rename variables for coherence --- CHANGES | 1 + redis/client.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 44c17df31b..2aab74dc68 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,7 @@ get stuck in an endless loop if a specific number of bytes were delivered from the socket. This fix also increases performance of parsing large responses from the Redis server. + * Added support for ZREVRANGEBYLEX. * 2.10.3 * Fixed a bug with the bytearray support introduced in 2.10.2. Thanks Josh Owen. diff --git a/redis/client.py b/redis/client.py index 6e4c9abe09..3ac242b2bc 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1661,10 +1661,10 @@ def zrangebylex(self, name, min, max, start=None, num=None): pieces.extend([Token('LIMIT'), start, num]) return self.execute_command(*pieces) - def zrevrangebylex(self, name, upper, lower, start=None, num=None): + def zrevrangebylex(self, name, max, min, start=None, num=None): """ Return the reversed lexicographical range of values from sorted set - ``name`` between ``upper`` and ``lower``. + ``name`` between ``max`` and ``min``. If ``start`` and ``num`` are specified, then return a slice of the range. @@ -1672,7 +1672,7 @@ def zrevrangebylex(self, name, upper, lower, start=None, num=None): if (start is not None and num is None) or \ (num is not None and start is None): raise RedisError("``start`` and ``num`` must both be specified") - pieces = ['ZREVRANGEBYLEX', name, upper, lower] + pieces = ['ZREVRANGEBYLEX', name, max, min] if start is not None and num is not None: pieces.extend([Token('LIMIT'), start, num]) return self.execute_command(*pieces)