81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
import os
|
|
import sys
|
|
import math
|
|
import time
|
|
from collections import OrderedDict
|
|
|
|
|
|
def write_to_record_file(data, file_path, verbose=True):
|
|
if verbose:
|
|
print(data)
|
|
record_file = open(file_path, 'a')
|
|
record_file.write(data+'\n')
|
|
record_file.close()
|
|
|
|
|
|
def asMinutes(s):
|
|
m = math.floor(s / 60)
|
|
s -= m * 60
|
|
return '%dm %ds' % (m, s)
|
|
|
|
def timeSince(since, percent):
|
|
now = time.time()
|
|
s = now - since
|
|
es = s / (percent)
|
|
rs = es - s
|
|
return '%s (- %s)' % (asMinutes(s), asMinutes(rs))
|
|
|
|
class Timer:
|
|
def __init__(self):
|
|
self.cul = OrderedDict()
|
|
self.start = {}
|
|
self.iter = 0
|
|
|
|
def reset(self):
|
|
self.cul = OrderedDict()
|
|
self.start = {}
|
|
self.iter = 0
|
|
|
|
def tic(self, key):
|
|
self.start[key] = time.time()
|
|
|
|
def toc(self, key):
|
|
delta = time.time() - self.start[key]
|
|
if key not in self.cul:
|
|
self.cul[key] = delta
|
|
else:
|
|
self.cul[key] += delta
|
|
|
|
def step(self):
|
|
self.iter += 1
|
|
|
|
def show(self):
|
|
total = sum(self.cul.values())
|
|
for key in self.cul:
|
|
print("%s, total time %0.2f, avg time %0.2f, part of %0.2f" %
|
|
(key, self.cul[key], self.cul[key]*1./self.iter, self.cul[key]*1./total))
|
|
print(total / self.iter)
|
|
|
|
|
|
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
|
|
"""
|
|
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)
|
|
bar_length - Optional : character length of bar (Int)
|
|
"""
|
|
str_format = "{0:." + str(decimals) + "f}"
|
|
percents = str_format.format(100 * (iteration / float(total)))
|
|
filled_length = int(round(bar_length * iteration / float(total)))
|
|
bar = '█' * filled_length + '-' * (bar_length - filled_length)
|
|
|
|
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
|
|
|
|
if iteration == total:
|
|
sys.stdout.write('\n')
|
|
sys.stdout.flush()
|