def read_chunks(reader, chunksize, limit=None):
    """Given an iterator/generator, read/yield chunks of chunksize up to
    (optional) limit"""
    chunk = []
    for i, line in enumerate(reader):
        if limit and i >= limit:
            break
        if i % chunksize == 0 and i > 0:
            yield chunk
            chunk = []
        chunk.append(line)
    yield chunk
