diff options
author | Andy McCurdy <andy@andymccurdy.com> | 2014-04-07 17:14:05 -0700 |
---|---|---|
committer | Andy McCurdy <andy@andymccurdy.com> | 2014-04-07 17:14:05 -0700 |
commit | bee60a661062bea5f9fbdd7dc28bc17ecd902005 (patch) | |
tree | c5c54397d5172be917bd025ef0f442a6c1e682d7 /redis/client.py | |
parent | 82a76b8621d9d0d722cac2deaf50bbee78b53e51 (diff) | |
parent | 8cd9f2311340e5eea01c5956bb1c4d4079a0b9d8 (diff) | |
download | redis-py-bee60a661062bea5f9fbdd7dc28bc17ecd902005.tar.gz |
Merge branch 'pr/441'
Conflicts:
redis/client.py
tests/test_commands.py
Diffstat (limited to 'redis/client.py')
-rw-r--r-- | redis/client.py | 89 |
1 files changed, 81 insertions, 8 deletions
diff --git a/redis/client.py b/redis/client.py index 4713e43..3855480 100644 --- a/redis/client.py +++ b/redis/client.py @@ -244,18 +244,20 @@ def parse_script(response, **options): def parse_scan(response, **options): - return response + cursor, r = response + return nativestr(cursor), r def parse_hscan(response, **options): cursor, r = response - return cursor, r and pairs_to_dict(r) or {} + return nativestr(cursor), r and pairs_to_dict(r) or {} def parse_zscan(response, **options): score_cast_func = options.get('score_cast_func', float) - it = iter(response[1]) - return [response[0], list(izip(it, imap(score_cast_func, it)))] + cursor, r = response + it = iter(r) + return nativestr(cursor), list(izip(it, imap(score_cast_func, it))) class StrictRedis(object): @@ -1182,7 +1184,8 @@ class StrictRedis(object): # SCAN COMMANDS def scan(self, cursor=0, match=None, count=None): """ - Scan and return (nextcursor, keys) + Incrementally return lists of key names. Also return a cursor + indicating the scan position. ``match`` allows for filtering the keys by pattern @@ -1195,9 +1198,25 @@ class StrictRedis(object): pieces.extend(['COUNT', count]) return self.execute_command('SCAN', *pieces) + def scan_iter(self, match=None, count=None): + """ + Make an iterator using the SCAN command so that the client doesn't + need to remember the cursor position. + + ``match`` allows for filtering the keys by pattern + + ``count`` allows for hint the minimum number of returns + """ + cursor = 0 + while cursor != '0': + cursor, data = self.scan(cursor=cursor, match=match, count=count) + for item in data: + yield item + def sscan(self, name, cursor=0, match=None, count=None): """ - Scan and return (nextcursor, members_of_set) + Incrementally return lists of elements in a set. Also return a cursor + indicating the scan position. ``match`` allows for filtering the keys by pattern @@ -1210,9 +1229,26 @@ class StrictRedis(object): pieces.extend(['COUNT', count]) return self.execute_command('SSCAN', *pieces) + def sscan_iter(self, name, match=None, count=None): + """ + Make an iterator using the SSCAN command so that the client doesn't + need to remember the cursor position. + + ``match`` allows for filtering the keys by pattern + + ``count`` allows for hint the minimum number of returns + """ + cursor = 0 + while cursor != '0': + cursor, data = self.sscan(name, cursor=cursor, + match=match, count=count) + for item in data: + yield item + def hscan(self, name, cursor=0, match=None, count=None): """ - Scan and return (nextcursor, dict) + Incrementally return key/value slices in a hash. Also return a cursor + indicating the scan position. ``match`` allows for filtering the keys by pattern @@ -1225,10 +1261,27 @@ class StrictRedis(object): pieces.extend(['COUNT', count]) return self.execute_command('HSCAN', *pieces) + def hscan_iter(self, name, match=None, count=None): + """ + Make an iterator using the HSCAN command so that the client doesn't + need to remember the cursor position. + + ``match`` allows for filtering the keys by pattern + + ``count`` allows for hint the minimum number of returns + """ + cursor = 0 + while cursor != '0': + cursor, data = self.hscan(name, cursor=cursor, + match=match, count=count) + for item in data.items(): + yield item + def zscan(self, name, cursor=0, match=None, count=None, score_cast_func=float): """ - Scan and return (nextcursor, pairs) + Incrementally return lists of elements in a sorted set. Also return a + cursor indicating the scan position. ``match`` allows for filtering the keys by pattern @@ -1244,6 +1297,26 @@ class StrictRedis(object): options = {'score_cast_func': score_cast_func} return self.execute_command('ZSCAN', *pieces, **options) + def zscan_iter(self, name, match=None, count=None, + score_cast_func=float): + """ + Make an iterator using the ZSCAN command so that the client doesn't + need to remember the cursor position. + + ``match`` allows for filtering the keys by pattern + + ``count`` allows for hint the minimum number of returns + + ``score_cast_func`` a callable used to cast the score return value + """ + cursor = 0 + while cursor != '0': + cursor, data = self.zscan(name, cursor=cursor, match=match, + count=count, + score_cast_func=score_cast_func) + for item in data: + yield item + # SET COMMANDS def sadd(self, name, *values): "Add ``value(s)`` to set ``name``" |