Skip to content

Instantly share code, notes, and snippets.

@larsyencken
Created September 23, 2011 03:58
Show Gist options
  • Save larsyencken/1236722 to your computer and use it in GitHub Desktop.
Save larsyencken/1236722 to your computer and use it in GitHub Desktop.
Pretty prints a size in bytes
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