Skip to content

zrange, zrangestore and zrevrange options #1617

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion docker-entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ REDIS_MASTER="${REDIS_MASTER_HOST}":"${REDIS_MASTER_PORT}"
echo "Testing against Redis Server: ${REDIS_MASTER}"

# skip the "codecov" env if not running on Travis
if [ "${GITHUB_ACTIONS}" = true ] ; then
if [ "${GITHUB_ACTIONS-false}" = true ] ; then
echo "Skipping codecov"
export TOX_SKIP_ENV="codecov"
fi
Expand Down
2 changes: 2 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,8 @@ def parse_response(self, connection, command_name, **options):
raise
if command_name in self.response_callbacks:
return self.response_callbacks[command_name](response, **options)
else:
return response
return response


Expand Down
90 changes: 84 additions & 6 deletions redis/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2416,7 +2416,8 @@ def bzpopmin(self, keys, timeout=0):
keys.append(timeout)
return self.execute_command('BZPOPMIN', *keys)

def zrange(self, name, start, end, desc=False, withscores=False,
def zrange(self, name, start, end, desc=False,
byScore=False, byLex=False, limit=None, withscores=False,
score_cast_func=float):
"""
Return a range of values from sorted set ``name`` between
Expand All @@ -2426,15 +2427,39 @@ def zrange(self, name, start, end, desc=False, withscores=False,

``desc`` a boolean indicating whether to sort the results descendingly

``byScore`` and ``byLex``: boolean. Allows to do selection
by numerical scores or by lexical order.
If both are ``True``, then ``byScore`` get a priority.

``rev``: boolean. Reverses order of elements from highest
to lowest scores.

``limit``: list or tuple of 2 elements or ``None``.
If not None, then the command will record only limit[1]
elements starting from limit[0] element in found range.

See details in Redis documentation for ZRANGE command.

``withscores`` indicates to return the scores along with the values.
The return type is a list of (value, score) pairs

``score_cast_func`` a callable used to cast the score return value
"""
if desc:
return self.zrevrange(name, start, end, withscores,
return self.zrevrange(name, start, end, byScore, byLex,
limit, withscores,
score_cast_func)
pieces = ['ZRANGE', name, start, end]

if byScore:
pieces.append('BYSCORE')
elif byLex:
pieces.append('BYLEX')

if limit is not None:
if len(limit) == 2:
pieces.extend(['LIMIT', limit[0], limit[1]])

if withscores:
pieces.append(b'WITHSCORES')
options = {
Expand All @@ -2443,14 +2468,43 @@ def zrange(self, name, start, end, desc=False, withscores=False,
}
return self.execute_command(*pieces, **options)

def zrangestore(self, dest, name, start, end):
def zrangestore(self, dest, name, start, end, byScore=False, byLex=False,
rev=False, limit=None):
"""
Stores in ``dest`` the result of a range of values from sorted set
``name`` between ``start`` and ``end`` sorted in ascending order.

``start`` and ``end`` can be negative, indicating the end of the range.

``byScore`` and ``byLex``: boolean. Allows to do selection
by numerical scores or by lexical order.
If both are ``True``, then ``byScore`` get a priority.

``rev``: boolean. Reverses order of elements from highest
to lowest scores.

``limit``: list or tuple of 2 elements or ``None``.
If not None, then the command will record only limit[1]
elements starting from limit[0] element in found range.

See details in Redis documentation for ZRANGE command.

"""
return self.execute_command('ZRANGESTORE', dest, name, start, end)

params = [dest, name, start, end]
if byScore:
params.append('BYSCORE')
elif byLex:
params.append('BYLEX')

if rev:
params.append('REV')

if limit is not None:
if len(limit) == 2:
params.extend(['LIMIT', limit[0], limit[1]])

return self.execute_command('ZRANGESTORE', *params)

def zrangebylex(self, name, min, max, start=None, num=None):
"""
Expand Down Expand Up @@ -2548,20 +2602,44 @@ def zremrangebyscore(self, name, min, max):
"""
return self.execute_command('ZREMRANGEBYSCORE', name, min, max)

def zrevrange(self, name, start, end, withscores=False,
def zrevrange(self, name, start, end, byScore=False, byLex=False,
limit=None, withscores=False,
score_cast_func=float):
"""
Return a range of values from sorted set ``name`` between
``start`` and ``end`` sorted in descending order.

``start`` and ``end`` can be negative, indicating the end of the range.
``start`` and ``end`` can be negative, indicating the end
of the range.

``byScore`` and ``byLex``: boolean. Allows to do selection
by numerical scores or by lexical order.
If both are ``True``, then ``byScore`` get a priority.

``rev``: boolean. Reverses order of elements from highest
to lowest scores.

``limit``: list or tuple of 2 elements or ``None``.
If not None, then the command will record only limit[1]
elements starting from limit[0] element in found range.

See details in Redis documentation for ZRANGE command.

``withscores`` indicates to return the scores along with the values
The return type is a list of (value, score) pairs

``score_cast_func`` a callable used to cast the score return value
"""
pieces = ['ZREVRANGE', name, start, end]
if byScore:
pieces.append('BYSCORE')
elif byLex:
pieces.append('BYLEX')

if limit is not None:
if len(limit) == 2:
pieces.extend(['LIMIT', limit[0], limit[1]])

if withscores:
pieces.append(b'WITHSCORES')
options = {
Expand Down