Skip to content

Commit 5964d70

Browse files
Jiekunjiekun.zhu
Jiekun
and
jiekun.zhu
authored
#1434 Added support for ZMSCORE new in Redis 6.2 RC (#1437)
* #1434 Added zmscore command support * #1434 Fixed typo and doc * #1434 Set [] as default value for members arg in zmscore func * #1434 Set None as default value for members arg in zmscore func * #1434 Removed default value for members arg in zmscore func * Fixed flake8 formatting Co-authored-by: jiekun.zhu <jiekun.zhu@shopee.com>
1 parent 295b547 commit 5964d70

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Diff for: redis/client.py

+6
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ def parse_zscan(response, **options):
390390
return int(cursor), list(zip(it, map(score_cast_func, it)))
391391

392392

393+
def parse_zmscore(response, **options):
394+
# zmscore: list of scores (double precision floating point number) or nil
395+
return [float(score) if score is not None else None for score in response]
396+
397+
393398
def parse_slowlog_get(response, **options):
394399
space = ' ' if options.get('decode_responses', False) else b' '
395400
return [{
@@ -705,6 +710,7 @@ class Redis(Commands, object):
705710
'XPENDING': parse_xpending,
706711
'ZADD': parse_zadd,
707712
'ZSCAN': parse_zscan,
713+
'ZMSCORE': parse_zmscore,
708714
}
709715

710716
@classmethod

Diff for: redis/commands.py

+14
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,20 @@ def zunionstore(self, dest, keys, aggregate=None):
24612461
"""
24622462
return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate)
24632463

2464+
def zmscore(self, key, members):
2465+
"""
2466+
Returns the scores associated with the specified members
2467+
in the sorted set stored at key.
2468+
``members`` should be a list of the member name.
2469+
Return type is a list of score.
2470+
If the member does not exist, a None will be returned
2471+
in corresponding position.
2472+
"""
2473+
if not members:
2474+
raise DataError('ZMSCORE members must be a non-empty list')
2475+
pieces = [key] + members
2476+
return self.execute_command('ZMSCORE', *pieces)
2477+
24642478
def _zaggregate(self, command, dest, keys, aggregate=None,
24652479
**options):
24662480
pieces = [command]

Diff for: tests/test_commands.py

+11
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,17 @@ def test_zunionstore_with_weight(self, r):
18651865
assert r.zrange('d', 0, -1, withscores=True) == \
18661866
[(b'a2', 5), (b'a4', 12), (b'a3', 20), (b'a1', 23)]
18671867

1868+
@skip_if_server_version_lt('6.1.240')
1869+
def test_zmscore(self, r):
1870+
with pytest.raises(exceptions.DataError):
1871+
r.zmscore('invalid_key', [])
1872+
1873+
assert r.zmscore('invalid_key', ['invalid_member']) == [None]
1874+
1875+
r.zadd('a', {'a1': 1, 'a2': 2, 'a3': 3.5})
1876+
assert r.zmscore('a', ['a1', 'a2', 'a3', 'a4']) == \
1877+
[1.0, 2.0, 3.5, None]
1878+
18681879
# HYPERLOGLOG TESTS
18691880
@skip_if_server_version_lt('2.8.9')
18701881
def test_pfadd(self, r):

0 commit comments

Comments
 (0)