Skip to content

Commit

Permalink
use response callbacks for sort() with tuples to work with pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
iartarisi committed Oct 11, 2011
1 parent 586ce18 commit de03d47
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions redis/client.py
Expand Up @@ -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
Expand Down Expand Up @@ -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 '
Expand Down Expand Up @@ -731,17 +742,15 @@ def sort(self, name, start=None, num=None, by=None, get=None,
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):
Expand Down

0 comments on commit de03d47

Please sign in to comment.