From 40354f5c04532c3e2d8f2f391e29682d4b54d27a Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Wed, 14 Jul 2021 12:30:34 +0300 Subject: [PATCH 1/9] implementing the lmove command closes #1503 --- redis/client.py | 11 ++++++++++- tests/test_commands.py | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 59575cd835..7d369fa17f 100755 --- a/redis/client.py +++ b/redis/client.py @@ -561,7 +561,7 @@ class Redis: """ RESPONSE_CALLBACKS = { **string_keys_to_dict( - 'AUTH EXPIRE EXPIREAT HEXISTS HMSET MOVE MSETNX PERSIST ' + 'AUTH EXPIRE EXPIREAT HEXISTS HMSET LOVE MOVE MSETNX PERSIST ' 'PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX', bool ), @@ -1726,6 +1726,15 @@ def keys(self, pattern='*'): "Returns a list of keys matching ``pattern``" return self.execute_command('KEYS', pattern) + def lmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): + """ + Atomically returns and removes the first/last element of a list, + pushing it as the first/last element on the destination list. + Returns the element being popped and pushed. + """ + params = [first_list, second_list, src, dest] + return self.execute_command("LMOVE", *params) + def mget(self, keys, *args): """ Returns a list of values ordered identically to ``keys`` diff --git a/tests/test_commands.py b/tests/test_commands.py index d1f85b7306..752ac3ee4e 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -781,6 +781,12 @@ def test_mget(self, r): r['c'] = '3' assert r.mget('a', 'other', 'b', 'c') == [b'1', None, b'2', b'3'] + @skip_if_server_version_lt('6.2.0') + def test_lmove(self, r): + r.rpush('a', 'one', 'two', 'three', 'four') + assert r.lmove('a', 'b') == b'one' + assert r.lmove('a', 'b', 'right', 'left') == b'four' + def test_mset(self, r): d = {'a': b'1', 'b': b'2', 'c': b'3'} assert r.mset(d) From 712c47763690b8212173e3d838df23b948d21e34 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Wed, 14 Jul 2021 13:16:36 +0300 Subject: [PATCH 2/9] support for blmove --- redis/client.py | 7 +++++++ tests/test_commands.py | 8 +++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index 7d369fa17f..6e93142140 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1735,6 +1735,13 @@ def lmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): params = [first_list, second_list, src, dest] return self.execute_command("LMOVE", *params) + def blmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): + """ + Blocking version of lmove. + """ + return self.lmove(first_list, second_list, src, dest) + + def mget(self, keys, *args): """ Returns a list of values ordered identically to ``keys`` diff --git a/tests/test_commands.py b/tests/test_commands.py index 752ac3ee4e..bb8bb3f0e0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -781,12 +781,18 @@ def test_mget(self, r): r['c'] = '3' assert r.mget('a', 'other', 'b', 'c') == [b'1', None, b'2', b'3'] - @skip_if_server_version_lt('6.2.0') def test_lmove(self, r): r.rpush('a', 'one', 'two', 'three', 'four') assert r.lmove('a', 'b') == b'one' assert r.lmove('a', 'b', 'right', 'left') == b'four' + @skip_if_server_version_lt('6.2.0') + def test_blmove(self, r): + r.rpush('a', 'one', 'two', 'three', 'four') + assert r.blmove('a', 'b') == b'one' + assert r.blmove('a', 'b', 'right', 'left') == b'four' + + def test_mset(self, r): d = {'a': b'1', 'b': b'2', 'c': b'3'} assert r.mset(d) From 14cfc151847caf9adf7cc4c6fef7424cf91eadd0 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Wed, 14 Jul 2021 14:03:32 +0300 Subject: [PATCH 3/9] flake8 --- redis/client.py | 1 - tests/test_commands.py | 1 - 2 files changed, 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index 6e93142140..50175d926c 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1741,7 +1741,6 @@ def blmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): """ return self.lmove(first_list, second_list, src, dest) - def mget(self, keys, *args): """ Returns a list of values ordered identically to ``keys`` diff --git a/tests/test_commands.py b/tests/test_commands.py index bb8bb3f0e0..4f1a6db670 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -792,7 +792,6 @@ def test_blmove(self, r): assert r.blmove('a', 'b') == b'one' assert r.blmove('a', 'b', 'right', 'left') == b'four' - def test_mset(self, r): d = {'a': b'1', 'b': b'2', 'c': b'3'} assert r.mset(d) From 242ff5660a5e9dfba935eed8c57a6733aaedaaca Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Wed, 14 Jul 2021 14:11:51 +0300 Subject: [PATCH 4/9] adding decorator for 6.2.0 on lmove --- tests/test_commands.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_commands.py b/tests/test_commands.py index 4f1a6db670..ec50445abe 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -781,6 +781,7 @@ def test_mget(self, r): r['c'] = '3' assert r.mget('a', 'other', 'b', 'c') == [b'1', None, b'2', b'3'] + @skip_if_server_version_lt('6.2.0') def test_lmove(self, r): r.rpush('a', 'one', 'two', 'three', 'four') assert r.lmove('a', 'b') == b'one' From 81f0adacf787db8a47c6031db420412c53f229d8 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Sun, 25 Jul 2021 12:18:20 +0300 Subject: [PATCH 5/9] string and test fix --- redis/client.py | 2 +- tests/test_commands.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/redis/client.py b/redis/client.py index 50175d926c..09a7fe66d1 100755 --- a/redis/client.py +++ b/redis/client.py @@ -561,7 +561,7 @@ class Redis: """ RESPONSE_CALLBACKS = { **string_keys_to_dict( - 'AUTH EXPIRE EXPIREAT HEXISTS HMSET LOVE MOVE MSETNX PERSIST ' + 'AUTH EXPIRE EXPIREAT HEXISTS HMSET LMOVE BLMOVE MOVE MSETNX PERSIST ' 'PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX', bool ), diff --git a/tests/test_commands.py b/tests/test_commands.py index d2bdc927f0..fe74e82dfe 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -783,14 +783,14 @@ def test_mget(self, r): @skip_if_server_version_lt('6.2.0') def test_lmove(self, r): r.rpush('a', 'one', 'two', 'three', 'four') - assert r.lmove('a', 'b') == b'one' - assert r.lmove('a', 'b', 'right', 'left') == b'four' + assert r.lmove('a', 'b') + assert r.lmove('a', 'b', 'right', 'left') @skip_if_server_version_lt('6.2.0') def test_blmove(self, r): r.rpush('a', 'one', 'two', 'three', 'four') - assert r.blmove('a', 'b') == b'one' - assert r.blmove('a', 'b', 'right', 'left') == b'four' + assert r.blmove('a', 'b') + assert r.blmove('a', 'b', 'right', 'left') def test_mset(self, r): d = {'a': b'1', 'b': b'2', 'c': b'3'} From e5e7062b0a8343fe004ba2d419b57358ca0dc266 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Thu, 29 Jul 2021 16:39:16 +0300 Subject: [PATCH 6/9] bl vs lm --- redis/client.py | 3 ++- tests/test_commands.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index da8408693d..052a4ef0c0 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1835,7 +1835,8 @@ def blmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): """ Blocking version of lmove. """ - return self.lmove(first_list, second_list, src, dest) + params = [first_list, second_list, src, dest] + return self.execute_command("BLMOVE", *params) def mget(self, keys, *args): """ diff --git a/tests/test_commands.py b/tests/test_commands.py index 4306c7985e..659251d76a 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -865,7 +865,7 @@ def test_lmove(self, r): def test_blmove(self, r): r.rpush('a', 'one', 'two', 'three', 'four') assert r.blmove('a', 'b') - assert r.blmove('a', 'b', 'right', 'left') + assert r.blmove('a', 'b', 'RIGHT', 'LEFT') def test_mset(self, r): d = {'a': b'1', 'b': b'2', 'c': b'3'} From a393da5a81d0021fd86d37f4709389bb7efe21a1 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Fri, 30 Jul 2021 08:22:01 +0300 Subject: [PATCH 7/9] added timeout thanks to docs update --- redis/client.py | 4 ++-- tests/test_commands.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/redis/client.py b/redis/client.py index 052a4ef0c0..eac26ba378 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1831,11 +1831,11 @@ def lmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): params = [first_list, second_list, src, dest] return self.execute_command("LMOVE", *params) - def blmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): + def blmove(self, first_list, second_list, timeout, src="LEFT", dest="RIGHT"): """ Blocking version of lmove. """ - params = [first_list, second_list, src, dest] + params = [first_list, second_list, src, dest, timeout] return self.execute_command("BLMOVE", *params) def mget(self, keys, *args): diff --git a/tests/test_commands.py b/tests/test_commands.py index 659251d76a..bd1a0eac12 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -864,8 +864,8 @@ def test_lmove(self, r): @skip_if_server_version_lt('6.2.0') def test_blmove(self, r): r.rpush('a', 'one', 'two', 'three', 'four') - assert r.blmove('a', 'b') - assert r.blmove('a', 'b', 'RIGHT', 'LEFT') + assert r.blmove('a', 'b', 5) + assert r.blmove('a', 'b', 1, 'RIGHT', 'LEFT') def test_mset(self, r): d = {'a': b'1', 'b': b'2', 'c': b'3'} From ded776f2dbf922f02b46a3ad4b38f154b81b9067 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Wed, 4 Aug 2021 11:28:46 +0300 Subject: [PATCH 8/9] flake8 --- redis/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/redis/client.py b/redis/client.py index eac26ba378..4cf1ac001e 100755 --- a/redis/client.py +++ b/redis/client.py @@ -561,8 +561,8 @@ class Redis: """ RESPONSE_CALLBACKS = { **string_keys_to_dict( - 'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET LMOVE BLMOVE MOVE MSETNX ' - 'PERSIST PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX', + 'AUTH COPY EXPIRE EXPIREAT HEXISTS HMSET LMOVE BLMOVE MOVE ' + 'MSETNX PERSIST PSETEX RENAMENX SISMEMBER SMOVE SETEX SETNX', bool ), **string_keys_to_dict( From 643ac8d3e53baa0ece17dd1839449680c75c5c64 Mon Sep 17 00:00:00 2001 From: "Chayim I. Kirshen" Date: Wed, 11 Aug 2021 11:14:13 +0300 Subject: [PATCH 9/9] flake8 --- redis/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/redis/client.py b/redis/client.py index d67ebcd3c7..0b572a4511 100755 --- a/redis/client.py +++ b/redis/client.py @@ -1843,7 +1843,8 @@ def lmove(self, first_list, second_list, src="LEFT", dest="RIGHT"): params = [first_list, second_list, src, dest] return self.execute_command("LMOVE", *params) - def blmove(self, first_list, second_list, timeout, src="LEFT", dest="RIGHT"): + def blmove(self, first_list, second_list, timeout, + src="LEFT", dest="RIGHT"): """ Blocking version of lmove. """