Skip to content
Snippets Groups Projects
code_toolbox.py 2.25 KiB
Newer Older
Contains functions to facilitate code workflow, directory creation, Notebook readability, logfile creation, etc.
23-08-2023
@author: jeremy auclair
"""

from typing import Tuple  # to declare argument types


def format_duration(timedelta: float) -> None:
    Print formatted timedelta in human readable format
    (days, hours, minutes, seconds, microseconds, milliseconds, nanoseconds).

    """

    if timedelta < 0.9e-6:
        print(round(timedelta*1e9, 1), 'ns')
    elif timedelta < 0.9e-3:
        print(round(timedelta*1e6, 1), 'µs')
    elif timedelta < 0.9:
        print(round(timedelta*1e3, 1), 'ms')
    elif timedelta < 60:
        print(round(timedelta, 1), 's')
    elif timedelta < 3.6e3:
        print(round(timedelta//60), 'm', round(timedelta % 60, 1),  's')
    elif timedelta < 24*3.6e3:
        print(round((timedelta/3.6e3)//1), 'h', round((timedelta/3.6e3) % 1*60//1), 'm', round((timedelta/3.6e3) % 1*60 % 1*60, 1), 's')
    elif timedelta < 48*3.6e3:
        print(round((timedelta/(24*3.6e3))//1), 'day,', round(((timedelta/(24*3.6e3)) % 1*24)//1), 'h,',
              round((timedelta/(24*3.6e3)) % 1*24 % 1*60//1), 'm,',  round((timedelta/(24*3.6e3)) % 1*24 % 1*60 % 1*60, 1), 's')
    else:
        print(round((timedelta/(24*3.6e3))//1), 'days,', round(((timedelta/(24*3.6e3)) % 1*24)//1), 'h,',
              round((timedelta/(24*3.6e3)) % 1*24 % 1*60//1), 'm,',  round((timedelta/(24*3.6e3)) % 1*24 % 1*60 % 1*60, 1), 's')

    return None


def format_Byte_size(size: float, decimals: int = 3) -> Tuple[float, str]:
    Format total memory size of calculation for print.

    2. decimals: ``int`` ``default = 3``
        number of decimal digits to round to
        return round(size * (1024**2), decimals), 'kiB'
        return round(size * 1024, decimals), 'MiB'