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

Add support for SORT_RO #1858

Merged
merged 5 commits into from
Mar 14, 2022
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
33 changes: 33 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,39 @@ def sort(
options = {"groups": len(get) if groups else None}
return self.execute_command("SORT", *pieces, **options)

def sort_ro(
self,
key: str,
start: Optional[int] = None,
num: Optional[int] = None,
by: Optional[str] = None,
get: Optional[List[str]] = None,
desc: bool = False,
alpha: bool = False,
) -> list:
"""
Returns the elements contained in the list, set or sorted set at key.
(read-only variant of the SORT command)

``start`` and ``num`` allow for paging through the sorted data
Copy link
Contributor

Choose a reason for hiding this comment

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

Where do start and num come from? I dont't see them on the docs page. I see a LIMIT in which case we can align with the command.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did it this way to stay consistent with 'sort', I can change both but I don't think it's a good idea

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. I wish sort didn't look this way now. Fine - better to have two behave the same.


``by`` allows using an external key to weight and sort the items.
Use an "*" to indicate where in the key the item value is located

``get`` allows for returning items from external keys rather than the
sorted data itself. Use an "*" to indicate where in the key
the item value is located

``desc`` allows for reversing the sort

``alpha`` allows for sorting lexicographically rather than numerically

For more information check https://redis.io/commands/sort_ro
"""
return self.sort(
key, start=start, num=num, by=by, get=get, desc=desc, alpha=alpha
)


class ScanCommands:
"""
Expand Down
10 changes: 10 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,16 @@ def test_sort_all_options(self, r):
assert num == 4
assert r.lrange("sorted", 0, 10) == [b"vodka", b"milk", b"gin", b"apple juice"]

@skip_if_server_version_lt("7.0.0")
def test_sort_ro(self, r):
r["score:1"] = 8
r["score:2"] = 3
r["score:3"] = 5
r.rpush("a", "3", "2", "1")
assert r.sort_ro("a", by="score:*") == [b"2", b"3", b"1"]
r.rpush("b", "2", "3", "1")
assert r.sort_ro("b", desc=True) == [b"3", b"2", b"1"]

def test_sort_issue_924(self, r):
# Tests for issue https://github.com/andymccurdy/redis-py/issues/924
r.execute_command("SADD", "issue#924", 1)
Expand Down