import functools
import time


def retry(n, interval=0.1):
    """Retry a function or method n times before raising an exception.

    :param n: Number of times to retry
    :param interval: Time to sleep before attempts
    """
    def decorate(fn):
        @functools.wraps(fn)
        def attempt(*args, **kwargs):
            attempts = 0
            while True:
                try:
                    return fn(*args, **kwargs)
                except Exception:
                    attempts += 1
                    if n <= attempts:
                        raise
                    time.sleep(interval)

        return attempt

    return decorate