Skip to content

Commit 238f69e

Browse files
wavenatorchayim
andauthored
Add a count parameter to lpop/rpop for redis >= 6.2.0 (#1487)
Co-authored-by: Chayim <[email protected]>
1 parent 5240d60 commit 238f69e

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

docker/base/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
FROM redis:6.2.5-buster
1+
FROM redis:6.2.5-buster

redis/client.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,9 +2148,18 @@ def llen(self, name):
21482148
"Return the length of the list ``name``"
21492149
return self.execute_command('LLEN', name)
21502150

2151-
def lpop(self, name):
2152-
"Remove and return the first item of the list ``name``"
2153-
return self.execute_command('LPOP', name)
2151+
def lpop(self, name, count=None):
2152+
"""
2153+
Removes and returns the first elements of the list ``name``.
2154+
2155+
By default, the command pops a single element from the beginning of
2156+
the list. When provided with the optional ``count`` argument, the reply
2157+
will consist of up to count elements, depending on the list's length.
2158+
"""
2159+
if count is not None:
2160+
return self.execute_command('LPOP', name, count)
2161+
else:
2162+
return self.execute_command('LPOP', name)
21542163

21552164
def lpush(self, name, *values):
21562165
"Push ``values`` onto the head of the list ``name``"
@@ -2196,9 +2205,18 @@ def ltrim(self, name, start, end):
21962205
"""
21972206
return self.execute_command('LTRIM', name, start, end)
21982207

2199-
def rpop(self, name):
2200-
"Remove and return the last item of the list ``name``"
2201-
return self.execute_command('RPOP', name)
2208+
def rpop(self, name, count=None):
2209+
"""
2210+
Removes and returns the last elements of the list ``name``.
2211+
2212+
By default, the command pops a single element from the end of the list.
2213+
When provided with the optional ``count`` argument, the reply will
2214+
consist of up to count elements, depending on the list's length.
2215+
"""
2216+
if count is not None:
2217+
return self.execute_command('RPOP', name, count)
2218+
else:
2219+
return self.execute_command('RPOP', name)
22022220

22032221
def rpoplpush(self, src, dst):
22042222
"""

tests/test_commands.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,14 @@ def test_lpop(self, r):
11221122
assert r.lpop('a') == b'3'
11231123
assert r.lpop('a') is None
11241124

1125+
@skip_if_server_version_lt('6.2.0')
1126+
def test_lpop_count(self, r):
1127+
r.rpush('a', '1', '2', '3')
1128+
assert r.lpop('a', 2) == [b'1', b'2']
1129+
assert r.lpop('a', 1) == [b'3']
1130+
assert r.lpop('a') is None
1131+
assert r.lpop('a', 3) is None
1132+
11251133
def test_lpush(self, r):
11261134
assert r.lpush('a', '1') == 1
11271135
assert r.lpush('a', '2') == 2
@@ -1171,6 +1179,14 @@ def test_rpop(self, r):
11711179
assert r.rpop('a') == b'1'
11721180
assert r.rpop('a') is None
11731181

1182+
@skip_if_server_version_lt('6.2.0')
1183+
def test_rpop_count(self, r):
1184+
r.rpush('a', '1', '2', '3')
1185+
assert r.rpop('a', 2) == [b'3', b'2']
1186+
assert r.rpop('a', 1) == [b'1']
1187+
assert r.rpop('a') is None
1188+
assert r.rpop('a', 3) is None
1189+
11741190
def test_rpoplpush(self, r):
11751191
r.rpush('a', 'a1', 'a2', 'a3')
11761192
r.rpush('b', 'b1', 'b2', 'b3')

0 commit comments

Comments
 (0)