Skip to content

Commit ea04bae

Browse files
Restore zrange functionality for older versions of Redis (#1670)
1 parent bba7518 commit ea04bae

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

redis/commands/core.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,9 +2536,14 @@ def zrevrange(self, name, start, end, withscores=False,
25362536
25372537
``score_cast_func`` a callable used to cast the score return value
25382538
"""
2539-
return self.zrange(name, start, end, desc=True,
2540-
withscores=withscores,
2541-
score_cast_func=score_cast_func)
2539+
pieces = ['ZREVRANGE', name, start, end]
2540+
if withscores:
2541+
pieces.append(b'WITHSCORES')
2542+
options = {
2543+
'withscores': withscores,
2544+
'score_cast_func': score_cast_func
2545+
}
2546+
return self.execute_command(*pieces, **options)
25422547

25432548
def zrangestore(self, dest, name, start, end,
25442549
byscore=False, bylex=False, desc=False,
@@ -2575,7 +2580,13 @@ def zrangebylex(self, name, min, max, start=None, num=None):
25752580
If ``start`` and ``num`` are specified, then return a slice of the
25762581
range.
25772582
"""
2578-
return self.zrange(name, min, max, bylex=True, offset=start, num=num)
2583+
if (start is not None and num is None) or \
2584+
(num is not None and start is None):
2585+
raise DataError("``start`` and ``num`` must both be specified")
2586+
pieces = ['ZRANGEBYLEX', name, min, max]
2587+
if start is not None and num is not None:
2588+
pieces.extend([b'LIMIT', start, num])
2589+
return self.execute_command(*pieces)
25792590

25802591
def zrevrangebylex(self, name, max, min, start=None, num=None):
25812592
"""
@@ -2585,8 +2596,13 @@ def zrevrangebylex(self, name, max, min, start=None, num=None):
25852596
If ``start`` and ``num`` are specified, then return a slice of the
25862597
range.
25872598
"""
2588-
return self.zrange(name, max, min, desc=True,
2589-
bylex=True, offset=start, num=num)
2599+
if (start is not None and num is None) or \
2600+
(num is not None and start is None):
2601+
raise DataError("``start`` and ``num`` must both be specified")
2602+
pieces = ['ZREVRANGEBYLEX', name, max, min]
2603+
if start is not None and num is not None:
2604+
pieces.extend(['LIMIT', start, num])
2605+
return self.execute_command(*pieces)
25902606

25912607
def zrangebyscore(self, name, min, max, start=None, num=None,
25922608
withscores=False, score_cast_func=float):
@@ -2602,10 +2618,19 @@ def zrangebyscore(self, name, min, max, start=None, num=None,
26022618
26032619
`score_cast_func`` a callable used to cast the score return value
26042620
"""
2605-
return self.zrange(name, min, max, byscore=True,
2606-
offset=start, num=num,
2607-
withscores=withscores,
2608-
score_cast_func=score_cast_func)
2621+
if (start is not None and num is None) or \
2622+
(num is not None and start is None):
2623+
raise DataError("``start`` and ``num`` must both be specified")
2624+
pieces = ['ZRANGEBYSCORE', name, min, max]
2625+
if start is not None and num is not None:
2626+
pieces.extend(['LIMIT', start, num])
2627+
if withscores:
2628+
pieces.append('WITHSCORES')
2629+
options = {
2630+
'withscores': withscores,
2631+
'score_cast_func': score_cast_func
2632+
}
2633+
return self.execute_command(*pieces, **options)
26092634

26102635
def zrevrangebyscore(self, name, max, min, start=None, num=None,
26112636
withscores=False, score_cast_func=float):
@@ -2621,10 +2646,19 @@ def zrevrangebyscore(self, name, max, min, start=None, num=None,
26212646
26222647
``score_cast_func`` a callable used to cast the score return value
26232648
"""
2624-
return self.zrange(name, max, min, desc=True,
2625-
byscore=True, offset=start,
2626-
num=num, withscores=withscores,
2627-
score_cast_func=score_cast_func)
2649+
if (start is not None and num is None) or \
2650+
(num is not None and start is None):
2651+
raise DataError("``start`` and ``num`` must both be specified")
2652+
pieces = ['ZREVRANGEBYSCORE', name, max, min]
2653+
if start is not None and num is not None:
2654+
pieces.extend(['LIMIT', start, num])
2655+
if withscores:
2656+
pieces.append('WITHSCORES')
2657+
options = {
2658+
'withscores': withscores,
2659+
'score_cast_func': score_cast_func
2660+
}
2661+
return self.execute_command(*pieces, **options)
26282662

26292663
def zrank(self, name, value):
26302664
"""

tests/test_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ def test_zrange(self, r):
18661866
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
18671867
assert r.zrange('a', 0, 1) == [b'a1', b'a2']
18681868
assert r.zrange('a', 1, 2) == [b'a2', b'a3']
1869-
assert r.zrange('a', 0, 2, desc=True) == [b'a3', b'a2', b'a1']
1869+
assert r.zrange('a', 0, 2) == [b'a1', b'a2', b'a3']
18701870

18711871
# withscores
18721872
assert r.zrange('a', 0, 1, withscores=True) == \

0 commit comments

Comments
 (0)