From 353c380762f6855d38648ede5100ac453ebca468 Mon Sep 17 00:00:00 2001 From: immerrr Date: Fri, 3 Jun 2016 20:37:40 +0300 Subject: [PATCH] iterutils: optimize chunked_iter --- boltons/iterutils.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/boltons/iterutils.py b/boltons/iterutils.py index 6cc5eb5..3037896 100644 --- a/boltons/iterutils.py +++ b/boltons/iterutils.py @@ -218,17 +218,13 @@ def chunked_iter(src, size, **kw): postprocess = lambda chk: chk if isinstance(src, basestring): postprocess = lambda chk, _sep=type(src)(): _sep.join(chk) - cur_chunk = [] - i = 0 - for item in src: - cur_chunk.append(item) - i += 1 - if i % size == 0: - yield postprocess(cur_chunk) - cur_chunk = [] - if cur_chunk: - if do_fill: - lc = len(cur_chunk) + src_iter = iter(src) + while True: + cur_chunk = list(itertools.islice(src_iter, size)) + if not cur_chunk: + break + lc = len(cur_chunk) + if lc < size and do_fill: cur_chunk[lc:] = [fill_val] * (size - lc) yield postprocess(cur_chunk) return