Project

General

Profile

« Previous | Next » 

Revision 989

Moved value to string conversion functions infrom util.py to new module format.py

View differences:

lib/format.py
1
# Value to string conversion
2

  
3
import locale
4

  
5
locale.setlocale(locale.LC_ALL, '') # needed to initialize locale
6

  
7
def format_str(format, val):
8
    return locale.format_string(format, val, grouping=True)
9

  
10
def int2str(val): return format_str('%d', val)
11

  
12
def to_percent(val, sig_figs=2):
13
    if val >= 1: sig_figs += 1
14
    return format_str('%#.'+str(sig_figs)+'g', val*100)+'%'
15

  
16
def to_si(val, sig_figs=3):
17
    '''Adds SI prefix to value'''
18
    prefix = ''
19
    if val < 1: prefix = 'm'; val *= 1000
20
    return format_str('%#.'+str(sig_figs)+'g', val)+' '+prefix
0 21

  
lib/profiling.py
3 3
import sys
4 4
import datetime
5 5

  
6
import util
6
import format
7 7

  
8 8
def now(): return datetime.datetime.now()
9 9

  
......
37 37
    def msg(self):
38 38
        msg = Profiler.msg(self)
39 39
        if self.iter_ct != None:
40
            msg += ('/'+util.int2str(self.iter_ct)+' '+self.iter_text+'(s) = '
41
            +util.to_si(float(self.total.total_seconds())/self.iter_ct)
40
            msg += ('/'+format.int2str(self.iter_ct)+' '+self.iter_text+'(s) = '
41
            +format.to_si(float(self.total.total_seconds())/self.iter_ct)
42 42
            +'s/'+self.iter_text)
43 43
        return msg
lib/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()
lib/exc.py
3 3
import atexit
4 4
import sys
5 5

  
6
import format
6 7
import strings
7 8
import term
8 9
import util
......
57 58
    
58 59
    def msg(self):
59 60
        msg = ExTracker.msg(self)
60
        if self.iter_ct != None: msg += ('/'+util.int2str(self.iter_ct)+' '
61
        if self.iter_ct != None: msg += ('/'+format.int2str(self.iter_ct)+' '
61 62
            +self.iter_text+'(s) = '
62
            +util.to_percent(float(self.e_ct)/self.iter_ct))
63
            +format.to_percent(float(self.e_ct)/self.iter_ct))
63 64
        return msg

Also available in: Unified diff