-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
32 lines (26 loc) · 1.07 KB
/
utils.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
import sys
import time
from itertools import cycle
from pathlib import Path
from typing import NewType, Optional
from loguru import logger
Success = NewType("Success", bool)
def setup_logger(script_name: str, level: str):
level = level.upper()
logger.remove() #remove the old handler. Else, the old one will work along with the new one you've added below'
logger.add(sys.stdout, level=level)
log_filename = f"{script_name}__{str(time.time()).replace('.', 'f')}.log"
log_filepath = Path("logs") / log_filename
log_filepath.parent.mkdir(exist_ok=True)
logger.add(log_filepath, level=level)
def channel_generator(min_: int = 1, max_: int = 8, start: Optional[int] = None, step: int = 1):
channels = list(range(min_, max_ + 1))
if start is not None:
assert 1 <= start <= 8, f"Start channel must be between 1 and 8, '{start}' given."
for ch in channels[start - 1::step]:
yield ch
channels_cycle = cycle(channels)
while True:
yield next(channels_cycle)
for _ in range(step - 1):
next(channels_cycle)