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.
- example:
- >>> a = list(chunker(range(100), 10))
- >>> print(a)
- '''
- def get_chunk():
- for n, element in enumerate(iterator, start=1):
- yield element
- if n == chunksize:
- break
- iterator = iter(iterable)
- while True:
- chunk = return_type(get_chunk())
- if not chunk:
- break
- yield chunk
Add Comment
Please, Sign In to add comment