From 96612605ea52d75ee5017eb2b32a0ff98d5bdb1c Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 5 Jan 2022 03:29:19 +0200 Subject: [PATCH 1/3] add zintercard --- redis/commands/core.py | 14 ++++++++++++++ tests/test_commands.py | 9 +++++++++ 2 files changed, 23 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 4f0accd957..80e94224bf 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2,6 +2,7 @@ import hashlib import time import warnings +from typing import List from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError @@ -3162,6 +3163,19 @@ def zinterstore(self, dest, keys, aggregate=None): """ return self._zaggregate("ZINTERSTORE", dest, keys, aggregate) + def zintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int: + """ + Return the cardinality of the intersect of multiple sorted sets + specified by ``keys`. + When LIMIT provided (defaults to 0 and means unlimited), if the intersection + cardinality reaches limit partway through the computation, the algorithm will + exit and yield limit as the cardinality + + For more information check https://redis.io/commands/zintercard + """ + args = [numkeys] + keys + ["LIMIT", limit] + return self.execute_command("ZINTERCARD", *args) + def zlexcount(self, name, min, max): """ Return the number of items in the sorted set ``name`` between the diff --git a/tests/test_commands.py b/tests/test_commands.py index b28b63ea6e..58bd69611c 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1972,6 +1972,15 @@ def test_zinter(self, r): (b"a1", 23), ] + @pytest.mark.onlynoncluster + # @skip_if_server_version_lt("7.0.0") turn on after redis 7 release + def test_zintercard(self, unstable_r): + unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 1}) + unstable_r.zadd("b", {"a1": 2, "a2": 2, "a3": 2}) + unstable_r.zadd("c", {"a1": 6, "a3": 5, "a4": 4}) + assert unstable_r.zintercard(3, ["a", "b", "c"]) == 2 + assert unstable_r.zintercard(3, ["a", "b", "c"], limit=1) == 1 + @pytest.mark.onlynoncluster def test_zinterstore_sum(self, r): r.zadd("a", {"a1": 1, "a2": 1, "a3": 1}) From 5c9e6bc5bd318da3517d8f46b0c37896abf4ceb8 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 12 Jan 2022 14:54:08 +0200 Subject: [PATCH 2/3] fix pr comment --- redis/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index 80e94224bf..d4ecfb8ea3 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3173,7 +3173,7 @@ def zintercard(self, numkeys: int, keys: List[str], limit: int = 0) -> int: For more information check https://redis.io/commands/zintercard """ - args = [numkeys] + keys + ["LIMIT", limit] + args = [numkeys, *keys, "LIMIT", limit] return self.execute_command("ZINTERCARD", *args) def zlexcount(self, name, min, max): From 685288f749b0c8f631d1ccc772c82d1d452fe49b Mon Sep 17 00:00:00 2001 From: dvora-h <67596500+dvora-h@users.noreply.github.com> Date: Wed, 2 Feb 2022 13:04:28 +0200 Subject: [PATCH 3/3] linters --- redis/commands/core.py | 1 - 1 file changed, 1 deletion(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index 734b8ad2be..a4a48f95f1 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -2,7 +2,6 @@ import hashlib import time import warnings - from typing import List, Optional from redis.exceptions import ConnectionError, DataError, NoScriptError, RedisError