def retries(max_tries, delay=1, backoff=2, exceptions=(Exception,), hook=None):
 def dec(func):
  def f2(*args, **kwargs):
   mydelay = delay
   tries = reversed(range(max_tries))
   for tries_remaining in tries:
    try:
     return func(*args, **kwargs)
    except exceptions as e:
     if (tries_remaining > 0):
      if (hook is not None):
       hook(tries_remaining, e, mydelay)
      sleep(mydelay)
      mydelay = (mydelay * backoff)
     else:
      raise
    else:
     break
  return f2
 return dec

def retries ( max_tries , delay = 1 , backoff = 2 , exceptions = ( Exception , ) , hook = None ) : def dec ( func ) : def f2 ( * args , ** kwargs ) : mydelay = delay tries = reversed ( range ( max_tries ) ) for tries_remaining in tries : try : return func ( * args , ** kwargs ) except exceptions as e : if ( tries_remaining > 0 ) : if ( hook is not None ) : hook ( tries_remaining , e , mydelay ) sleep ( mydelay ) mydelay = ( mydelay * backoff ) else : raise else : break return f2 return dec
function decorator implementing retrying logic .