-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeat.py
55 lines (50 loc) · 1.55 KB
/
repeat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import Callable
import functools
import logging
import time
import traceback
logger = logging.getLogger()
def repeat(
_func: Callable =None,
*,
num_times: int =3,
time_sleep: int =5
) -> Callable:
"""
Try repeat the function num_times until success
Parameters
----------
_func : function, optional
Wrapped function (arguments allowed), by default None
num_times : int, optional
Times to repeat the wrapped function, by default 3
time_sleep : int, optional
Seconds to wait between repetitions, by default 5
Returns
-------
function
Wrapped function
"""
"""Repeat the function num_times"""
def decorator_repeat(func):
@functools.wraps(func)
def wrapper_repeat(*args, **kwargs):
for _ in range(num_times):
try:
value = func(*args, **kwargs)
return value
except:
logger.error(
'Error in {function_name}. Trying again in {time_sleep}'
'second(s). Traceback: {error_traceback}'.format(
function_name=func.__name__,
time_sleep=time_sleep,
error_traceback=traceback.format_exc()
)
)
time.sleep(time_sleep)
return wrapper_repeat
if _func is None:
return decorator_repeat
else:
return decorator_repeat(_func)