decorators

Decorators module.

spicerack.decorators.ensure_wrap(func)[source]

Decorator to wrap other decorators to allow to call them both with and without arguments.

Parameters:func -- the decorated function, it must be a decorator. A decorator that accepts only one positional argument that is also a callable is not supported.
spicerack.decorators.get_backoff_sleep(backoff_mode, base, index)[source]

Calculate the amount of sleep for this attempt.

Parameters:
  • backoff_mode (str) -- the backoff mode to use for the delay, see the documentation for retry().
  • base (int, float) -- the base for the backoff algorithm.
  • index (int) -- the index to calculate the Nth sleep time for the backoff.
Returns:

the amount of sleep to perform for the backoff.

Return type:

int, float

spicerack.decorators.retry(func=None, tries=3, delay=datetime.timedelta(0, 3), backoff_mode='exponential', exceptions=(<class 'spicerack.exceptions.SpicerackError'>, ))[source]

Decorator to retry a function or method if it raises certain exceptions with customizable backoff.

Note

The decorated function or method must be idempotent to avoid unwanted side effects. It can be called with or without arguments, in the latter case all the default values will be used.

Parameters:
  • func (function, method) -- the decorated function.
  • tries (int, optional) -- the number of times to try calling the decorated function or method before giving up. Must be a positive integer.
  • delay (datetime.timedelta, optional) -- the initial delay for the first retry, used also as the base for the backoff algorithm.
  • backoff_mode (str, optional) -- the backoff mode to use for the delay, available values are: constant: delay => 3, 3, 3, 3, 3, ...; linear: delay * N => 3, 6, 9, 12, 15, ...; N in [1, tries] power: delay * 2^N => 3, 6, 12, 24, 48, ...; N in [0, tries - 1] exponential: delay^N => 3, 9, 27, 81, 243, ...; N in [1, tries], delay must be > 1.
  • exceptions (type, tuple, optional) -- the decorated function call will be retried if it fails until it succeeds or tries attempts are reached. A retryable failure is defined as raising any of the exceptions listed.
Returns:

the decorated function.

Return type:

function