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 ZCOUNT command #878

Merged
merged 1 commit into from
Feb 5, 2024
Merged

Conversation

shohamazon
Copy link
Member

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@shohamazon shohamazon requested a review from a team as a code owner January 30, 2024 13:48
int: The number of members in the specified score range.

Examples:
>>> await zcount("my_sorted_set", BoundPair(5.0 , true) , InfBound.POS_INF)
Copy link
Contributor

@barshaul barshaul Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the arg names to the BoundPair example to make it more readable, e.g.:

ScoreLimit(5.0 , is_inclusive=true)

>>> await zcount("my_sorted_set", BoundPair(5.0 , true) , InfBound.POS_INF)
2 # Indicates that there are 2 members with scores between 5.0 (not exclusive) and +inf in the sorted set "my_sorted_set".
>>> await zcount("my_sorted_set", BoundPair(5.0 , true) , BoundPair(1.0 , false))
1 # Indicates that there is one member with 5.0 < score <= 10.0 in the sorted set "my_sorted_set".
Copy link
Contributor

@barshaul barshaul Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the example you wrote 1.0, in the comment 10.0.
I guess that it's a typo, but what happens if max_score < min_score?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if key isn't exists? an error returned, or a null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If key does not exist, it is treated as an empty sorted set, and the command returns 0.
If max_score < min_score, 0 is returned.
Added this to the doc.

NEG_INF = "-inf"


class BoundPair:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BoundPair -> maybe ScoreLimit


Args:
value (float): The score value.
is_inclusive (bool): Whether the score is inclusive. Defaults to False.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether the score is inclusive. => Whether the score value is inclusive.


class BoundPair:
"""
Represents a pair of score and inclusivity in a sorted set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Represents a score limit in a sorted set.

await redis_client.zcount(key, BoundPair(1, False), BoundPair(3, True)) == 2
)
assert await redis_client.zcount(key, InfBound.NEG_INF, BoundPair(3, True)) == 3
assert await redis_client.zcount(key, InfBound.POS_INF, BoundPair(3, True)) == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add tests for non existing key and max < min

Copy link
Member Author

@shohamazon shohamazon Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test exists (max < min), see:
assert await redis_client.zcount(key, InfBound.POS_INF, BoundPair(3, True)) == 0

@shohamazon shohamazon force-pushed the python/zcount branch 3 times, most recently from 8615842 to b3ac229 Compare February 1, 2024 14:36
@shohamazon shohamazon merged commit 63c62f1 into aws:main Feb 5, 2024
5 checks passed
@shohamazon shohamazon deleted the python/zcount branch February 5, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants