import functools
import time


def retry(exception_check, tries=3, delay=1, backoff=1):
    """Retry calling the decorated function using an exponential backoff.

    original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry

    :param exception_check: ``Exception || Tuple`` the exception to check.
                             may be a tuple of exceptions to check
    :param tries: ``int`` number of times to try (not retry) before giving up
    :param delay: ``int`` initial delay between retries in seconds
    :param backoff: ``int`` backoff multiplier e.g. value of 2 will double the
                    delay each retry
    """
    def deco_retry(f):
        @functools.wraps(f)
        def f_retry(*args, **kwargs):
            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return f(*args, **kwargs)
                except exception_check:
                    time.sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return f(*args, **kwargs)
        return f_retry  # true decorator
    return deco_retry