avoids copying entire result into a new list

This commit is contained in:
George Alton 2018-10-28 11:00:21 +00:00
parent aa4be6fcad
commit 9ba28a05b8
No known key found for this signature in database
GPG Key ID: BF2E281ABF61709E

View File

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import datetime import datetime
import functools import functools
import itertools
import json import json
import os import os
import time import time
@ -43,16 +44,12 @@ def paginate(limit, start_arg="next_token", limit_arg="max_results"):
def outer_wrapper(func): def outer_wrapper(func):
@functools.wraps(func) @functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
# setup
start = int(default_start if kwargs.get(start_arg) is None else kwargs[start_arg]) start = int(default_start if kwargs.get(start_arg) is None else kwargs[start_arg])
stop = int(limit if kwargs.get(limit_arg) is None else kwargs[limit_arg]) lim = int(limit if kwargs.get(limit_arg) is None else kwargs[limit_arg])
end = start + stop stop = start + lim
# call
result = func(*args, **kwargs) result = func(*args, **kwargs)
# modify limited_results = list(itertools.islice(result, start, stop))
results = list(result) next_token = stop if stop < len(result) else None
limited_results = results[start: end]
next_token = end if end < len(results) else None
return limited_results, next_token return limited_results, next_token
return wrapper return wrapper
return outer_wrapper return outer_wrapper