Retry

hivecore.decorator.retry(num_retries: int, exception_to_check: Exception = <class 'Exception'>, sleep_time: int | float = 0)

Decorator that retries the execution of a function if it raises a specific exception.

Parameters:
  • num_retries (int) – Number of times to retry running the function.

  • exception_to_check (Optional[Type[Exception]], optional) – Exception type to look for. Default: Exception.

  • sleep_time (Optional[Union[int, float]], optional) – Time to sleep between excecutions. Default: 0.

Returns:

The result from the function execution.

Return type:

Any

Example

from hivecore.decorator import retry

import random
@retry(num_retries=3)
def random_value():
    value = random.randint(1, 5)
    if value == 3:
        raise ValueError("Value cannot be 3")
    return value

random_value()