From 4ba65104eecbff8c4b76a7933679cbb5376770c6 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Thu, 22 Jul 2021 23:27:18 +0300 Subject: [PATCH 1/5] zdiff and zdiffstore --- redis/client.py | 22 +++++++++++++++++++++- tests/test_commands.py | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 57932b9f73..8353f68ac6 100755 --- a/redis/client.py +++ b/redis/client.py @@ -595,7 +595,7 @@ class Redis: lambda r: r and set(r) or set() ), **string_keys_to_dict( - 'ZPOPMAX ZPOPMIN ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE', + 'ZPOPMAX ZPOPMIN ZDIFF ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE', zset_score_pairs ), **string_keys_to_dict('BZPOPMIN BZPOPMAX', \ @@ -2902,6 +2902,26 @@ def zcount(self, name, min, max): """ 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, name, keys, withscores=False): + """ + Computes the difference between the first and all successive input + sorted sets provided in ``keys`` and stores the result in ``name``. + """ + pieces = [len(keys), *keys] + if withscores: + pieces.append("WITHSCORES") + return self.execute_command("ZDIFFSTORE", name, *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 27117e3188..5e5c11b469 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1443,6 +1443,21 @@ def test_zcount(self, r): 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 From f4abcca2cc913ff2a6279ef02e4d1e4660a5acf9 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Thu, 22 Jul 2021 23:28:44 +0300 Subject: [PATCH 2/5] line to long.. --- redis/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index 8353f68ac6..144f12d8f4 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 ZDIFF 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: From 115c345f14e0f9d91b9307059bc1c8351fc32266 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Sun, 25 Jul 2021 16:10:26 +0300 Subject: [PATCH 3/5] remove withscores in zdiffstore --- redis/client.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/redis/client.py b/redis/client.py index 144f12d8f4..aa0b4695c3 100755 --- a/redis/client.py +++ b/redis/client.py @@ -2912,14 +2912,12 @@ def zdiff(self, keys, withscores=False): pieces.append("WITHSCORES") return self.execute_command("ZDIFF", *pieces) - def zdiffstore(self, name, keys, withscores=False): + def zdiffstore(self, name, keys): """ Computes the difference between the first and all successive input sorted sets provided in ``keys`` and stores the result in ``name``. """ pieces = [len(keys), *keys] - if withscores: - pieces.append("WITHSCORES") return self.execute_command("ZDIFFSTORE", name, *pieces) def zincrby(self, name, amount, value): From 52ae486915f422087ced1434a4f1f68da5e156ca Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Sun, 25 Jul 2021 16:13:08 +0300 Subject: [PATCH 4/5] rename "name" to "dest" --- redis/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index aa0b4695c3..7f280e87fe 100755 --- a/redis/client.py +++ b/redis/client.py @@ -2912,13 +2912,13 @@ def zdiff(self, keys, withscores=False): pieces.append("WITHSCORES") return self.execute_command("ZDIFF", *pieces) - def zdiffstore(self, name, keys): + 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 ``name``. """ pieces = [len(keys), *keys] - return self.execute_command("ZDIFFSTORE", name, *pieces) + return self.execute_command("ZDIFFSTORE", dest, *pieces) def zincrby(self, name, amount, value): "Increment the score of ``value`` in sorted set ``name`` by ``amount``" From c49592ba7674841a126ef6beea4a088c95eb3a99 Mon Sep 17 00:00:00 2001 From: AvitalFineRedis Date: Sun, 25 Jul 2021 16:13:08 +0300 Subject: [PATCH 5/5] rename "name" to "dest" --- redis/client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/redis/client.py b/redis/client.py index aa0b4695c3..8fef29bfba 100755 --- a/redis/client.py +++ b/redis/client.py @@ -2912,13 +2912,13 @@ def zdiff(self, keys, withscores=False): pieces.append("WITHSCORES") return self.execute_command("ZDIFF", *pieces) - def zdiffstore(self, name, keys): + 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 ``name``. + sorted sets provided in ``keys`` and stores the result in ``dest``. """ pieces = [len(keys), *keys] - return self.execute_command("ZDIFFSTORE", name, *pieces) + return self.execute_command("ZDIFFSTORE", dest, *pieces) def zincrby(self, name, amount, value): "Increment the score of ``value`` in sorted set ``name`` by ``amount``"