FileToLink / Thunder /utils /human_readable.py
Fyaz Mohammed
1.9.5
8359204 unverified
Raw
History Blame Contribute Delete
563 Bytes
# Thunder/utils/human_readable.py
from Thunder.utils.logger import logger
_UNITS = ('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
def humanbytes(size: int, decimal_places: int = 2) -> str:
try:
if not size:
return "0 B"
n = 0
while size >= 1024 and n < len(_UNITS) - 1:
size /= 1024
n += 1
return f"{round(size, decimal_places)} {_UNITS[n]}B"
except Exception as e:
logger.error(f"Error in humanbytes for size {size}: {e}", exc_info=True)
return "N/A"