diff --git a/utils/base.py b/utils/base.py index 1884d26..9a5597a 100644 --- a/utils/base.py +++ b/utils/base.py @@ -65,40 +65,47 @@ def printf(info, color='green', bold=False, underline=False, flash=False): print(output_formatter(info, color=color, bold=bold, underline=underline, flash=flash)) +def time_formatter(t: float) -> str: + """format the time""" + if t > 60 * 60: return f"{int(t / (60 * 60))}h " + time_formatter(t % (60 * 60)) + elif t > 60: return f"{int(t / 60)}m " + time_formatter(t % 60) + else: return f"{int(t)}s" + + def process_bar(cidx=[0]): - def wrapper(total, processed, found=0, timer=False, start_time=0): + def wrapper(total, done, found=0, timer=False, start_time=0): """since tqdm cant be used when we use mutiprocess""" # icon icon_list = '⇐⇖⇑⇗⇒⇘⇓⇙' - _icon = output_formatter(icon_list[cidx[0]], color='green', bold=True) + icon = output_formatter(icon_list[cidx[0]], color='green', bold=True) cidx[0] = cidx[0] + 1 if cidx[0] < len(icon_list) - 1 else 0 - _icon = f"[{_icon}]" + icon = f"[{icon}]" # time if timer and start_time != 0: - time_used = round(time.time() - start_time, 1) - if processed == 0: # avoid the devision number is zero - processed = processed + 1 - time_pred = round((time_used / (processed)) * total, 1) - time_used = output_formatter(time_used, color='white', bold=True) - time_pred = output_formatter(time_pred, color='white', bold=True) - _time = f"time: {time_used}s/{time_pred}s" + time_used = time.time() - start_time + done = done + 1 if done == 0 else done # avoid the devision number is zero + time_pred = time_used * (total / done) + time_used = output_formatter(time_formatter(time_used), color='cyan', bold=True) + time_pred = output_formatter(time_formatter(time_pred), color='white', bold=True) + _time = f"time: {time_used}/{time_pred}" # count _total = output_formatter(total, color='blue', bold=True) - _processed = output_formatter(processed, color='blue', bold=True) + _done = output_formatter(done, color='blue', bold=True) + _percent = output_formatter(f"{round(done / total * 100, 1)}%", color='pink', bold=True) _found = 'Found ' + output_formatter(found, color='red', bold=True) if found else '' - _count = f"{_processed}/{_total} {_found}" + count = f"{_done}/{_total} ({_percent}) {_found}" - print(f"\r{_icon} {_count} {_time}", end='') + print(f"\r{icon} {count} {_time}", end='') return wrapper if __name__ == '__main__': - t0 = time.time() - found = 0 + found, t0 = 0, time.time() bar = process_bar() - for i in range(1, 101): + for i in range(1, 10001): + time.sleep(.05) found += 1 if not i % 10 else 0 - bar(100, i, found, timer=True, start_time=t0) - time.sleep(.1) + bar(10000, i, found, timer=True, start_time=t0) + print()