code: def retries(max_tries, delay=1, backoff=2, exceptions=(Exception,), hook=None):
      
  
      """
      def example_hook(tries_remaining, exception, delay):
          '''Example exception handler; prints a warning to stderr.
  
          tries_remaining: The number of tries remaining.
          exception: The exception instance which was raised.
          '''
          print >> sys.stderr, "Caught '%s', %d tries remaining, sleeping for %s seconds" % (exception, tries_remaining, delay)
  
      The decorator will call the function up to max_tries times if it raises
      an exception.
  
      By default it catches instances of the Exception class and subclasses.
      This will recover after all but the most fatal errors. You may specify a
      custom tuple of exception classes with the 'exceptions' argument; the
      function will only be retried if it raises one of the specified
      exceptions.
  
      Additionally you may specify a hook function which will be called prior
      to retrying with the number of remaining tries and the exception instance;
      see given example. This is primarily intended to give the opportunity to
      log the failure. Hook is not called after failure if no retries remain.
      """
      def dec(func):
          def f2(*args, **kwargs):
              mydelay = delay
              #tries = range(max_tries)
              #tries.reverse()
              tries = range(max_tries-1, -1, -1)
              for tries_remaining in tries:
                  try:
                      return func(*args, **kwargs)
                  except GracefulExit:
                      raise
                  except exceptions as e:
                      if tries_remaining > 0:
                          if hook is not None:
                              hook(tries_remaining, e, mydelay)
                          sleep(mydelay)
                          mydelay *= backoff
                      else:
                          raise
          return f2
      return dec
docs: Function decorator implementing retrying logic.
  
      delay: Sleep this many seconds * backoff * try number after failure
      backoff: Multiply delay by this factor after each failure
      exceptions: A tuple of exception classes; default (Exception,)
      hook: A function with the signature: (tries_remaining, exception, mydelay)