chunker for iterables
DeaD_EyE Oct 31st, 2018 25 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- def chunker(iterable, chunksize, return_type=tuple):
- A generator, which yields chunks with given chunksize
- as return_type tuple.
- iterable := any object which is iterable
- chunksize := chunksize of each chunk
- return_type := returns the chunks as given type.
- >>> a = list(chunker(range(100), 10))
- >>> print(a)
- def get_chunk():
- for n, element in enumerate(iterator, start=1):
- yield element
- if n == chunksize:
- iterator = iter(iterable)
- while True:
- chunk = return_type(get_chunk())
- if not chunk:
- yield chunk
RAW Paste Data