Last active
August 29, 2015 14:08
-
-
Save alexprengere/5b5adf93c7fbed949ff1 to your computer and use it in GitHub Desktop.
Some prettification functions.
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/python | |
""" | |
Some prettification functions. | |
>>> humanize(1024, is_bytes=True) | |
'1.0 kB' | |
>>> humanize(1000*12342) | |
'12.3 M' | |
>>> pretty_num(1255000) | |
'1,255,000' | |
""" | |
import locale | |
def humanize(d, precision=1, is_bytes=False): | |
"""Return a humanized string representation of a number. | |
>>> humanize(1, is_bytes=True) | |
'1 byte' | |
>>> humanize(1024, is_bytes=True) | |
'1.0 kB' | |
>>> humanize(1024*123, is_bytes=True) | |
'123.0 kB' | |
>>> humanize(1024*12342, is_bytes=True) | |
'12.1 MB' | |
>>> humanize(1024*12342, precision=2, is_bytes=True) | |
'12.05 MB' | |
>>> humanize(1024*1234, precision=2, is_bytes=True) | |
'1.21 MB' | |
>>> humanize(1024*1234*1111, precision=2, is_bytes=True) | |
'1.31 GB' | |
>>> humanize(1024*1234*1111, precision=1, is_bytes=True) | |
'1.3 GB' | |
>>> humanize(1) | |
'1' | |
>>> humanize(1000) | |
'1.0 k' | |
>>> humanize(1000*123) | |
'123.0 k' | |
>>> humanize(1000*12342) | |
'12.3 M' | |
>>> humanize(1000*12342, precision=2) | |
'12.34 M' | |
>>> humanize(1000*1234, precision=2) | |
'1.23 M' | |
>>> humanize(1000*1234*1111, precision=2) | |
'1.37 G' | |
>>> humanize(1000*1234*1111, precision=1) | |
'1.4 G' | |
""" | |
abbrevs_bytes = ( | |
(1<<50L, 'PB'), | |
(1<<40L, 'TB'), | |
(1<<30L, 'GB'), | |
(1<<20L, 'MB'), | |
(1<<10L, 'kB'), | |
(1, 'bytes') | |
) | |
abbrevs_si = ( | |
(1e15, 'P'), | |
(1e12, 'T'), | |
(1e9, 'G'), | |
(1e6, 'M'), | |
(1e3, 'k'), | |
(1, '') | |
) | |
if is_bytes: | |
abbrevs = abbrevs_bytes | |
else: | |
abbrevs = abbrevs_si | |
if d == 1: | |
if is_bytes: | |
return '1 byte' | |
else: | |
return '1' | |
for factor, suffix in abbrevs: | |
if d >= factor: | |
break | |
return '%.*f %s' % (precision, float(d) / factor, suffix) | |
try: | |
locale.setlocale(locale.LC_ALL, 'en_US') | |
except locale.Error: | |
def pretty_num(d): | |
"""Prettify num. | |
>>> pretty_num(1255000) | |
'1255000' | |
""" | |
return "%d" % d | |
else: | |
def pretty_num(d): | |
"""Prettify num. | |
>>> locale.format("%d", 1255000, grouping=True) | |
'1,255,000' | |
>>> locale.format("%d", 1255000.50, grouping=True) | |
'1,255,000' | |
>>> pretty_num(1255000) | |
'1,255,000' | |
""" | |
return locale.format("%d", d, grouping=True) | |
def _test(): | |
""" | |
When called directly, launching doctests. | |
""" | |
import doctest | |
opt = (doctest.ELLIPSIS | | |
doctest.NORMALIZE_WHITESPACE | | |
doctest.REPORT_ONLY_FIRST_FAILURE ) | |
#doctest.IGNORE_EXCEPTION_DETAIL) | |
doctest.testmod(optionflags=opt, verbose=False) | |
if __name__ == '__main__': | |
_test() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment