log_time.py 585 B

123456789101112131415161718192021
  1. from time import time
  2. async def log_time_async(method: callable, **kwargs):
  3. start = time()
  4. result = await method(**kwargs)
  5. secs = f"{round(time() - start, 2)} secs"
  6. return " ".join([result, secs]) if result else secs
  7. def log_time_yield(method: callable, **kwargs):
  8. start = time()
  9. result = yield from method(**kwargs)
  10. yield f" {round(time() - start, 2)} secs"
  11. def log_time(method: callable, **kwargs):
  12. start = time()
  13. result = method(**kwargs)
  14. secs = f"{round(time() - start, 2)} secs"
  15. return " ".join([result, secs]) if result else secs