Created
August 26, 2015 14:47
-
-
Save romuald/5b4daa3e310e305a2833 to your computer and use it in GitHub Desktop.
Convert bytes into Kbytes, MBytes & cie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Converts large bytes into a more readable form. | |
Example:: | |
% python mo.py 769736704 | |
734.08 Mio - 751696.00 Kio | |
""" | |
import sys | |
def transform(value): | |
UNITS = ('o', 'Kio', 'Mio', 'Gio', 'Tio', 'Pio', 'Eio', 'Zio', 'Yio') | |
ret = [] | |
for i, unit in enumerate(UNITS): | |
x = value / (1024.0 ** i) | |
if x < 1: | |
break | |
ret.append((x, unit)) | |
return ret | |
def transformat(value, num=2): | |
return ' - '.join('%.2f %s' % z for z in reversed(transform(value)[-num:])) | |
for value in sys.argv[1:]: | |
print(transformat(int(value))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment