Skip to content

Commit 4b22476

Browse files
authored
Add support for SORT_RO (#1858)
* add sort_ro * mark test as onlynon cluster * delete mark test as onlynoncluster * skip test
1 parent 160a7f6 commit 4b22476

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: redis/commands/core.py

+33
Original file line numberDiff line numberDiff line change
@@ -2724,6 +2724,39 @@ def sort(
27242724
options = {"groups": len(get) if groups else None}
27252725
return self.execute_command("SORT", *pieces, **options)
27262726

2727+
def sort_ro(
2728+
self,
2729+
key: str,
2730+
start: Optional[int] = None,
2731+
num: Optional[int] = None,
2732+
by: Optional[str] = None,
2733+
get: Optional[List[str]] = None,
2734+
desc: bool = False,
2735+
alpha: bool = False,
2736+
) -> list:
2737+
"""
2738+
Returns the elements contained in the list, set or sorted set at key.
2739+
(read-only variant of the SORT command)
2740+
2741+
``start`` and ``num`` allow for paging through the sorted data
2742+
2743+
``by`` allows using an external key to weight and sort the items.
2744+
Use an "*" to indicate where in the key the item value is located
2745+
2746+
``get`` allows for returning items from external keys rather than the
2747+
sorted data itself. Use an "*" to indicate where in the key
2748+
the item value is located
2749+
2750+
``desc`` allows for reversing the sort
2751+
2752+
``alpha`` allows for sorting lexicographically rather than numerically
2753+
2754+
For more information check https://redis.io/commands/sort_ro
2755+
"""
2756+
return self.sort(
2757+
key, start=start, num=num, by=by, get=get, desc=desc, alpha=alpha
2758+
)
2759+
27272760

27282761
AsyncListCommands = ListCommands
27292762

Diff for: tests/test_commands.py

+10
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,16 @@ def test_sort_all_options(self, r):
29122912
assert num == 4
29132913
assert r.lrange("sorted", 0, 10) == [b"vodka", b"milk", b"gin", b"apple juice"]
29142914

2915+
@skip_if_server_version_lt("7.0.0")
2916+
def test_sort_ro(self, r):
2917+
r["score:1"] = 8
2918+
r["score:2"] = 3
2919+
r["score:3"] = 5
2920+
r.rpush("a", "3", "2", "1")
2921+
assert r.sort_ro("a", by="score:*") == [b"2", b"3", b"1"]
2922+
r.rpush("b", "2", "3", "1")
2923+
assert r.sort_ro("b", desc=True) == [b"3", b"2", b"1"]
2924+
29152925
def test_sort_issue_924(self, r):
29162926
# Tests for issue https://github.com/andymccurdy/redis-py/issues/924
29172927
r.execute_command("SADD", "issue#924", 1)

0 commit comments

Comments
 (0)