andymccurdy/redis-py — pull request #202 by mapleoin (2011-10-10 15:58:38+00:00)
Title: add an optional argument to sort to make it return tuples

I always find I need to use an extra function to consume the values returned by the **sort** command when used with more than one **get** keys. I think I might not be the only one who would like sort to return a list of tuples instead of a flat list.

The tests explain it quite well I think, but here's an extra example:

Considering we have those records:

```python
>>> r.lrange('a', 0, 3)
['1', '2', '3']
>>> r.mget('user:1', 'user:2', 'user:3')
['u1', 'u2', 'u3']
```

This is my proposed behaviour:

leave the old way the same:

```python
>>> r.sort('a', get=('user:*', '#'))
['u1', '1', 'u2', '2', 'u3', '3']
```

add an optional **tuples** keyword argument in order to get a list of tuples:

```python
>>> r.sort('a', get=('user:*', '#'), tuples=True)
[('u1', '1'), ('u2', '2'), ('u3', '3')]
```

Now you can easily iterate through the returned values:

```python
>>> for user, apples in r.sort('a', get=('user:*', '#'), tuples=True):
... print "User %s has %s apples." % (user, apples)
...
User u1 has 1 apples.
User u2 has 2 apples.
User u3 has 3 apples.
```