Revision 989
Added by Aaron Marcuse-Kubitza almost 13 years ago
util.py | ||
---|---|---|
32 | 32 |
|
33 | 33 |
def is_str(val): return isinstance(val, basestring) |
34 | 34 |
|
35 |
#### Strings |
|
36 |
|
|
37 |
def format_str(format, val): |
|
38 |
return locale.format_string(format, val, grouping=True) |
|
39 |
|
|
40 |
def int2str(val): return format_str('%d', val) |
|
41 |
|
|
42 |
def to_percent(val, sig_figs=2): |
|
43 |
if val >= 1: sig_figs += 1 |
|
44 |
return format_str('%#.'+str(sig_figs)+'g', val*100)+'%' |
|
45 |
|
|
46 |
def to_si(val, sig_figs=3): |
|
47 |
'''Adds SI prefix to value''' |
|
48 |
prefix = '' |
|
49 |
if val < 1: prefix = 'm'; val *= 1000 |
|
50 |
return format_str('%#.'+str(sig_figs)+'g', val)+' '+prefix |
|
51 |
|
|
52 | 35 |
#### Iterables |
53 | 36 |
|
54 | 37 |
def first(iter_): return iter_.next() |
Also available in: Unified diff
Moved value to string conversion functions infrom util.py to new module format.py