File size: 1,607 Bytes
5631464 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | """Tools related to timing functions."""
from contextlib import contextmanager
from time import time
from typing import Generator
from haddock import log
@contextmanager
def log_time(pre_msg: str) -> Generator[None, None, None]:
"""
Log the time taken to execute the code under the context.
Examples
--------
>>> with log_time("function took"):
do_something()
do_more()
logged: "function tooked 10 minutes"
Parameters
----------
pre_msg : str
String to log with appended time.
See Also
--------
:py:func:`convert_seconds_to_min_sec`
"""
start = time()
yield
end = time()
elapsed = convert_seconds_to_min_sec(end - start)
log.info(f"{pre_msg} {elapsed}")
def convert_seconds_to_min_sec(seconds: float) -> str:
"""
Convert seconds to min&sec.
Examples
--------
>>> convert_seconds_to_min_sec(60)
1 minute
>>> convert_seconds_to_min_sec(120)
2 minutes and 0 seconds
>>> convert_seconds_to_min_sec(179)
2 minutes and 59 seconds
Parameters
----------
seconds : int
The elapsed time in seconds. Seconds are round to integers.
Returns
-------
str
"""
seconds = int(round(seconds, 0))
hours = seconds // 3600
minutes = (seconds) // 60 % 60
seconds = (seconds) % 60
if hours:
return f"{hours}h{minutes}m{seconds}s"
if minutes:
s = "" if minutes == 1 else "s"
return f"{minutes} minute{s} and {seconds} seconds"
s = "" if seconds == 1 else "s"
return f"{seconds} seconds"
|