Created
February 9, 2012 09:44
-
-
Save lsbardel/1778912 to your computer and use it in GitHub Desktop.
Convert a number of bytes into a human readable memory usage
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
memory_symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') | |
memory_size = dict(((s,1 << (i+1)*10) for i,s in enumerate(memory_symbols))) | |
def convert_bytes(b): | |
'''Convert a number of bytes into a human readable memory usage''' | |
if b is None: | |
return '#NA' | |
for s in reversed(memory_symbols): | |
if b >= memory_size[s]: | |
value = float(b) / memory_size[s] | |
return '%.1f%sB' % (value, s) | |
return "%sB" % b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment