summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2010-06-15 01:40:41 -0700
committerAndy McCurdy <andy@andymccurdy.com>2010-06-15 01:40:41 -0700
commita4cd6bf21bd5ee5cb194c6f4e96bb4b2e93a5ceb (patch)
tree6a53052478541c65d87e1055ac0ebe1520bc5468
parentb686c967e71e3b406f2af4f36744a5258ceca8bb (diff)
downloadredis-py-a4cd6bf21bd5ee5cb194c6f4e96bb4b2e93a5ceb.tar.gz
added ZREMRANGEBYRANK command
-rw-r--r--redis/__init__.py2
-rw-r--r--redis/client.py14
-rw-r--r--tests/server_commands.py13
3 files changed, 27 insertions, 2 deletions
diff --git a/redis/__init__.py b/redis/__init__.py
index 93155fb..18078af 100644
--- a/redis/__init__.py
+++ b/redis/__init__.py
@@ -3,6 +3,8 @@ from redis.client import Redis, ConnectionPool
from redis.exceptions import RedisError, ConnectionError, AuthenticationError
from redis.exceptions import ResponseError, InvalidResponse, InvalidData
+__version__ = '1.36'
+
__all__ = [
'Redis', 'ConnectionPool',
'RedisError', 'ConnectionError', 'ResponseError', 'AuthenticationError'
diff --git a/redis/client.py b/redis/client.py
index 0841c04..3dabcf4 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -208,7 +208,7 @@ class Redis(threading.local):
),
string_keys_to_dict(
'DECRBY HLEN INCRBY LLEN SCARD SDIFFSTORE SINTERSTORE '
- 'SUNIONSTORE ZCARD ZREMRANGEBYSCORE ZREVRANK',
+ 'SUNIONSTORE ZCARD ZREMRANGEBYRANK ZREMRANGEBYSCORE ZREVRANK',
int
),
string_keys_to_dict(
@@ -996,10 +996,19 @@ class Redis(threading.local):
"Remove member ``value`` from sorted set ``name``"
return self.execute_command('ZREM', name, value)
+ def zremrangebyrank(self, name, min, max):
+ """
+ Remove all elements in the sorted set ``name`` with ranks between
+ ``min`` and ``max``. Values are 0-based, ordered from smallest score
+ to largest. Values can be negative indicating the highest scores.
+ Returns the number of elements removed
+ """
+ return self.execute_command('ZREMRANGEBYRANK', name, min, max)
+
def zremrangebyscore(self, name, min, max):
"""
Remove all elements in the sorted set ``name`` with scores
- between ``min`` and ``max``
+ between ``min`` and ``max``. Returns the number of elements removed.
"""
return self.execute_command('ZREMRANGEBYSCORE', name, min, max)
@@ -1283,3 +1292,4 @@ class Pipeline(Redis):
def select(self, *args, **kwargs):
raise RedisError("Cannot select a different database from a pipeline")
+
diff --git a/tests/server_commands.py b/tests/server_commands.py
index 83544f2..0a2acf1 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -16,6 +16,8 @@ class ServerCommandsTestCase(unittest.TestCase):
def tearDown(self):
self.client.flushdb()
+ for c in self.client.connection_pool.get_all_connections():
+ c.disconnect()
# GENERAL SERVER COMMANDS
def test_dbsize(self):
@@ -715,6 +717,17 @@ class ServerCommandsTestCase(unittest.TestCase):
self.assertEquals(self.client.zrem('a', 'b'), False)
self.assertEquals(self.client.zrange('a', 0, 5), ['a1', 'a3'])
+ def test_zremrangebyrank(self):
+ # key is not a zset
+ self.client['a'] = 'a'
+ self.assertRaises(redis.ResponseError, self.client.zremrangebyscore,
+ 'a', 0, 1)
+ del self.client['a']
+ # real logic
+ self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3, 'a4': 4, 'a5': 5})
+ self.assertEquals(self.client.zremrangebyrank('a', 1, 3), 3)
+ self.assertEquals(self.client.zrange('a', 0, 5), ['a1', 'a5'])
+
def test_zremrangebyscore(self):
# key is not a zset
self.client['a'] = 'a'