Skip to content

Commit

Permalink
Merge pull request #667 from sirk390/master
Browse files Browse the repository at this point in the history
-added zrevrangebylex
  • Loading branch information
andymccurdy committed Oct 20, 2015
2 parents 85b8561 + 18893f7 commit 93ee0b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, max, min, start=None, num=None):
"""
Return the reversed lexicographical range of values from sorted set
``name`` between ``max`` and ``min``.
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, max, min]
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):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down

0 comments on commit 93ee0b9

Please sign in to comment.