In [3]:
1
import time
Print Progressive Bar in python
In [2]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Print iterations progress
def printProgressBar(iteration, total, msg, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█'):
"""
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)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
# print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
show = '\r{}|{}| {} {}% - {}'.format(prefix, bar, percent, suffix, msg)
print(show, end='\r')
In [5]:
1
2
3
4
num_step = 5000
for i in range(num_step):
time.sleep(0.01)
printProgressBar(iteration=i + 1, total=num_step, msg='iteration', length=50)
1
|██████████████████████████████████████████████████| 100.0 % - iteration
logging time
Using decorator, we can implement logging time function.
In [6]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
def logging_time(original_fn):
def wrapper_fn(*args, **kwargs):
verbose = False
if 'verbose' in kwargs:
verbose = True
del kwargs['verbose']
start_time = time.time()
result = original_fn(*args, **kwargs)
elapsed_time = (time.time() - start_time) * 1e3
if verbose:
print("WorkingTime[{}]: {:.5f} ms".format(original_fn.__name__, elapsed_time))
return result
return result, elapsed_time
return wrapper_fn
In [10]:
1
2
3
4
5
@logging_time
def func():
for i in range(num_step):
time.sleep(0.001)
printProgressBar(iteration=i + 1, total=num_step, msg='iteration', length=50)
In [11]:
1
func(verbose=True)
1
2
WorkingTime[func]: 6094.05684 ms███████████████████| 100.0 % - iteration
Leave a comment