from datetime import datetime from rich.console import Console console = Console() timers_started_ats = dict() def start_timer(name=1, do_print=True): started_at = datetime.now() timers_started_ats[name] = started_at if do_print: console.print(f"{name:<{len(str(name)) + 4}} ", end="") print(f"started at: {started_at.strftime('%H:%M:%S.%f')}") return name, started_at def stop_timer(name=1, do_print=True): ended_at = datetime.now() took = ended_at - timers_started_ats[name] if do_print: console.print(f"{name:<{len(str(name)) + 4}} ", end="") print(f"{'ended at:':>11} {ended_at.strftime('%H:%M:%S.%f')} ", end="") console.print(f"took: {took}") return name, ended_at, took if __name__ == "__main__": from time import sleep start_timer() sleep(1) stop_timer()