Skip to content

Commit

Permalink
Addressed comments from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
GilboaAWS committed May 27, 2024
1 parent ba5625f commit 8fb41f5
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 69 deletions.
3 changes: 1 addition & 2 deletions python/python/glide/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0

from glide.async_commands.command_args import Limit, SortOrder
from glide.async_commands.core import (
ConditionalChange,
ExpireOptions,
Expand All @@ -9,7 +10,6 @@
GeoUnit,
InfoSection,
InsertPosition,
SortOrder,
StreamAddOptions,
StreamTrimOptions,
TrimByMaxLen,
Expand All @@ -21,7 +21,6 @@
AggregationType,
InfBound,
LexBoundary,
Limit,
RangeByIndex,
RangeByLex,
RangeByScore,
Expand Down
31 changes: 22 additions & 9 deletions python/python/glide/async_commands/cluster_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from typing import Dict, List, Mapping, Optional, Tuple, cast

from glide.async_commands.command_args import Limit, SortOrder
from glide.async_commands.core import (
CoreCommands,
InfoSection,
SortOrder,
_build_sort_args,
)
from glide.async_commands.transaction import BaseTransaction, ClusterTransaction
Expand Down Expand Up @@ -349,34 +349,47 @@ async def time(self, route: Optional[Route] = None) -> TClusterResponse[List[str
async def sort(
self,
key: str,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
) -> List[Optional[str]]:
) -> List[str]:
"""
Sorts the elements in the list, set, or sorted set at `key` and returns the result.
To store the result into a new key, see `sort_store`.
By default, sorting is numeric, and elements are compared by their value interpreted as double precision floating point numbers.
See https://valkey-io.github.io/commands/sort/ for more details.
Args:
key (str): The key of the list, set, or sorted set to be sorted.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
limit (Optional[Limit]): The limit argument for a range query. Defaults to None. See `Limit` class for more information.
A tuple specifying the offset and count for limiting the number of results returned.
The `limit` parameter takes a tuple `(offset, count)` where `offset` specifies the starting position and `count` specifies the maximum number of elements to return.
If `offset` exceeds the length of the returned result, an empty list is returned.
order (Optional[SortOrder]): Specifies the order to sort the elements.
Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): When `True`, sorts elements lexicographically. When `False` (default), sorts elements numerically.
Use this when the list, set, or sorted set contains string values that cannot be converted into double precision floating point numbers.
Returns:
List[Optional[str]]: Returns a list of sorted elements.
List[str]: A list of sorted elements.
Examples:
>>> await client.lpush("mylist", 3, 1, 2)
>>> await client.lpush("mylist", '3', '1', '2')
>>> await client.sort("mylist")
['1', '2', '3']
>>> await client.sort("mylist", order=SortOrder.DESC)
['3', '2', '1']
>>> await client.lpush("mylist", 2, 1, 2, 3, 3, 1)
>>> await client.lpush("mylist", '2', '1', '2', '3', '3', '1')
>>> await client.sort("mylist", limit=(2, 3))
['2', '2', '3']
>>> await client.lpush("mylist", "a", "b", "c", "d")
>>> await client.sort("mylist", limit=(3, 2), order=SortOrder.DESC, alpha=True)
['b', 'a']
"""
args = _build_sort_args(key, None, limit, None, order, alpha)
result = await self._execute_command(RequestType.Sort, args)
Expand Down
47 changes: 47 additions & 0 deletions python/python/glide/async_commands/command_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0

from enum import Enum
from typing import List, Optional, Tuple, Union


class Limit:
"""
Represents a limit argument for range queries in various Redis commands.
The `LIMIT` argument is commonly used to specify a subset of results from the matching elements,
similar to the `LIMIT` clause in SQL (e.g., `SELECT LIMIT offset, count`).
This class can be utilized in multiple Redis commands that support limit options,
such as [ZRANGE](https://valkey.io/commands/zrange), [SORT](https://valkey.io/commands/sort/), and others.
Args:
offset (int): The starting position of the range.
count (int): The maximum number of elements to include in the range.
A negative count returns all elements from the offset.
Examples:
>>> limit = Limit(0, 10) # Fetch the first 10 elements
>>> limit = Limit(5, -1) # Fetch all elements starting from the 5th element
"""

def __init__(self, offset: int, count: int):
self.offset = offset
self.count = count

self.count = count


class SortOrder(Enum):
"""
SORT order options: options for sorting elements.
"""

ASC = "ASC"
"""
ASC: Sort in ascending order.
"""

DESC = "DESC"
"""
DESC: Sort in descending order.
"""
21 changes: 3 additions & 18 deletions python/python/glide/async_commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
get_args,
)

from glide.async_commands.command_args import Limit, SortOrder
from glide.async_commands.sorted_set import (
AggregationType,
InfBound,
Expand Down Expand Up @@ -136,22 +137,6 @@ class UpdateOptions(Enum):
GREATER_THAN = "GT"


class SortOrder(Enum):
"""
SORT order options: options for sorting elements.
"""

ASC = "ASC"
"""
ASC: Sort in ascending order.
"""

DESC = "DESC"
"""
DESC: Sort in descending order.
"""


class GeospatialData:
def __init__(self, longitude: float, latitude: float):
"""
Expand Down Expand Up @@ -381,7 +366,7 @@ class InsertPosition(Enum):
def _build_sort_args(
key: str,
by_pattern: Optional[str] = None,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
Expand All @@ -393,7 +378,7 @@ def _build_sort_args(
args.extend(["BY", by_pattern])

if limit:
args.extend(["LIMIT", str(limit[0]), str(limit[1])])
args.extend(["LIMIT", str(limit.offset), str(limit.count)])

if get_patterns:
for pattern in get_patterns:
Expand Down
18 changes: 1 addition & 17 deletions python/python/glide/async_commands/sorted_set.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright GLIDE-for-Redis Project Contributors - SPDX Identifier: Apache-2.0

from enum import Enum
from glide.async_commands.command_args import Limit
from typing import List, Optional, Tuple, Union


Expand Down Expand Up @@ -88,23 +89,6 @@ def __init__(self, value: str, is_inclusive: bool = True):
self.value = f"[{value}" if is_inclusive else f"({value}"


class Limit:
"""
Represents a limit argument for a range query in a sorted set to be used in [ZRANGE](https://redis.io/commands/zrange) command.
The optional LIMIT argument can be used to obtain a sub-range from the matching elements
(similar to SELECT LIMIT offset, count in SQL).
Args:
offset (int): The offset from the start of the range.
count (int): The number of elements to include in the range.
A negative count returns all elements from the offset.
"""

def __init__(self, offset: int, count: int):
self.offset = offset
self.count = count


class RangeByIndex:
"""
Represents a range by index (rank) in a sorted set.
Expand Down
10 changes: 5 additions & 5 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

from typing import Dict, List, Mapping, Optional, Tuple, cast

from glide.async_commands.command_args import Limit, SortOrder
from glide.async_commands.core import (
CoreCommands,
InfoSection,
SortOrder,
_build_sort_args,
)
from glide.async_commands.transaction import BaseTransaction, Transaction
Expand Down Expand Up @@ -256,7 +256,7 @@ async def sort(
self,
key: str,
by_pattern: Optional[str] = None,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
Expand All @@ -271,7 +271,7 @@ async def sort(
Args:
key (str): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[str]): A pattern to sort by. If not provided, elements are sorted by their value.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
limit (Optional[Limit]): A tuple specifying the offset and count for limiting the number of results.
get_patterns (Optional[List[str]]): One or more patterns to extract values to return.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
Expand Down Expand Up @@ -303,7 +303,7 @@ async def sort_store(
key: str,
store: str,
by_pattern: Optional[str] = None,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
Expand All @@ -319,7 +319,7 @@ async def sort_store(
key (str): The key of the list, set, or sorted set to be sorted.
store (str): The key where the sorted result will be stored.
by_pattern (Optional[str]): A pattern to sort by. If not provided, elements are sorted by their value.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
limit (Optional[Limit]): A tuple specifying the offset and count for limiting the number of results.
get_patterns (Optional[List[str]]): One or more patterns to extract values to return.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
Expand Down
18 changes: 9 additions & 9 deletions python/python/glide/async_commands/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import threading
from typing import List, Mapping, Optional, Tuple, TypeVar, Union

from glide.async_commands.command_args import Limit, SortOrder
from glide.async_commands.core import (
ConditionalChange,
ExpireOptions,
Expand All @@ -11,7 +12,6 @@
GeoUnit,
InfoSection,
InsertPosition,
SortOrder,
StreamAddOptions,
StreamTrimOptions,
UpdateOptions,
Expand Down Expand Up @@ -2500,7 +2500,7 @@ def sort(
self: TTransaction,
key: str,
by_pattern: Optional[str] = None,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
Expand All @@ -2515,7 +2515,7 @@ def sort(
Args:
key (str): The key of the list, set, or sorted set to be sorted.
by_pattern (Optional[str]): A pattern to sort by. If not provided, elements are sorted by their value.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
limit (Optional[Limit]): A tuple specifying the offset and count for limiting the number of results.
get_patterns (Optional[List[str]]): One or more patterns to extract values to return.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
Expand All @@ -2531,7 +2531,7 @@ def sort_store(
key: str,
store: str,
by_pattern: Optional[str] = None,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
get_patterns: Optional[List[str]] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
Expand All @@ -2547,7 +2547,7 @@ def sort_store(
key (str): The key of the list, set, or sorted set to be sorted.
store (str): The key where the sorted result will be stored.
by_pattern (Optional[str]): A pattern to sort by. If not provided, elements are sorted by their value.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
limit (Optional[Limit]): A tuple specifying the offset and count for limiting the number of results.
get_patterns (Optional[List[str]]): One or more patterns to extract values to return.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
Expand All @@ -2573,7 +2573,7 @@ class ClusterTransaction(BaseTransaction):
def sort(
self: TTransaction,
key: str,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
) -> TTransaction:
Expand All @@ -2585,7 +2585,7 @@ def sort(
Args:
key (str): The key of the list, set, or sorted set to be sorted.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
limit (Optional[Limit]): A tuple specifying the offset and count for limiting the number of results.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
Expand All @@ -2599,7 +2599,7 @@ def sort_store(
self: TTransaction,
key: str,
store: str,
limit: Optional[Tuple[int, int]] = None,
limit: Optional[Limit] = None,
order: Optional[SortOrder] = None,
alpha: Optional[bool] = None,
) -> TTransaction:
Expand All @@ -2613,7 +2613,7 @@ def sort_store(
Args:
key (str): The key of the list, set, or sorted set to be sorted.
store (str): The key where the sorted result will be stored.
limit (Optional[Tuple[int, int]]): A tuple specifying the offset and count for limiting the number of results.
limit (Optional[Limit]): A tuple specifying the offset and count for limiting the number of results.
order (Optional[SortOrder]): Specifies the order to sort the elements. Can be `SortOrder.ASC` (ascending) or `SortOrder.DESC` (descending).
alpha (Optional[bool]): Whether to sort elements lexicographically. If `False`, elements are sorted numerically.
Expand Down
Loading

0 comments on commit 8fb41f5

Please sign in to comment.