Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Callable, TypeVar
- from functools import wraps
- import time
- T = TypeVar("T")
- def retry(retries: int = 3, timeout: float = 1) -> Callable[[T], T]:
- # ваш код
- def take_in_func(func):
- @wraps(func)
- def wrapper(*args, **kwargs):
- mistake = -1
- for i in range(retries):
- try:
- res = func(*args, **kwargs)
- return res
- except Exception as exp:
- mistake = exp
- time.sleep(timeout)
- return mistake
- return wrapper
- return take_in_func
- def raiser_factory(stop_on: int = 2) -> Callable:
- call_counter = 0
- def raiser(*args, **kwargs) -> None:
- nonlocal call_counter
- if call_counter != 0 and call_counter % stop_on == 0:
- return
- call_counter += 1
- raise Exception
- return raiser
- # первый пример
- raiser = retry()(raiser_factory())
- raiser()
- # второй пример
- raiser = retry()(raiser_factory(stop_on=4))
- try:
- raiser()
- was_raised = False
- except Exception:
- was_raised = True
- assert was_raised
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement