def chunked(iterable, size):
    """Yields chunks of the requested size from the iterable.  The final chunk
    may be smaller than size"""
    if not size:
        for item in iterable:
            yield item
        return
    chunk = []
    for i, item in enumerate(iterable):
        chunk.append(item)
        if len(chunk) == size:
            yield chunk
            chunk = []
    if chunk:
        yield chunk
