def retry(f, num=3, args=(), **kwargs):
    """
    Retry the given function `f` with the given arguments `num` times
    """

    try:
        return f(*args, **kwargs)
    except Exception as exception:
        for i in range(num):
            try:
                return f(*args, **kwargs)
            except Exception as e:
                exception = e
                print("Retrying '%s' (%s/%s) ..." % (f.__name__, i + 1, num))
        else:
            raise exception