Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: adds ZSCORE command #877

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions glide-core/src/client/value_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub(crate) fn expected_type_for_cmd(cmd: &Cmd) -> Option<ExpectedReturnType> {
Some(ExpectedReturnType::Boolean)
}
b"SMEMBERS" => Some(ExpectedReturnType::Set),
b"ZSCORE" => Some(ExpectedReturnType::DoubleOrNull),
_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/protobuf/redis_request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ enum RequestType {
Zcard = 64;
Zcount = 65;
ZIncrBy = 66;
ZScore = 67;
}

message Command {
Expand Down
1 change: 1 addition & 0 deletions glide-core/src/socket_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ fn get_command(request: &Command) -> Option<Cmd> {
RequestType::Zcard => Some(cmd("ZCARD")),
RequestType::Zcount => Some(cmd("ZCOUNT")),
RequestType::ZIncrBy => Some(cmd("ZINCRBY")),
RequestType::ZScore => Some(cmd("ZSCORE")),
}
}

Expand Down
28 changes: 28 additions & 0 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,34 @@ async def zrem(
await self._execute_command(RequestType.Zrem, [key] + members),
)

async def zscore(self, key: str, member: str) -> Optional[float]:
"""
Returns the score of `member` in the sorted set stored at `key`.

See https://redis.io/commands/zscore/ for more details.

Args:
key (str): The key of the sorted set.
member (str): The member whose score is to be retrieved.

Returns:
Optional[float]: The score of the member.
If `member` does not exist in the sorted set, None is returned.
If `key` does not exist, None is returned.
If `key` holds a value that is not a sorted set, an error is returned.


Examples:
>>> await zscore("my_sorted_set", "member")
10.5 # Indicates that the score of "member" in the sorted set "my_sorted_set" is 10.5.
>>> await zscore("my_sorted_set", "non_existing_member")
None
"""
return cast(
Optional[float],
await self._execute_command(RequestType.ZScore, [key, member]),
)

async def invoke_script(
self,
script: Script,
Expand Down
18 changes: 18 additions & 0 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,24 @@ def zrem(
"""
self.append_command(RequestType.Zrem, [key] + members)

def zscore(self, key: str, member: str):
"""
Returns the score of `member` in the sorted set stored at `key`.

See https://redis.io/commands/zscore/ for more details.

Args:
key (str): The key of the sorted set.
member (str): The member whose score is to be retrieved.

Commands response:
Optional[float]: The score of the member.
If `member` does not exist in the sorted set, None is returned.
If `key` does not exist, None is returned.
If `key` holds a value that is not a sorted set, the transaction fails with an error
"""
self.append_command(RequestType.ZScore, [key, member])


class Transaction(BaseTransaction):
"""
Expand Down
12 changes: 12 additions & 0 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,18 @@ async def test_zcard(self, redis_client: TRedisClient):
assert await redis_client.zrem(key, ["one"]) == 1
assert await redis_client.zcard(key) == 2

@pytest.mark.parametrize("cluster_mode", [True, False])
async def test_zscore(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.zscore(key, "one") == 1.0

assert await redis_client.zscore(key, "non_existing_member") == None
shohamazon marked this conversation as resolved.
Show resolved Hide resolved
assert (
await redis_client.zscore("non_existing_key", "non_existing_member") == None
)


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 @@ -90,6 +90,7 @@ def transaction_test(
transaction.zadd_incr(key8, "one", 3)
transaction.zrem(key8, ["one"])
transaction.zcard(key8)
transaction.zscore(key8, "two")
return [
OK,
value,
Expand Down Expand Up @@ -132,6 +133,7 @@ def transaction_test(
4,
1,
2,
2.0,
]


Expand Down
Loading