equal
deleted
inserted
replaced
25 iec_prefixes = ('Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi') |
25 iec_prefixes = ('Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi') |
26 si_prefixes = 'kMGTPEZY' |
26 si_prefixes = 'kMGTPEZY' |
27 big_prefixes = 'KMGTPEZY' |
27 big_prefixes = 'KMGTPEZY' |
28 |
28 |
29 |
29 |
30 def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' '): |
30 def prettysize(num, base=1024, prefixes=si_prefixes, unit='B', sep=' ', trailing_zeros=False): |
31 '''Convert data size in bytes to human readable aproximation. |
31 '''Convert data size in bytes to human readable aproximation. |
32 |
32 |
33 Beware that default output is invalid by recent standardization. |
33 Beware that default output is invalid by recent standardization. |
34 Still, this form was in use for ages. |
34 Still, this form was in use for ages. |
35 |
35 |
41 num = float(num) |
41 num = float(num) |
42 while True: |
42 while True: |
43 if num < base: |
43 if num < base: |
44 # format number |
44 # format number |
45 out = '{:.2f}'.format(num) |
45 out = '{:.2f}'.format(num) |
46 # cut off trailing zeros |
46 if not trailing_zeros: |
47 out = out.rstrip('0') |
47 # cut off trailing zeros |
48 if out.endswith('.'): |
48 out = out.rstrip('0') |
49 out = out[:-1] |
49 if out.endswith('.'): |
|
50 out = out[:-1] |
50 return out + sep + prefix + unit |
51 return out + sep + prefix + unit |
51 num /= base |
52 num /= base |
52 prefix = prefixes.pop(0) |
53 prefix = prefixes.pop(0) |
53 |
54 |
54 raise OverflowError('Number is too large.') |
55 raise OverflowError('Number is too large.') |
56 |
57 |
57 def prettysize_si(num, **kw): |
58 def prettysize_si(num, **kw): |
58 return prettysize(num, base=1000, **kw) |
59 return prettysize(num, base=1000, **kw) |
59 |
60 |
60 |
61 |
61 def prettysize_short(num): |
62 def prettysize_short(num, **kw): |
62 return prettysize(num, unit='', sep='', prefixes=big_prefixes) |
63 return prettysize(num, unit='', sep='', prefixes=big_prefixes, **kw) |
63 |
64 |
64 |
65 |
65 def prettysize_iec(num): |
66 def prettysize_iec(num, **kw): |
66 return prettysize(num, prefixes=iec_prefixes) |
67 return prettysize(num, prefixes=iec_prefixes, **kw) |
67 |
68 |