Added an utils module with a function to convert sizes in bytes to human readable strings

This commit is contained in:
Manuel Cortez 2022-02-18 12:47:50 -06:00
parent e4c953e5b6
commit ec214bc387
No known key found for this signature in database
GPG Key ID: 9E0735CA15EFE790

22
updater/utils.py Normal file
View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
def convert_bytes(n: float) -> str:
""" Converts a value expressed in bytes to a human readable String.
:param n: Size in bytes.
:returns: A string formatted in human readable format.
:rtype: str.
"""
K, M, G, T, P = 1 << 10, 1 << 20, 1 << 30, 1 << 40, 1 << 50
if n >= P:
return "%.2fPb" % (float(n) / T)
elif n >= T:
return "%.2fTb" % (float(n) / T)
elif n >= G:
return "%.2fGb" % (float(n) / G)
elif n >= M:
return "%.2fMb" % (float(n) / M)
elif n >= K:
return "%.2fKb" % (float(n) / K)
else:
return "%d" % n