summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIonuț Arțăriși <iartarisi@suse.cz>2011-10-11 11:24:02 +0200
committerIonuț Arțăriși <iartarisi@suse.cz>2011-10-11 11:24:02 +0200
commitde03d47b9579d6bcda2903b59640a7a845c6274a (patch)
treeb8315c2b783e95b3080d88dcf6ab936ee0eac57d
parent586ce1861fd9c0d0be8ebaf376f004916614c08e (diff)
downloadredis-py-de03d47b9579d6bcda2903b59640a7a845c6274a.tar.gz
use response callbacks for sort() with tuples to work with pipelines
-rw-r--r--redis/client.py27
1 files changed, 18 insertions, 9 deletions
diff --git a/redis/client.py b/redis/client.py
index 50c5905..91ad01b 100644
--- a/redis/client.py
+++ b/redis/client.py
@@ -86,6 +86,16 @@ def zset_score_pairs(response, **options):
it = iter(response)
return zip(it, imap(score_cast_func, it))
+def sort_return_tuples(response, **options):
+ """
+ If ``tuples`` is specified, return the response as a list of
+ n-element tuples with n being the value found in options['tuples']
+ """
+ if not response or not options['tuples']:
+ return response
+ n = options['tuples']
+ return zip(*[response[i::n] for i in range(n)])
+
def int_or_none(response):
if response is None:
return None
@@ -129,6 +139,7 @@ class StrictRedis(object):
'LPUSH RPUSH',
lambda r: isinstance(r, long) and r or r == 'OK'
),
+ string_keys_to_dict('SORT', sort_return_tuples),
string_keys_to_dict('ZSCORE ZINCRBY', float_or_none),
string_keys_to_dict(
'FLUSHALL FLUSHDB LSET LTRIM MSET RENAME '
@@ -731,17 +742,15 @@ class StrictRedis(object):
pieces.append('STORE')
pieces.append(store)
- sorted_list = self.execute_command('SORT', *pieces)
-
+ options = {'tuples': None}
if tuples:
if not get or isinstance(get, basestring) or len(get) < 2:
- raise DataError("``get`` argument must be specified and "
- "contain at least two keys")
- n = len(get)
- return zip(*[sorted_list[i::n] for i in range(n)])
-
- return sorted_list
-
+ raise DataError("when using ``tuples`` the ``get`` argument "
+ "must be specified and contain at least "
+ "two keys")
+ options['tuples'] = len(get)
+
+ return self.execute_command('SORT', *pieces, **options)
#### SET COMMANDS ####
def sadd(self, name, *values):