[Updated version thanks to the OP: I've been throwing yield from
at everything in sight since I upgraded and it didn't even occur to me that I didn't need it here.]
Oh, what the heck:
from itertools import takewhile, islice, count
def chunk(stream, size):
return takewhile(bool, (list(islice(stream, size)) for _ in count()))
which gives:
>>> list(chunk((i for i in range(3)), 3))
[[0, 1, 2]]
>>> list(chunk((i for i in range(6)), 3))
[[0, 1, 2], [3, 4, 5]]
>>> list(chunk((i for i in range(8)), 3))
[[0, 1, 2], [3, 4, 5], [6, 7]]
Warning: the above suffers the same problem as the OP's chunk_input_stream
if the input is a list. You could get around this with an extra iter()
wrap but that's less pretty. Conceptually, using repeat
or cycle
might make more sense than count()
but I was character-counting for some reason. :^)
[FTR: no, I'm still not entirely serious about this, but hey-- it's a Monday.]