-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
94 lines (81 loc) · 3.5 KB
/
misc.py
File metadata and controls
94 lines (81 loc) · 3.5 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import functools
from time import time
import types
from typing import TypeVar, Callable
def time_me(func):
@functools.wraps(func)
def wrapper(*arg, **kwarg):
start_time = time()
result = func(*arg, **kwarg)
print(f'{func.__name__} took {time()-start_time} seconds')
return result
return wrapper
class Timer:
def __init__(self):
self.timers = dict()
def time(self, print_time: bool=True, key:int =0, msg: str = ''):
current_time = time()
if print_time:
last_time = self.timers.get(0, current_time)
if len(msg)>0:
print(msg)
print(f'{key}: {current_time-last_time} sec passed. ')
self.timers[key] = current_time
def print_progress_bar(iteration, total, prefix ='', suffix ='', decimals = 1, length = 100, fill ='█', printEnd =''):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
R = TypeVar('R')
def dict_param(func: Callable[..., R]) -> Callable[..., R]:
"""
Transforms a function which gets a dictionary as a single parameter, to be able to get instead named
parameters. For example, after decorating
@dict_param
def foo(d: dict):
...
the following calls will be the same:
foo(dict(a=1, b=2, c='3')) = foo(a=1, b=2, c='3')
It similarly works for functions of objects, namely:
foo(self, dict(a=1, b=2, c='3')) = foo(self, a=1, b=2, c='3')
"""
if isinstance(func, types.FunctionType):
@functools.wraps(func)
def wrapper(self, *arg, **index_values) -> R:
if len(arg) > 1:
raise Exception('should not have more than 1 argument')
if len(arg) == 1:
if type(arg[0]) != dict:
raise Exception('The single argument for this function must be a dictionary')
if len(index_values) > 0:
raise Exception('If these is a dictionary argument, you cannot pass more named arguments')
index_values = arg[0]
return func(self, index_values)
else:
@functools.wraps(func)
def wrapper(*arg, **index_values) -> R:
if len(arg) > 1:
raise Exception('should not have more than 1 argument')
if len(arg) == 1:
if type(arg[0]) != dict:
raise Exception('The single argument for this function must be a dictionary')
if len(index_values) > 0:
raise Exception('If these is a dictionary argument, you cannot pass more named arguments')
index_values = arg[0]
return func(index_values)
return wrapper