-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
50 lines (36 loc) · 1.26 KB
/
Copy pathcommon.py
File metadata and controls
50 lines (36 loc) · 1.26 KB
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
import sys
from datetime import datetime
from time import sleep
def flat_map(f, xs):
ys = []
for x in xs:
ys.extend([f(el) for el in x])
return ys
def generate_months(start_date: datetime, end_date: datetime):
year = start_date.year
month = start_date.month
end_year = end_date.year
end_month = end_date.month
while True:
yield year, month
if year == end_year and month == end_month:
return
month = month % 12 + 1
year = year if month != 1 else year + 1
def retry(func=None, /, *, retries=5, delay=1.0):
def inner(func):
def wrapper(*args, **kwargs):
retry_count = 0
while True:
# noinspection PyBroadException
try:
return func(*args, **kwargs)
except Exception as e:
retry_count += 1
print(f"Retrying {func.__name__} because of {e.__class__.__name__}: {retry_count}...", file=sys.stderr)
if retry_count >= retries:
raise e
sleep(delay)
return wrapper
# so that the decorator can be used with or without params
return inner if func is None else inner(func)