import functools
import time


def thrice(delay=0.5):
    """This decorator tries failed operations 3 times before it gives up

    The delay determines how long to wait between tries (in seconds)
    """

    def decorated(f):
        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            for i in range(3):
                try:
                    return f(*args, **kwargs)
                except Exception:
                    if i == 2:
                        raise
                    time.sleep(delay)

        return wrapped

    return decorated