Update prettysize: Add parameter to enable trailing zeros.
--- a/pycolib/prettysize.py Mon Aug 19 16:32:33 2013 +0200
+++ b/pycolib/prettysize.py Mon Apr 14 22:29:07 2014 +0200
@@ -27,7 +27,7 @@
big_prefixes = 'KMGTPEZY'
-def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' '):
+def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' ', trailing_zeros=False):
'''Convert data size in bytes to human readable aproximation.
Beware that default output is invalid by recent standardization.
@@ -43,10 +43,11 @@
if num < base:
# format number
out = '{:.2f}'.format(num)
- # cut off trailing zeros
- out = out.rstrip('0')
- if out.endswith('.'):
- out = out[:-1]
+ if not trailing_zeros:
+ # cut off trailing zeros
+ out = out.rstrip('0')
+ if out.endswith('.'):
+ out = out[:-1]
return out + sep + prefix + unit
num /= base
prefix = prefixes.pop(0)
@@ -58,10 +59,10 @@
return prettysize(num, base=1000, **kw)
-def prettysize_short(num):
- return prettysize(num, unit='', sep='', prefixes=big_prefixes)
+def prettysize_short(num, **kw):
+ return prettysize(num, unit='', sep='', prefixes=big_prefixes, **kw)
-def prettysize_iec(num):
- return prettysize(num, prefixes=iec_prefixes)
+def prettysize_iec(num, **kw):
+ return prettysize(num, prefixes=iec_prefixes, **kw)