summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy McCurdy <andy@andymccurdy.com>2011-05-31 19:21:43 -0700
committerAndy McCurdy <andy@andymccurdy.com>2011-05-31 19:21:43 -0700
commit0a7701e75ee591389ef615490f5d27b8c254df65 (patch)
treeadab32c578a6b76d995f1b4187463a8c74a93d8f
parent2e836ea0b68b140cc265a7098092d3cd604dbafe (diff)
downloadredis-py-0a7701e75ee591389ef615490f5d27b8c254df65.tar.gz
support ZADD variable length args. deprecate the version passing score and value (in the wrong order).
-rw-r--r--redis/client.py22
-rw-r--r--tests/pipeline.py2
-rw-r--r--tests/server_commands.py4
3 files changed, 21 insertions, 7 deletions
diff --git a/redis/client.py b/redis/client.py
index 601965f..2c269eb 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -1,6 +1,7 @@
import datetime
import time
-from itertools import chain, imap, izip, starmap
+import warnings
+from itertools import imap, izip, starmap
from redis.connection import ConnectionPool, UnixDomainSocketConnection
from redis.exceptions import (
ConnectionError,
@@ -769,9 +770,22 @@ class Redis(object):
#### SORTED SET COMMANDS ####
- def zadd(self, name, value, score):
- "Add member ``value`` with score ``score`` to sorted set ``name``"
- return self.execute_command('ZADD', name, score, value)
+ def zadd(self, name, value=None, score=None, **pairs):
+ "Add a member to a sorted set. If value and score"
+ all_pairs = []
+ if value is not None or score is not None:
+ if value is None or score is None:
+ raise RedisError("Both 'value' and 'score' must be specified " \
+ "to ZADD")
+ warnings.warn(DeprecationWarning(
+ "Passing 'value' and 'score' has been deprecated. " \
+ "Please pass via kwargs instead."))
+ all_pairs.append(score)
+ all_pairs.append(value)
+ for pair in pairs.iteritems():
+ all_pairs.append(pair[1])
+ all_pairs.append(pair[0])
+ return self.execute_command('ZADD', name, *all_pairs)
def zcard(self, name):
"Return the number of elements in the sorted set ``name``"
diff --git a/tests/pipeline.py b/tests/pipeline.py
index a0f00a9..6eda4f2 100644
--- a/tests/pipeline.py
+++ b/tests/pipeline.py
@@ -11,7 +11,7 @@ class PipelineTestCase(unittest.TestCase):
def test_pipeline(self):
pipe = self.client.pipeline()
- pipe.set('a', 'a1').get('a').zadd('z', 'z1', 1).zadd('z', 'z2', 4)
+ pipe.set('a', 'a1').get('a').zadd('z', z1=1).zadd('z', z2=4)
pipe.zincrby('z', 'z1').zrange('z', 0, 5, withscores=True)
self.assertEquals(pipe.execute(),
[
diff --git a/tests/server_commands.py b/tests/server_commands.py
index 806c6cb..babb98e 100644
--- a/tests/server_commands.py
+++ b/tests/server_commands.py
@@ -250,7 +250,7 @@ class ServerCommandsTestCase(unittest.TestCase):
self.client.sadd('a', '1')
self.assertEquals(self.client.type('a'), 'set')
del self.client['a']
- self.client.zadd('a', '1', 1)
+ self.client.zadd('a', **{'1': 1})
self.assertEquals(self.client.type('a'), 'zset')
def test_watch(self):
@@ -712,7 +712,7 @@ class ServerCommandsTestCase(unittest.TestCase):
# SORTED SETS
def make_zset(self, name, d):
for k,v in d.items():
- self.client.zadd(name, k, v)
+ self.client.zadd(name, **{k: v})
def test_zadd(self):
self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})