Newer
Older
# -*- coding: UTF-8 -*-
# Python
Contains functions to facilitate code workflow, directory creation, Notebook readability, logfile creation, etc.
Jeremy Auclair
committed
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).
Jeremy Auclair
committed
Arguments
=========
timedelta: ``float``
time value in seconds to format
Jeremy Auclair
committed
Returns
=======
``None``
"""
if timedelta < 0.9e-6:

Jérémy AUCLAIR
committed
print(round(timedelta * 1e9, 1), 'ns')
elif timedelta < 0.9e-3:

Jérémy AUCLAIR
committed
print(round(timedelta * 1e6, 1), 'µs')
elif timedelta < 0.9:

Jérémy AUCLAIR
committed
print(round(timedelta * 1e3, 1), 'ms')
elif timedelta < 60:
print(round(timedelta, 1), 's')
elif timedelta < 3.6e3:

Jérémy AUCLAIR
committed
print(round(timedelta // 60), 'm', round(timedelta % 60, 1), 's')
elif timedelta < 24*3.6e3:

Jérémy AUCLAIR
committed
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:

Jérémy AUCLAIR
committed
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:

Jérémy AUCLAIR
committed
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
Jeremy Auclair
committed
def format_Byte_size(size: float, decimals: int = 3) -> Tuple[float, str]:
Format total memory size of calculation for print.
Jeremy Auclair
committed
Arguments
=========
1. size: ``float``
memory size of calculation in GiB
Jeremy Auclair
committed
2. decimals: ``int`` ``default = 3``
number of decimal digits to round to
Jeremy Auclair
committed
Returns
=======
1. size, unit: ``Tuple[float, str]``
formatted size for print
"""
if size < 1e-3:
Jeremy Auclair
committed
return round(size * (1024**2), decimals), 'kiB'
elif size < 1:
Jeremy Auclair
committed
return round(size * 1024, decimals), 'MiB'
else:
Jeremy Auclair
committed
return round(size, decimals), 'GiB'