def chunk(sequence, chunk_size):
    """ Split an iterable into batches of items.
    """
    c = []
    for item in sequence:
        c.append(item)
        if len(c) >= chunk_size:
            yield c
            c = []
    if c:
        yield c
