-
Notifications
You must be signed in to change notification settings - Fork 3
/
Chunker.py
30 lines (25 loc) · 893 Bytes
/
Chunker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#! /usr/bin/python2
# vim: set fileencoding=utf-8
# from https://stackoverflow.com/a/1336821
class Chunker(object):
"""Split `iterable` on evenly sized chunks.
Leftovers are yielded at the end.
"""
def __init__(self, chunksize):
assert chunksize > 0
self.chunksize = chunksize
self.chunk = []
def __call__(self, iterable):
"""Yield items from `iterable` `self.chunksize` at the time."""
assert len(self.chunk) < self.chunksize
for item in iterable:
self.chunk.append(item)
if len(self.chunk) == self.chunksize:
yield self.chunk
self.chunk = []
if len(self.chunk) > 0:
yield self.chunk
if __name__ == '__main__':
chunker = Chunker(3)
res = [''.join(chunk) for chunk in chunker('abcdefghij')]
assert res == ['abc', 'def', 'ghi', 'j']