ask user before creating a clipboard larger than 5 MB
This commit is contained in:
30
conversion.py
Normal file
30
conversion.py
Normal file
@@ -0,0 +1,30 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user