From 49af347fab7b88c6b084d18697d11b75935331f6 Mon Sep 17 00:00:00 2001 From: 2014BDuck <2014bduck@gmail.com> Date: Mon, 21 Dec 2020 15:16:30 +0800 Subject: [PATCH 1/6] #1434 Added zmscore command support --- redis/client.py | 21 +++++++++++++++++++++ tests/test_commands.py | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/redis/client.py b/redis/client.py index 42d1bfaa53..e287c93c39 100755 --- a/redis/client.py +++ b/redis/client.py @@ -398,6 +398,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 [{ @@ -688,6 +693,7 @@ class Redis: 'XPENDING': parse_xpending, 'ZADD': parse_zadd, 'ZSCAN': parse_zscan, + 'ZMSCORE': parse_zmscore, } @classmethod @@ -3058,6 +3064,21 @@ 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 no member can be matched, an empty list will be returned. + """ + if not isinstance(members, list) or len(members) < 1: + 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): pieces = [command, dest, len(keys)] if isinstance(keys, dict): diff --git a/tests/test_commands.py b/tests/test_commands.py index 211307859a..235d74c89e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1631,6 +1631,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): From 77eadf345bc417a64cded51569f01b0bdaf95ac0 Mon Sep 17 00:00:00 2001 From: 2014BDuck <2014bduck@gmail.com> Date: Mon, 21 Dec 2020 15:20:27 +0800 Subject: [PATCH 2/6] #1434 Fixed typo and doc --- redis/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index e287c93c39..7446990906 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3071,14 +3071,15 @@ def zmscore(self, key, members): ``members`` should be a list of the member name. Return type is a list of score. - If no member can be matched, an empty list will be returned. + + If the member does not exist, a None will be returned + in corresponding position. """ if not isinstance(members, list) or len(members) < 1: 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): pieces = [command, dest, len(keys)] if isinstance(keys, dict): From 234e7d02f13a9c38dfe38d3d72bd28f94712ca08 Mon Sep 17 00:00:00 2001 From: 2014BDuck <2014bduck@gmail.com> Date: Mon, 16 Aug 2021 19:17:01 +0800 Subject: [PATCH 3/6] #1434 Set [] as default value for members arg in zmscore func --- redis/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index ae4b58b53d..d6070dbd01 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3366,7 +3366,7 @@ def zunionstore(self, dest, keys, aggregate=None): """ return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate) - def zmscore(self, key, members): + def zmscore(self, key, members=[]): """ Returns the scores associated with the specified members in the sorted set stored at key. @@ -3377,7 +3377,7 @@ def zmscore(self, key, members): If the member does not exist, a None will be returned in corresponding position. """ - if not isinstance(members, list) or len(members) < 1: + if not members: raise DataError('ZMSCORE members must be a non-empty list') pieces = [key] + members return self.execute_command('ZMSCORE', *pieces) From 1cbf7cd6d43755610f9edaa147a12786eb8a31b3 Mon Sep 17 00:00:00 2001 From: 2014BDuck <2014bduck@gmail.com> Date: Mon, 16 Aug 2021 19:28:44 +0800 Subject: [PATCH 4/6] #1434 Set None as default value for members arg in zmscore func --- redis/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index d6070dbd01..051d8ce4e7 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3366,7 +3366,7 @@ def zunionstore(self, dest, keys, aggregate=None): """ return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate) - def zmscore(self, key, members=[]): + def zmscore(self, key, members=None): """ Returns the scores associated with the specified members in the sorted set stored at key. From 1448f2ce45f3c2920f02a117a60ca034a6ce2d11 Mon Sep 17 00:00:00 2001 From: 2014BDuck <2014bduck@gmail.com> Date: Mon, 16 Aug 2021 19:31:26 +0800 Subject: [PATCH 5/6] #1434 Removed default value for members arg in zmscore func --- redis/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 051d8ce4e7..65ccfdcfce 100755 --- a/redis/client.py +++ b/redis/client.py @@ -3366,7 +3366,7 @@ def zunionstore(self, dest, keys, aggregate=None): """ return self._zaggregate('ZUNIONSTORE', dest, keys, aggregate) - def zmscore(self, key, members=None): + def zmscore(self, key, members): """ Returns the scores associated with the specified members in the sorted set stored at key. From 9bc7507f3bcde6f15d0de4d8a18a43d06f513a44 Mon Sep 17 00:00:00 2001 From: "jiekun.zhu" Date: Mon, 23 Aug 2021 20:06:39 +0800 Subject: [PATCH 6/6] Fixed flake8 formatting --- redis/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 888e52cf9f..ec11154415 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1899,4 +1899,4 @@ def execute(self): """ command = self.command self.reset() - return self.client.execute_command(*command) \ No newline at end of file + return self.client.execute_command(*command)