pypet/utils/decorators.py
def retry(n, errors, wait=0.0, logger_name=None):
    """This is a decorator that retries a function.

    Tries `n` times and catches a given tuple of `errors`.

    If the `n` retries are not enough, the error is reraised.

    If desired `waits` some seconds.

    Optionally takes a 'logger_name' of a given logger to print the caught error.

    """

    def wrapper(func):
        @functools.wraps(func)
        def new_func(*args, **kwargs):
            retries = 0
            while True:
                try:
                    result = func(*args, **kwargs)
                    if retries and logger_name:
                        logger = logging.getLogger(logger_name)
                        logger.debug('Retry of `%s` successful' % func.__name__)
                    return result
                except errors:
                    if retries >= n:
                        if logger_name:
                            logger = logging.getLogger(logger_name)
                            logger.exception('I could not execute `%s` with args %s and kwargs %s, '
                                             'starting next try. ' % (func.__name__,
                                                                      str(args),
                                                                      str(kwargs)))
                        raise
                    elif logger_name:
                        logger = logging.getLogger(logger_name)
                        logger.debug('I could not execute `%s` with args %s and kwargs %s, '
                                         'starting next try. ' % (func.__name__,
                                                                  str(args),
                                                                  str(kwargs)))
                    retries += 1
                    if wait:
                        time.sleep(wait)
        return new_func

    return wrapper
def retry(n, errors, wait=0.0, logger_name=None):
    """This is a decorator that retries a function.

    Tries `n` times and catches a given tuple of `errors`.

    If the `n` retries are not enough, the error is reraised.

    If desired `waits` some seconds.

    Optionally takes a 'logger_name' of a given logger to print the caught error.

    """

    def wrapper(func):
        @functools.wraps(func)
        def new_func(*args, **kwargs):
            retries = 0
            while True:
                try:
                    result = func(*args, **kwargs)
                    if retries and logger_name:
                        logger = logging.getLogger(logger_name)
                        logger.debug('Retry of `%s` successful' % func.__name__)
                    return result
                except errors:
                    if retries >= n:
                        if logger_name:
                            logger = logging.getLogger(logger_name)
                            logger.exception('I could not execute `%s` with args %s and kwargs %s, '
                                             'starting next try. ' % (func.__name__,
                                                                      str(args),
                                                                      str(kwargs)))
                        raise
                    elif logger_name:
                        logger = logging.getLogger(logger_name)
                        logger.debug('I could not execute `%s` with args %s and kwargs %s, '
                                         'starting next try. ' % (func.__name__,
                                                                  str(args),
                                                                  str(kwargs)))
                    retries += 1
                    if wait:
                        time.sleep(wait)
        return new_func

    return wrapper
This is a decorator that retries a function.

    Tries `n` times and catches a given tuple of `errors`.

    If the `n` retries are not enough, the error is reraised.

    If desired `waits` some seconds.

    Optionally takes a 'logger_name' of a given logger to print the caught error.