def timeleft(i, num, elapsed):
    """Returns the time left, in secs, of a given operation.
    Useful for loops, where `i` is the current iteration,
    `num` is the total number of iterations, and `elapsed`
    is the time elapsed since starting.
    """
    try:
        rate = i/float(elapsed)
        left = (num-i)/rate
    except ZeroDivisionError: return 0
    return left