Skip to content

Commit a87a750

Browse files
authored
Add support for ZMPOP (#1923)
1 parent 9442d34 commit a87a750

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

redis/commands/core.py

+25
Original file line numberDiff line numberDiff line change
@@ -3325,6 +3325,31 @@ def bzpopmin(self, keys, timeout=0):
33253325
keys.append(timeout)
33263326
return self.execute_command("BZPOPMIN", *keys)
33273327

3328+
def zmpop(
3329+
self,
3330+
num_keys: int,
3331+
keys: List[str],
3332+
min: Optional[bool] = False,
3333+
max: Optional[bool] = False,
3334+
count: Optional[int] = 1,
3335+
) -> list:
3336+
"""
3337+
Pop ``count`` values (default 1) off of the first non-empty sorted set
3338+
named in the ``keys`` list.
3339+
For more information check https://redis.io/commands/zmpop
3340+
"""
3341+
args = [num_keys] + keys
3342+
if (min and max) or (not min and not max):
3343+
raise DataError
3344+
elif min:
3345+
args.append("MIN")
3346+
else:
3347+
args.append("MAX")
3348+
if count != 1:
3349+
args.extend(["COUNT", count])
3350+
3351+
return self.execute_command("ZMPOP", *args)
3352+
33283353
def bzmpop(
33293354
self,
33303355
timeout: float,

tests/test_commands.py

+11
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,17 @@ def test_bzpopmin(self, r):
21062106
r.zadd("c", {"c1": 100})
21072107
assert r.bzpopmin("c", timeout=1) == (b"c", b"c1", 100)
21082108

2109+
@pytest.mark.onlynoncluster
2110+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
2111+
def test_zmpop(self, unstable_r):
2112+
unstable_r.zadd("a", {"a1": 1, "a2": 2, "a3": 3})
2113+
res = [b"a", [[b"a1", b"1"], [b"a2", b"2"]]]
2114+
assert unstable_r.zmpop("2", ["b", "a"], min=True, count=2) == res
2115+
with pytest.raises(redis.DataError):
2116+
unstable_r.zmpop("2", ["b", "a"], count=2)
2117+
unstable_r.zadd("b", {"b1": 10, "ab": 9, "b3": 8})
2118+
assert unstable_r.zmpop("2", ["b", "a"], max=True) == [b"b", [[b"b1", b"10"]]]
2119+
21092120
@pytest.mark.onlynoncluster
21102121
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
21112122
def test_bzmpop(self, unstable_r):

0 commit comments

Comments
 (0)