31 lines
899 B
Python
31 lines
899 B
Python
import unittest
|
|
|
|
|
|
def humanbytes(bytes: int) -> str:
|
|
"""non-localized conversion of bytes to human readable strings"""
|
|
powers = {0: 'bytes', 1: 'KB', 2: 'MB', 3: 'GB', 4: 'TB', 5: 'PB', 6: 'EB'}
|
|
power = 1
|
|
result = "%d bytes" % bytes
|
|
while bytes >= 1024 ** power and power in powers:
|
|
result = "%.3f" % (bytes / (1024 ** power))
|
|
result = result.rstrip("0")
|
|
result = result.rstrip(".")
|
|
result = result + " " + powers[power]
|
|
power = power + 1
|
|
return result
|
|
|
|
|
|
class TestLogFileModel(unittest.TestCase):
|
|
|
|
def test_humanbytes(self):
|
|
inputs = {
|
|
0: "0 bytes",
|
|
1023: "1023 bytes",
|
|
1024: "1 KB",
|
|
1048575: "1023.999 KB",
|
|
1048576: "1 MB",
|
|
}
|
|
for input in inputs.keys():
|
|
actual = humanbytes(input)
|
|
self.assertEqual(inputs[input], actual)
|