@@ -244,18 +244,20 @@ def parse_script(response, **options):
244
244
245
245
246
246
def parse_scan (response , ** options ):
247
- return response
247
+ cursor , r = response
248
+ return nativestr (cursor ), r
248
249
249
250
250
251
def parse_hscan (response , ** options ):
251
252
cursor , r = response
252
- return cursor , r and pairs_to_dict (r ) or {}
253
+ return nativestr ( cursor ) , r and pairs_to_dict (r ) or {}
253
254
254
255
255
256
def parse_zscan (response , ** options ):
256
257
score_cast_func = options .get ('score_cast_func' , float )
257
- it = iter (response [1 ])
258
- return [response [0 ], list (izip (it , imap (score_cast_func , it )))]
258
+ cursor , r = response
259
+ it = iter (r )
260
+ return nativestr (cursor ), list (izip (it , imap (score_cast_func , it )))
259
261
260
262
261
263
class StrictRedis (object ):
@@ -1182,7 +1184,8 @@ def sort(self, name, start=None, num=None, by=None, get=None,
1182
1184
# SCAN COMMANDS
1183
1185
def scan (self , cursor = 0 , match = None , count = None ):
1184
1186
"""
1185
- Scan and return (nextcursor, keys)
1187
+ Incrementally return lists of key names. Also return a cursor
1188
+ indicating the scan position.
1186
1189
1187
1190
``match`` allows for filtering the keys by pattern
1188
1191
@@ -1195,9 +1198,25 @@ def scan(self, cursor=0, match=None, count=None):
1195
1198
pieces .extend (['COUNT' , count ])
1196
1199
return self .execute_command ('SCAN' , * pieces )
1197
1200
1201
+ def scan_iter (self , match = None , count = None ):
1202
+ """
1203
+ Make an iterator using the SCAN command so that the client doesn't
1204
+ need to remember the cursor position.
1205
+
1206
+ ``match`` allows for filtering the keys by pattern
1207
+
1208
+ ``count`` allows for hint the minimum number of returns
1209
+ """
1210
+ cursor = 0
1211
+ while cursor != '0' :
1212
+ cursor , data = self .scan (cursor = cursor , match = match , count = count )
1213
+ for item in data :
1214
+ yield item
1215
+
1198
1216
def sscan (self , name , cursor = 0 , match = None , count = None ):
1199
1217
"""
1200
- Scan and return (nextcursor, members_of_set)
1218
+ Incrementally return lists of elements in a set. Also return a cursor
1219
+ indicating the scan position.
1201
1220
1202
1221
``match`` allows for filtering the keys by pattern
1203
1222
@@ -1210,9 +1229,26 @@ def sscan(self, name, cursor=0, match=None, count=None):
1210
1229
pieces .extend (['COUNT' , count ])
1211
1230
return self .execute_command ('SSCAN' , * pieces )
1212
1231
1232
+ def sscan_iter (self , name , match = None , count = None ):
1233
+ """
1234
+ Make an iterator using the SSCAN command so that the client doesn't
1235
+ need to remember the cursor position.
1236
+
1237
+ ``match`` allows for filtering the keys by pattern
1238
+
1239
+ ``count`` allows for hint the minimum number of returns
1240
+ """
1241
+ cursor = 0
1242
+ while cursor != '0' :
1243
+ cursor , data = self .sscan (name , cursor = cursor ,
1244
+ match = match , count = count )
1245
+ for item in data :
1246
+ yield item
1247
+
1213
1248
def hscan (self , name , cursor = 0 , match = None , count = None ):
1214
1249
"""
1215
- Scan and return (nextcursor, dict)
1250
+ Incrementally return key/value slices in a hash. Also return a cursor
1251
+ indicating the scan position.
1216
1252
1217
1253
``match`` allows for filtering the keys by pattern
1218
1254
@@ -1225,10 +1261,27 @@ def hscan(self, name, cursor=0, match=None, count=None):
1225
1261
pieces .extend (['COUNT' , count ])
1226
1262
return self .execute_command ('HSCAN' , * pieces )
1227
1263
1264
+ def hscan_iter (self , name , match = None , count = None ):
1265
+ """
1266
+ Make an iterator using the HSCAN command so that the client doesn't
1267
+ need to remember the cursor position.
1268
+
1269
+ ``match`` allows for filtering the keys by pattern
1270
+
1271
+ ``count`` allows for hint the minimum number of returns
1272
+ """
1273
+ cursor = 0
1274
+ while cursor != '0' :
1275
+ cursor , data = self .hscan (name , cursor = cursor ,
1276
+ match = match , count = count )
1277
+ for item in data .items ():
1278
+ yield item
1279
+
1228
1280
def zscan (self , name , cursor = 0 , match = None , count = None ,
1229
1281
score_cast_func = float ):
1230
1282
"""
1231
- Scan and return (nextcursor, pairs)
1283
+ Incrementally return lists of elements in a sorted set. Also return a
1284
+ cursor indicating the scan position.
1232
1285
1233
1286
``match`` allows for filtering the keys by pattern
1234
1287
@@ -1244,6 +1297,26 @@ def zscan(self, name, cursor=0, match=None, count=None,
1244
1297
options = {'score_cast_func' : score_cast_func }
1245
1298
return self .execute_command ('ZSCAN' , * pieces , ** options )
1246
1299
1300
+ def zscan_iter (self , name , match = None , count = None ,
1301
+ score_cast_func = float ):
1302
+ """
1303
+ Make an iterator using the ZSCAN command so that the client doesn't
1304
+ need to remember the cursor position.
1305
+
1306
+ ``match`` allows for filtering the keys by pattern
1307
+
1308
+ ``count`` allows for hint the minimum number of returns
1309
+
1310
+ ``score_cast_func`` a callable used to cast the score return value
1311
+ """
1312
+ cursor = 0
1313
+ while cursor != '0' :
1314
+ cursor , data = self .zscan (name , cursor = cursor , match = match ,
1315
+ count = count ,
1316
+ score_cast_func = score_cast_func )
1317
+ for item in data :
1318
+ yield item
1319
+
1247
1320
# SET COMMANDS
1248
1321
def sadd (self , name , * values ):
1249
1322
"Add ``value(s)`` to set ``name``"
0 commit comments