Skip to content

Commit 0a7701e

Browse files
committed
support ZADD variable length args. deprecate the version passing score and value (in the wrong order).
1 parent 2e836ea commit 0a7701e

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

redis/client.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import time
3-
from itertools import chain, imap, izip, starmap
3+
import warnings
4+
from itertools import imap, izip, starmap
45
from redis.connection import ConnectionPool, UnixDomainSocketConnection
56
from redis.exceptions import (
67
ConnectionError,
@@ -769,9 +770,22 @@ def sunionstore(self, dest, keys, *args):
769770

770771

771772
#### SORTED SET COMMANDS ####
772-
def zadd(self, name, value, score):
773-
"Add member ``value`` with score ``score`` to sorted set ``name``"
774-
return self.execute_command('ZADD', name, score, value)
773+
def zadd(self, name, value=None, score=None, **pairs):
774+
"Add a member to a sorted set. If value and score"
775+
all_pairs = []
776+
if value is not None or score is not None:
777+
if value is None or score is None:
778+
raise RedisError("Both 'value' and 'score' must be specified " \
779+
"to ZADD")
780+
warnings.warn(DeprecationWarning(
781+
"Passing 'value' and 'score' has been deprecated. " \
782+
"Please pass via kwargs instead."))
783+
all_pairs.append(score)
784+
all_pairs.append(value)
785+
for pair in pairs.iteritems():
786+
all_pairs.append(pair[1])
787+
all_pairs.append(pair[0])
788+
return self.execute_command('ZADD', name, *all_pairs)
775789

776790
def zcard(self, name):
777791
"Return the number of elements in the sorted set ``name``"

tests/pipeline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def tearDown(self):
1111

1212
def test_pipeline(self):
1313
pipe = self.client.pipeline()
14-
pipe.set('a', 'a1').get('a').zadd('z', 'z1', 1).zadd('z', 'z2', 4)
14+
pipe.set('a', 'a1').get('a').zadd('z', z1=1).zadd('z', z2=4)
1515
pipe.zincrby('z', 'z1').zrange('z', 0, 5, withscores=True)
1616
self.assertEquals(pipe.execute(),
1717
[

tests/server_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def test_type(self):
250250
self.client.sadd('a', '1')
251251
self.assertEquals(self.client.type('a'), 'set')
252252
del self.client['a']
253-
self.client.zadd('a', '1', 1)
253+
self.client.zadd('a', **{'1': 1})
254254
self.assertEquals(self.client.type('a'), 'zset')
255255

256256
def test_watch(self):
@@ -712,7 +712,7 @@ def test_sunionstore(self):
712712
# SORTED SETS
713713
def make_zset(self, name, d):
714714
for k,v in d.items():
715-
self.client.zadd(name, k, v)
715+
self.client.zadd(name, **{k: v})
716716

717717
def test_zadd(self):
718718
self.make_zset('a', {'a1': 1, 'a2': 2, 'a3': 3})

0 commit comments

Comments
 (0)