summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorItamar Haber <itamar@redislabs.com>2018-10-05 17:39:52 +0300
committerItamar Haber <itamar@redislabs.com>2018-10-05 17:39:52 +0300
commit66eefc6f52e9130e61dad251438352964946c786 (patch)
tree1f03eedab7f189bee613fdcb8ac754fbdd0e252a
parent0d6c5f28ef87c83df5540abc358252cae3d2060e (diff)
downloadredis-py-66eefc6f52e9130e61dad251438352964946c786.tar.gz
Adds ZPOPMAX and ZPOPMIN
Signed-off-by: Itamar Haber <itamar@redislabs.com>
-rw-r--r--.vscode/settings.json7
-rwxr-xr-xredis/client.py18
-rw-r--r--tests/test_commands.py20
3 files changed, 44 insertions, 1 deletions
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..fc1004a
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,7 @@
+{
+ "python.venvPath": "${workspaceFolder}/.tox",
+ "python.linting.enabled": false,
+ "python.unitTest.unittestEnabled": false,
+ "python.unitTest.nosetestsEnabled": false,
+ "python.unitTest.pyTestEnabled": true
+} \ No newline at end of file
diff --git a/redis/client.py b/redis/client.py
index 79e94d0..95e9438 100755
--- a/redis/client.py
+++ b/redis/client.py
@@ -391,7 +391,7 @@ class StrictRedis(object):
lambda r: r and set(r) or set()
),
string_keys_to_dict(
- 'ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE',
+ 'ZPOPMAX ZPOPMIN ZRANGE ZRANGEBYSCORE ZREVRANGE ZREVRANGEBYSCORE',
zset_score_pairs
),
string_keys_to_dict('ZRANK ZREVRANK', int_or_none),
@@ -1728,6 +1728,22 @@ class StrictRedis(object):
"""
return self.execute_command('ZLEXCOUNT', name, min, max)
+ def zpopmax(self, name, count=None):
+ "Remove and return up to ``count`` members with the highest scores from the sorted set ``name``"
+ args = (count is not None) and [count] or []
+ options = {
+ 'withscores': True
+ }
+ return self.execute_command('ZPOPMAX', name, *args, **options)
+
+ def zpopmin(self, name, count=None):
+ "Remove and return up to ``count`` members with the lowest scores from the sorted set ``name``"
+ args = (count is not None) and [count] or []
+ options = {
+ 'withscores': True
+ }
+ return self.execute_command('ZPOPMIN', name, *args, **options)
+
def zrange(self, name, start, end, desc=False, withscores=False,
score_cast_func=float):
"""
diff --git a/tests/test_commands.py b/tests/test_commands.py
index b9b9b66..a3e1a33 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -969,6 +969,26 @@ class TestRedisCommands(object):
assert r.zrange('d', 0, -1, withscores=True) == \
[(b('a3'), 20), (b('a1'), 23)]
+ @skip_if_server_version_lt('4.9.0')
+ def test_zpopmax(self, r):
+ r.delete('a')
+ r.zadd('a', a1=1, a2=2, a3=3)
+ assert r.zpopmax('a') == [(b('a3'), 3)]
+
+ # with count
+ assert r.zpopmax('a', count=2) == \
+ [(b('a2'), 2), (b('a1'), 1)]
+
+ @skip_if_server_version_lt('4.9.0')
+ def test_zpopmin(self, r):
+ r.delete('a')
+ r.zadd('a', a1=1, a2=2, a3=3)
+ assert r.zpopmin('a') == [(b('a1'), 1)]
+
+ # with count
+ assert r.zpopmin('a', count=2) == \
+ [(b('a2'), 2), (b('a3'), 3)]
+
def test_zrange(self, r):
r.zadd('a', a1=1, a2=2, a3=3)
assert r.zrange('a', 0, 1) == [b('a1'), b('a2')]