forked from adamtiger/LHYP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
48 lines (41 loc) · 1.65 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import logging
import time
import functools
def get_logger(name):
"""
Returns an already configured logger for a specific module.
(This should be used instead of stdout.)
:param name: the name of the modeule where the logger is created
:return: a custom configured logger object
"""
logger = logging.getLogger(name)
logger.setLevel(logging.INFO)
handler = logging.FileHandler("hypertrophy.log", mode='a')
formatter = logging.Formatter('%(levelname)s - %(asctime)s - %(name)s -- %(msg)s')
handler.setFormatter(formatter)
handler.setLevel(logging.INFO)
logger.addHandler(handler)
return logger
def process_time(logger):
"""
Decorator for measuring the ellapsed time for a process.
The result is logged.
"""
def decorator_wrapper(func):
@functools.wraps(func)
def wrapper_process_time(*args, **kwargs):
logger.info("Process {} STARTED.".format(func.__name__))
start_time = time.perf_counter()
value = func(*args, **kwargs)
end_time = time.perf_counter()
logger.info("Process {0} FINISHED. Ellapsed time: {1:.4f}".format(func.__name__, end_time - start_time))
return value
return wrapper_process_time
return decorator_wrapper
def progress_bar(current, total, bins):
freq = total / bins
bar = "#" * int(current / freq) + " " * (bins - int(current / freq))
print("\rLoading [{}] {} %".format(bar, int(current/total * 100.0)), end="", flush=True)
#print("\rLoading [{}] {} %".format(bar, int(current / total * 100.0)), flush=True)
if current == total:
print("\nLoading finsihed\n")