Skip to content

Commit

Permalink
-added zrevrangebylex
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bodt committed Oct 20, 2015
1 parent 85b8561 commit c1a5344
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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, 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):
"""
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 c1a5344

Please sign in to comment.