Created
September 23, 2011 03:58
-
-
Save larsyencken/1236722 to your computer and use it in GitHub Desktop.
Pretty prints a size in bytes
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
def human_size(s): | |
""" | |
Given a size interval in seconds, return it pretty-printed. | |
>>> human_size(875) | |
'875b' | |
>>> human_size(3037) | |
'3.0Kb' | |
>>> human_size(3691234) | |
'3.7Mb' | |
>>> human_size(2342425232) | |
'2.3Gb' | |
""" | |
if s < 1000: | |
return '%db' % s | |
s = float(s) / 1000.0 | |
if s < 1000: | |
return '%.01fKb' % s | |
s = float(s) / 1000.0 | |
if s < 1000: | |
return '%.01fMb' % s | |
s = float(s) / 1000.0 | |
if s < 1000: | |
return '%.01fGb' % s | |
s = float(s) / 1000.0 | |
if s < 1000: | |
return '%.01fTb' % s | |
return '%.0fTb' % s | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment