Skip to content

Commit

Permalink
Python: add ZCARD command
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Jan 28, 2024
1 parent 89f8a2c commit 55ef1d1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,24 @@ async def zadd_incr(
await self._execute_command(RequestType.Zadd, args),
)

async def zcard(self, key: str) -> int:
"""
Returns the cardinality (number of elements) of the sorted set stored at `key`.
See https://redis.io/commands/zcard/ for more details.
Args:
key (str): The key of the sorted set.
Returns:
int: The number of elements in the sorted set.
Examples:
>>> await zcard("my_sorted_set")
3 # Indicates that there are 3 elements in the sorted set "my_sorted_set".
"""
return cast(int, await self._execute_command(RequestType.Zcard, [key]))

async def zrem(
self,
key: str,
Expand Down
14 changes: 14 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,20 @@ def zadd_incr(
args += [str(increment), member]
self.append_command(RequestType.Zadd, args)

def zcard(self, key: str):
"""
Returns the cardinality (number of elements) of the sorted set stored at `key`.
See https://redis.io/commands/zcard/ for more details.
Args:
key (str): The key of the sorted set.
Commands response:
int: The number of elements in the sorted set.
"""
self.append_command(RequestType.Zcard, [key])

def zrem(
self,
key: str,
Expand Down
10 changes: 10 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,16 @@ async def test_zrem(self, redis_client: TRedisClient):

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

@pytest.mark.parametrize("cluster_mode", [True, False])
async def test_zcard(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.zcard(key) == 3

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


class TestCommandsUnitTests:
def test_expiry_cmd_args(self):
Expand Down
2 changes: 2 additions & 0 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def transaction_test(
transaction.zadd(key8, {"one": 1, "two": 2, "three": 3})
transaction.zadd_incr(key8, "one", 3)
transaction.zrem(key8, ["one"])
transaction.zcard(key8)
return [
OK,
value,
Expand Down Expand Up @@ -128,6 +129,7 @@ def transaction_test(
3,
4,
1,
2,
]


Expand Down

0 comments on commit 55ef1d1

Please sign in to comment.