def batch(enumerable, batch_size):
batch_size = max(int(batch_size), <NUM_LIT:1>)<EOL>try:<EOL><INDENT>enumerable.__getitem__<EOL>total_size = len(enumerable)<EOL><DEDENT>except (TypeError, AttributeError):<EOL><INDENT>enumerable = list(enumerable)<EOL>total_size = len(enumerable)<EOL><DEDENT>if total_size == <NUM_LIT:0>:<EOL><INDENT>yield tuple()<EOL><DEDENT>try:<EOL><INDENT>for batch_index in xrange(<NUM_LIT:0>, total_size, batch_size):<EOL><INDENT>yield enumerable[batch_index:min(batch_index + batch_size, total_size)]<EOL><DEDENT><DEDENT>except TypeError:<EOL><INDENT>for batch_start in xrange(<NUM_LIT:0>, total_size, batch_size):<EOL><INDENT>yield tuple(islice(enumerable, batch_start, min(batch_start + batch_size, total_size)))<EOL><DEDENT><DEDENT>
Breaks enumerable argument into batch size enumerable pieces. The last chunk can
be of any length up to batch_size.

batch(xrange(5), 3)
# => [0, 1, 2], [3, 4]