Skip to content

Commit

Permalink
Python: adds ZREM command
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Jan 18, 2024
1 parent 9d90474 commit 445076f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,34 @@ async def zadd_incr(
Optional[float],
await self._execute_command(RequestType.Zadd, args),
)

async def zrem(
self,
key: str,
members: List[str],
) -> int:
"""
Removes the specified members from the sorted set stored at `key`.
Specified members that are not a member of this set are ignored.
See https://redis.io/commands/zrem/ for more details.
Args:
key (str): The key of the sorted set.
members (List[str]): A list of members to remove from the sorted set.
Returns:
int: The number of members that were removed from the sorted set, not including non-existing members.
If `key` does not exist, it is treated as an empty sorted set, and this command returns 0.
If `key` holds a value that is not a sorted set, an error is returned.
Examples:
>>> await zrem("my_sorted_set", ["member1", "member2"])
2 # Indicates that two members have been removed from the sorted set "my_sorted_set."
>>> await zrem("non_existing_sorted_set", ["member1", "member2"])
0 # Indicates that no members were removed as the sorted set "non_existing_sorted_set" does not exist.
"""
return cast(
int,
await self._execute_command(RequestType.Zrem, [key] + members),
)
11 changes: 11 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,17 @@ async def test_zadd_gt_lt(self, redis_client: TRedisClient):
== None
)

@pytest.mark.parametrize("cluster_mode", [True, False])
async def test_zrem(self, redis_client: TRedisClient):
key = get_random_string(10)
members_scores = {"one": 1, "two": 2, "three": 3}
assert await redis_client.zadd(key, members_scores=members_scores) == 3

assert await redis_client.zrem(key, ["one"]) == 1
assert await redis_client.zrem(key, ["one", "two", "three"]) == 2

assert await redis_client.zrem("non_existing_set", ["member"]) == 0


class TestCommandsUnitTests:
def test_expiry_cmd_args(self):
Expand Down

0 comments on commit 445076f

Please sign in to comment.