Browse Source

implement logging range

devel
Stefan Holst 4 years ago
parent
commit
4f531fe4cb
  1. 29
      src/kyupy/__init__.py

29
src/kyupy/__init__.py

@ -29,6 +29,19 @@ def hr_bytes(nbytes): @@ -29,6 +29,19 @@ def hr_bytes(nbytes):
return f'{nbytes:.1f}{["", "ki", "Mi", "Gi", "Ti", "Pi"][multiplier]}B'
def hr_time(seconds):
s = ''
if seconds >= 3600:
h = seconds // 3600
seconds -= h * 3600
s += f'{h}h'
if seconds >= 60 or len(s) > 0:
m = seconds // 60
seconds -= m * 60
s += f'{m}m'
return f'{s}{int(seconds)}s'
class Log:
def __init__(self):
self.start = time.perf_counter()
@ -48,6 +61,22 @@ class Log: @@ -48,6 +61,22 @@ class Log:
def error(self, message): self.log('E', message)
def range(self, *args):
elems = len(range(*args))
start_time = time.perf_counter()
lastlog_time = start_time
log_interval = 5
for elem, i in enumerate(range(*args)):
yield i
current_time = time.perf_counter()
if current_time > lastlog_time + log_interval:
work_done = elem / elems
elapsed_time = current_time - start_time
total_time = elapsed_time / work_done
remaining_time = total_time - elapsed_time
self.log(':', f'{work_done*100:.0f}% done {hr_time(elapsed_time)} elapsed {hr_time(remaining_time)} remaining')
log_interval = min(600, int(log_interval*1.5))
lastlog_time = current_time
log = Log()

Loading…
Cancel
Save