summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvital Fine <79420960+AvitalFineRedis@users.noreply.github.com>2021-07-29 16:41:55 +0300
committerGitHub <noreply@github.com>2021-07-29 16:41:55 +0300
commitfc69bd65494d7ac8d08e16ff38e06936ab6dcc02 (patch)
treeae6f2d886ff0f1f2b63118abd0a9267cb2301ba4
parent53fe4d3eae75cabfb635eae5ae9197efa0bf86c3 (diff)
downloadredis-py-fc69bd65494d7ac8d08e16ff38e06936ab6dcc02.tar.gz
zdiff and zdiffstore (#1518)
-rwxr-xr-xredis/client.py22
-rw-r--r--tests/test_commands.py15
2 files changed, 35 insertions, 2 deletions
diff --git a/redis/client.py b/redis/client.py
index 33b0374..4e4ccc6 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -595,8 +595,8 @@ class Redis:
lambda r: r and set(r) or set()
),
**string_keys_to_dict(
- 'ZPOPMAX ZPOPMIN ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE',
- zset_score_pairs
+ 'ZPOPMAX ZPOPMIN ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE '
+ 'ZREVRANGEBYSCORE', zset_score_pairs
),
**string_keys_to_dict('BZPOPMIN BZPOPMAX', \
lambda r:
@@ -2932,6 +2932,24 @@ class Redis:
"""
return self.execute_command('ZCOUNT', name, min, max)
+ def zdiff(self, keys, withscores=False):
+ """
+ Returns the difference between the first and all successive input
+ sorted sets provided in ``keys``.
+ """
+ pieces = [len(keys), *keys]
+ if withscores:
+ pieces.append("WITHSCORES")
+ return self.execute_command("ZDIFF", *pieces)
+
+ def zdiffstore(self, dest, keys):
+ """
+ Computes the difference between the first and all successive input
+ sorted sets provided in ``keys`` and stores the result in ``dest``.
+ """
+ pieces = [len(keys), *keys]
+ return self.execute_command("ZDIFFSTORE", dest, *pieces)
+
def zincrby(self, name, amount, value):
"Increment the score of ``value`` in sorted set ``name`` by ``amount``"
return self.execute_command('ZINCRBY', name, amount, value)
diff --git a/tests/test_commands.py b/tests/test_commands.py
index 8411e5e..9682603 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -1491,6 +1491,21 @@ class TestRedisCommands:
assert r.zcount('a', 1, '(' + str(2)) == 1
assert r.zcount('a', 10, 20) == 0
+ @skip_if_server_version_lt('6.2.0')
+ def test_zdiff(self, r):
+ r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
+ r.zadd('b', {'a1': 1, 'a2': 2})
+ assert r.zdiff(['a', 'b']) == [b'a3']
+ assert r.zdiff(['a', 'b'], withscores=True) == [b'a3', b'3']
+
+ @skip_if_server_version_lt('6.2.0')
+ def test_zdiffstore(self, r):
+ r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
+ r.zadd('b', {'a1': 1, 'a2': 2})
+ assert r.zdiffstore("out", ['a', 'b'])
+ assert r.zrange("out", 0, -1) == [b'a3']
+ assert r.zrange("out", 0, -1, withscores=True) == [(b'a3', 3.0)]
+
def test_zincrby(self, r):
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3})
assert r.zincrby('a', 1, 'a2') == 3.0