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

#1434 Added support for ZMSCORE new in Redis 6.2 RC #1437

Merged
merged 9 commits into from
Aug 29, 2021
6 changes: 6 additions & 0 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ def parse_zscan(response, **options):
return int(cursor), list(zip(it, map(score_cast_func, it)))


def parse_zmscore(response, **options):
# zmscore: list of scores (double precision floating point number) or nil
return [float(score) if score is not None else None for score in response]


def parse_slowlog_get(response, **options):
space = ' ' if options.get('decode_responses', False) else b' '
return [{
Expand Down Expand Up @@ -701,6 +706,7 @@ class Redis(Commands, object):
'XPENDING': parse_xpending,
'ZADD': parse_zadd,
'ZSCAN': parse_zscan,
'ZMSCORE': parse_zmscore,
}

@classmethod
Expand Down
14 changes: 14 additions & 0 deletions redis/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2445,6 +2445,20 @@ def zunionstore(self, dest, keys, aggregate=None):
"""
return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate)

def zmscore(self, key, members):
"""
Returns the scores associated with the specified members
in the sorted set stored at key.
``members`` should be a list of the member name.
Return type is a list of score.
If the member does not exist, a None will be returned
in corresponding position.
"""
if not members:
raise DataError('ZMSCORE members must be a non-empty list')
pieces = [key] + members
return self.execute_command('ZMSCORE', *pieces)

def _zaggregate(self, command, dest, keys, aggregate=None,
**options):
pieces = [command]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,17 @@ def test_zunionstore_with_weight(self, r):
assert r.zrange('d', 0, -1, withscores=True) == \
[(b'a2', 5), (b'a4', 12), (b'a3', 20), (b'a1', 23)]

@skip_if_server_version_lt('6.1.240')
def test_zmscore(self, r):
with pytest.raises(exceptions.DataError):
r.zmscore('invalid_key', [])

assert r.zmscore('invalid_key', ['invalid_member']) == [None]

r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3.5})
assert r.zmscore('a', ['a1', 'a2', 'a3', 'a4']) == \
[1.0, 2.0, 3.5, None]

# HYPERLOGLOG TESTS
@skip_if_server_version_lt('2.8.9')
def test_pfadd(self, r):
Expand Down