<s> import time <EOL> class RetryFailed ( RuntimeError ) : <EOL> pass <EOL> class TooManyRetries ( RuntimeError ) : <EOL> pass <EOL> def retry ( max_tries , delay = <NUM_LIT:3> , backoff = <NUM_LIT:2> , sleep_func = None ) : <EOL> """<STR_LIT>""" <EOL> if backoff <= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if max_tries < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if delay <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if sleep_func is None : <EOL> sleep_func = time . sleep <EOL> def decorator ( f ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> current_delay = delay <EOL> current_try = max_tries <EOL> while current_try > <NUM_LIT:0> : <EOL> current_try -= <NUM_LIT:1> <EOL> for current_try in range ( max_tries ) : <EOL> try : <EOL> return f ( * args , ** kwargs ) <EOL> except RetryFailed : <EOL> if current_try < max_tries - <NUM_LIT:1> : <EOL> sleep_func ( current_delay ) <EOL> current_delay *= backoff <EOL> raise TooManyRetries ( ) <EOL> return wrapper <EOL> return decorator </s>
