Skip to content

Commit fc69bd6

Browse files
zdiff and zdiffstore (#1518)
1 parent 53fe4d3 commit fc69bd6

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

redis/client.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ class Redis:
595595
lambda r: r and set(r) or set()
596596
),
597597
**string_keys_to_dict(
598-
'ZPOPMAX ZPOPMIN ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE',
599-
zset_score_pairs
598+
'ZPOPMAX ZPOPMIN ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE '
599+
'ZREVRANGEBYSCORE', zset_score_pairs
600600
),
601601
**string_keys_to_dict('BZPOPMIN BZPOPMAX', \
602602
lambda r:
@@ -2932,6 +2932,24 @@ def zcount(self, name, min, max):
29322932
"""
29332933
return self.execute_command('ZCOUNT', name, min, max)
29342934

2935+
def zdiff(self, keys, withscores=False):
2936+
"""
2937+
Returns the difference between the first and all successive input
2938+
sorted sets provided in ``keys``.
2939+
"""
2940+
pieces = [len(keys), *keys]
2941+
if withscores:
2942+
pieces.append("WITHSCORES")
2943+
return self.execute_command("ZDIFF", *pieces)
2944+
2945+
def zdiffstore(self, dest, keys):
2946+
"""
2947+
Computes the difference between the first and all successive input
2948+
sorted sets provided in ``keys`` and stores the result in ``dest``.
2949+
"""
2950+
pieces = [len(keys), *keys]
2951+
return self.execute_command("ZDIFFSTORE", dest, *pieces)
2952+
29352953
def zincrby(self, name, amount, value):
29362954
"Increment the score of ``value`` in sorted set ``name`` by ``amount``"
29372955
return self.execute_command('ZINCRBY', name, amount, value)

tests/test_commands.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,21 @@ def test_zcount(self, r):
14911491
assert r.zcount('a', 1, '(' + str(2)) == 1
14921492
assert r.zcount('a', 10, 20) == 0
14931493

1494+
@skip_if_server_version_lt('6.2.0')
1495+
def test_zdiff(self, r):
1496+
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
1497+
r.zadd('b', {'a1': 1, 'a2': 2})
1498+
assert r.zdiff(['a', 'b']) == [b'a3']
1499+
assert r.zdiff(['a', 'b'], withscores=True) == [b'a3', b'3']
1500+
1501+
@skip_if_server_version_lt('6.2.0')
1502+
def test_zdiffstore(self, r):
1503+
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
1504+
r.zadd('b', {'a1': 1, 'a2': 2})
1505+
assert r.zdiffstore("out", ['a', 'b'])
1506+
assert r.zrange("out", 0, -1) == [b'a3']
1507+
assert r.zrange("out", 0, -1, withscores=True) == [(b'a3', 3.0)]
1508+
14941509
def test_zincrby(self, r):
14951510
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
14961511
assert r.zincrby('a', 1, 'a2') == 3.0

0 commit comments

Comments
 (0)