
Hi On Sun, Dec 8, 2013 at 8:24 PM, Mark Lawrence <breamoreboy@yahoo.co.uk>wrote:
On 08/12/2013 04:44, Amber Yust wrote:
After seeing yet another person asking how to do this on #python (and having needed to do it in the past myself), I'm wondering why itertools doesn't have a function to break an iterator up into N-sized chunks.
As discussed umpteen times previously, there is no way that we can agree on "a function" that can meet all of the variations that have been proposed on this theme.
Maybe if we add this function:
def mod_pad(it, modulo, fillval): '"".join(mod_pad("hello", 3, fillval="!")) => hello!' for i, val in enumerate(iter(it)): yield val for _ in range(i%modulo): yield fillval
"".join(mod_pad("hello", 3, fillval="!")) 'hello!'
Then we can make grouper/batcher throw an exception in the case of iter_len % modulo != 0 grouper(mod_pad("hello", 3, fillval="!"), 3) => hel, lo! (in a iterator...) grouper('hello', 3) => BOOM