def chop(seq, count):
    """Returns an iterator that traverses the given sequence, returning tuples
    of count items at a time.  The final tuple is padded with Nones if there
    aren't enough items in the sequence.
    """
    buf = []
    for elt in seq:
        buf.append(elt)
        if len(buf) == count:
            yield tuple(buf)
            buf[:] = []
    if buf:
        yield tuple(buf) + (None,) * (count - len(buf))
