Skip to content

Commit

Permalink
change Infbounfd values
Browse files Browse the repository at this point in the history
  • Loading branch information
shohamazon committed Feb 28, 2024
1 parent da1d895 commit 675b72c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 48 deletions.
35 changes: 18 additions & 17 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,9 +1178,9 @@ async def zadd(
If `changed` is set, returns the number of elements updated in the sorted set.
Examples:
>>> await zadd("my_sorted_set", {"member1": 10.5, "member2": 8.2})
>>> await client.zadd("my_sorted_set", {"member1": 10.5, "member2": 8.2})
2 # Indicates that two elements have been added or updated in the sorted set "my_sorted_set."
>>> await zadd("existing_sorted_set", {"member1": 15.0, "member2": 5.5}, existing_options=ConditionalChange.XX)
>>> await client.zadd("existing_sorted_set", {"member1": 15.0, "member2": 5.5}, existing_options=ConditionalChange.XX)
2 # Updates the scores of two existing members in the sorted set "existing_sorted_set."
"""
args = [key]
Expand Down Expand Up @@ -1241,9 +1241,9 @@ async def zadd_incr(
If there was a conflict with choosing the XX/NX/LT/GT options, the operation aborts and None is returned.
Examples:
>>> await zaddIncr("my_sorted_set", member , 5.0)
>>> await client.zaddIncr("my_sorted_set", member , 5.0)
5.0
>>> await zaddIncr("existing_sorted_set", member , "3.0" , UpdateOptions.LESS_THAN)
>>> await client.zaddIncr("existing_sorted_set", member , "3.0" , UpdateOptions.LESS_THAN)
None
"""
args = [key]
Expand Down Expand Up @@ -1282,9 +1282,9 @@ async def zcard(self, key: str) -> int:
If `key` does not exist, it is treated as an empty sorted set, and the command returns 0.
Examples:
>>> await zcard("my_sorted_set")
>>> await client.zcard("my_sorted_set")
3 # Indicates that there are 3 elements in the sorted set "my_sorted_set".
>>> await zcard("non_existing_key")
>>> await client.zcard("non_existing_key")
0
"""
return cast(int, await self._execute_command(RequestType.Zcard, [key]))
Expand Down Expand Up @@ -1321,14 +1321,14 @@ async def zcount(
1 # Indicates that there is one ScoreBoundary with 5.0 < score <= 10.0 in the sorted set "my_sorted_set".
"""
score_min = (
min_score.value
if not type(min_score) == InfBound
else min_score.value["default_arg"]
min_score.value["score_arg"]
if type(min_score) == InfBound
else min_score.value
)
score_max = (
max_score.value
if not type(max_score) == InfBound
else max_score.value["default_arg"]
max_score.value["score_arg"]
if type(max_score) == InfBound
else max_score.value
)
return cast(
int,
Expand Down Expand Up @@ -1414,6 +1414,8 @@ async def zrange(
See https://redis.io/commands/zrange/ for more details.
To get the elements with their scores, see zrange_withscores.
Args:
key (str): The key of the sorted set.
range_query (Union[RangeByIndex, RangeByLex, RangeByScore]): The range query object representing the type of range query to perform.
Expand All @@ -1430,7 +1432,7 @@ async def zrange(
>>> await client.zrange("my_sorted_set", RangeByIndex(0, -1))
['member1', 'member2', 'member3'] # Returns all members in ascending order.
>>> await client.zrange("my_sorted_set", RangeByScore(start=InfBound.NEG_INF, stop= ScoreBoundary(3), limit= Limit(0 , 2)))
['member2', 'member3']
['member2', 'member3'] # Returns members with scores within the range of negative infinity to 3, limited to 2 elements, in ascending order.
"""
args = _create_zrange_args(key, range_query, reverse, with_scores=False)

Expand All @@ -1444,7 +1446,7 @@ async def zrange_withscores(
) -> Mapping[str, float]:
"""
Returns the specified range of elements with their scores in the sorted set stored at `key`.
Similar to ZRANGE but with a WTHISCORE flag.
Similar to ZRANGE but with a WITHSCORE flag.
See https://redis.io/commands/zrange/ for more details.
Expand All @@ -1456,15 +1458,14 @@ async def zrange_withscores(
reverse (bool): If True, reverses the sorted set, with index 0 as the element with the highest score.
Returns:
Map[str , float]: A map of elements and their scores within the specified range.
Mapping[str , float]: A map of elements and their scores within the specified range.
If `key` does not exist, it is treated as an empty sorted set, and the command returns an empty map.
Examples:
>>> await client.zrange_withscores("my_sorted_set", RangeByScore(ScoreBoundary(10), ScoreBoundary(20)))
{'member1': 10.5, 'member2': 15.2} # Returns members with scores between 10 and 20 with their scores.
>>> await client.zrange("my_sorted_set", RangeByScore(start=InfBound.NEG_INF, stop= ScoreBoundary(3), limit= Limit(0 , 2)))
{'member4': -2.0, 'member7': 1.5}
{'member4': -2.0, 'member7': 1.5} # Returns members with with scores within the range of negative infinity to 3, limited to 2 elements, with their scores.
"""
args = _create_zrange_args(key, range_query, reverse, with_scores=True)

Expand Down
22 changes: 9 additions & 13 deletions python/python/glide/async_commands/sorted_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

class InfBound(Enum):
"""
Enumeration representing numeric and lexicographic positive and negative infinity bounds for sorted set scores.
Enumeration representing numeric and lexicographic positive and negative infinity bounds for sorted set.
"""

POS_INF = {"default_arg": "+inf", "lex_arg": "+"}
NEG_INF = {"default_arg": "-inf", "lex_arg": "-"}
POS_INF = {"score_arg": "+inf", "lex_arg": "+"}
NEG_INF = {"score_arg": "-inf", "lex_arg": "-"}


class ScoreBoundary:
Expand Down Expand Up @@ -83,7 +83,7 @@ class RangeByScore:
Args:
start (Union[InfBound, ScoreBoundary]): The start score boundary.
stop (Union[InfBound, ScoreBoundary]): The stop score boundary.
limit (Optional[Limit]): The limit argument for a range query. Defaults to None.
limit (Optional[Limit]): The limit argument for a range query. Defaults to None. See `Limit`.
"""

def __init__(
Expand All @@ -93,11 +93,9 @@ def __init__(
limit: Optional[Limit] = None,
):
self.start = (
start.value if not type(start) == InfBound else start.value["default_arg"]
)
self.stop = (
stop.value if not type(stop) == InfBound else stop.value["default_arg"]
start.value["score_arg"] if type(start) == InfBound else start.value
)
self.stop = stop.value["score_arg"] if type(stop) == InfBound else stop.value
self.limit = limit


Expand All @@ -110,7 +108,7 @@ class RangeByLex:
Args:
start (Union[InfBound, LexBoundary]): The start lexicographic boundary.
stop (Union[InfBound, LexBoundary]): The stop lexicographic boundary.
limit (Optional[Limit]): The limit argument for a range query. Defaults to None.
limit (Optional[Limit]): The limit argument for a range query. Defaults to None. See `Limit`.
"""

def __init__(
Expand All @@ -119,10 +117,8 @@ def __init__(
stop: Union[InfBound, LexBoundary],
limit: Optional[Limit] = None,
):
self.start = (
start.value if not type(start) == InfBound else start.value["lex_arg"]
)
self.stop = stop.value if not type(stop) == InfBound else stop.value["lex_arg"]
self.start = start.value["lex_arg"] if type(start) == InfBound else start.value
self.stop = stop.value["lex_arg"] if type(stop) == InfBound else stop.value
self.limit = limit


Expand Down
24 changes: 12 additions & 12 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,27 +1056,27 @@ def zcount(
Args:
key (str): The key of the sorted set.
min_score (Union[InfBound, ScoreLimit]): The minimum score to count from.
min_score (Union[InfBound, ScoreBoundary]): The minimum score to count from.
Can be an instance of InfBound representing positive/negative infinity,
or ScoreLimit representing a specific score and inclusivity.
max_score (Union[InfBound, ScoreLimit]): The maximum score to count up to.
or ScoreBoundary representing a specific score and inclusivity.
max_score (Union[InfBound, ScoreBoundary]): The maximum score to count up to.
Can be an instance of InfBound representing positive/negative infinity,
or ScoreLimit representing a specific score and inclusivity.
or ScoreBoundary representing a specific score and inclusivity.
Commands response:
int: The number of members in the specified score range.
If key does not exist, 0 is returned.
If `max_score` < `min_score`, 0 is returned.
"""
score_min = (
min_score.value
if not type(min_score) == InfBound
else min_score.value["default_arg"]
min_score.value["score_arg"]
if type(min_score) == InfBound
else min_score.value
)
score_max = (
max_score.value
if not type(max_score) == InfBound
else max_score.value["default_arg"]
max_score.value["score_arg"]
if type(max_score) == InfBound
else max_score.value
)
return self.append_command(RequestType.Zcount, [key, score_min, score_max])

Expand Down Expand Up @@ -1163,7 +1163,7 @@ def zrange_withscores(
) -> TTransaction:
"""
Returns the specified range of elements with their scores in the sorted set stored at `key`.
Similar to ZRANGE but with a WTHISCORE flag.
Similar to ZRANGE but with a WITHSCORE flag.
See https://redis.io/commands/zrange/ for more details.
Expand All @@ -1175,7 +1175,7 @@ def zrange_withscores(
reverse (bool): If True, reverses the sorted set, with index 0 as the element with the highest score.
Commands response:
Map[str , float]: A map of elements and their scores within the specified range.
Mapping[str , float]: A map of elements and their scores within the specified range.
If `key` does not exist, it is treated as an empty sorted set, and the command returns an empty map.
"""
args = _create_zrange_args(key, range_query, reverse, with_scores=True)
Expand Down
16 changes: 12 additions & 4 deletions python/python/tests/test_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,22 +1168,30 @@ async def test_zcount(self, redis_client: TRedisClient):
assert await redis_client.zcount(key, InfBound.NEG_INF, InfBound.POS_INF) == 3
assert (
await redis_client.zcount(
key, ScoreBoundary(1, False), ScoreBoundary(3, False)
key,
ScoreBoundary(1, is_inclusive=False),
ScoreBoundary(3, is_inclusive=False),
)
== 1
)
assert (
await redis_client.zcount(
key, ScoreBoundary(1, False), ScoreBoundary(3, True)
key,
ScoreBoundary(1, is_inclusive=False),
ScoreBoundary(3, is_inclusive=True),
)
== 2
)
assert (
await redis_client.zcount(key, InfBound.NEG_INF, ScoreBoundary(3, True))
await redis_client.zcount(
key, InfBound.NEG_INF, ScoreBoundary(3, is_inclusive=True)
)
== 3
)
assert (
await redis_client.zcount(key, InfBound.POS_INF, ScoreBoundary(3, True))
await redis_client.zcount(
key, InfBound.POS_INF, ScoreBoundary(3, is_inclusive=True)
)
== 0
)
assert (
Expand Down
4 changes: 2 additions & 2 deletions python/python/tests/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest
from glide import RequestError
from glide.async_commands.core import InfBound, RangeByIndex, ScoreBoundary
from glide.async_commands.sorted_set import InfBound, RangeByIndex, ScoreBoundary
from glide.async_commands.transaction import (
BaseTransaction,
ClusterTransaction,
Expand Down Expand Up @@ -143,7 +143,7 @@ def transaction_test(
args.append(1)
transaction.zcard(key8)
args.append(2)
transaction.zcount(key8, ScoreBoundary(2, True), InfBound.POS_INF)
transaction.zcount(key8, ScoreBoundary(2, is_inclusive=True), InfBound.POS_INF)
args.append(2)
transaction.zscore(key8, "two")
args.append(2.0)
Expand Down

0 comments on commit 675b72c

Please sign in to comment.