print.py (1772:a3a83e812a5e) print.py (2665:a124942bacb8)
1# Copyright (c) 2003-2004 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1# Copyright (c) 2003-2004 The Regents of The University of Michigan
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Nathan Binkert
26
27all = False
28descriptions = False
29
30class Value:
31 def __init__(self, value, precision, percent = False):
32 self.value = value
33 self.precision = precision
34 self.percent = percent
35 def __str__(self):
36 if isinstance(self.value, str):
37 if self.value.lower() == 'nan':
38 value = 'NaN'
39 if self.value.lower() == 'inf':
40 value = 'Inf'
41 else:
42 if self.precision >= 0:
43 format = "%%.%df" % self.precision
44 elif self.value == 0.0:
45 format = "%.0f"
46 elif self.value % 1.0 == 0.0:
47 format = "%.0f"
48 else:
49 format = "%f"
50 value = self.value
51 if self.percent:
52 value = value * 100.0
53 value = format % value
54
55 if self.percent:
56 value = value + "%"
57
58 return value
59
60class Print:
61 def __init__(self, **vals):
62 self.__dict__.update(vals)
63
64 def __str__(self):
65 value = Value(self.value, self.precision)
66 pdf = ''
67 cdf = ''
68 if self.__dict__.has_key('pdf'):
69 pdf = Value(self.pdf, 2, True)
70 if self.__dict__.has_key('cdf'):
71 cdf = Value(self.cdf, 2, True)
72
73 output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
74
75 if descriptions and self.__dict__.has_key('desc') and self.desc:
76 output = "%s # %s" % (output, self.desc)
77
78 return output
79
80 def doprint(self):
81 if display_all:
82 return True
83 if self.value == 0.0 and (self.flags & flags_nozero):
84 return False
85 if isinstance(self.value, str):
86 if self.value == 'NaN' and (self.flags & flags_nonan):
87 return False
88 return True
89
90 def display(self):
91 if self.doprint():
92 print self
93
94class VectorDisplay:
95 def display(self):
96 p = Print()
97 p.flags = self.flags
98 p.precision = self.precision
99
100 if isinstance(self.value, (list, tuple)):
101 if not len(self.value):
102 return
103
104 mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
105 mycdf = 0.0
106
107 value = self.value
108
109 if display_all:
110 subnames = [ '[%d]' % i for i in range(len(value)) ]
111 else:
112 subnames = [''] * len(value)
113
114 if self.__dict__.has_key('subnames'):
115 for i,each in enumerate(self.subnames):
116 if len(each) > 0:
117 subnames[i] = '.%s' % each
118
119 subdescs = [self.desc]*len(value)
120 if self.__dict__.has_key('subdescs'):
121 for i in xrange(min(len(value), len(self.subdescs))):
122 subdescs[i] = self.subdescs[i]
123
124 for val,sname,sdesc in map(None, value, subnames, subdescs):
125 if mytotal > 0.0:
126 mypdf = float(val) / float(mytotal)
127 mycdf += mypdf
128 if (self.flags & flags_pdf):
129 p.pdf = mypdf
130 p.cdf = mycdf
131
132 if len(sname) == 0:
133 continue
134
135 p.name = self.name + sname
136 p.desc = sdesc
137 p.value = val
138 p.display()
139
140 if (self.flags & flags_total):
141 if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
142 if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
143 p.name = self.name + '.total'
144 p.desc = self.desc
145 p.value = mytotal
146 p.display()
147
148 else:
149 p.name = self.name
150 p.desc = self.desc
151 p.value = self.value
152 p.display()
153
28
29all = False
30descriptions = False
31
32class Value:
33 def __init__(self, value, precision, percent = False):
34 self.value = value
35 self.precision = precision
36 self.percent = percent
37 def __str__(self):
38 if isinstance(self.value, str):
39 if self.value.lower() == 'nan':
40 value = 'NaN'
41 if self.value.lower() == 'inf':
42 value = 'Inf'
43 else:
44 if self.precision >= 0:
45 format = "%%.%df" % self.precision
46 elif self.value == 0.0:
47 format = "%.0f"
48 elif self.value % 1.0 == 0.0:
49 format = "%.0f"
50 else:
51 format = "%f"
52 value = self.value
53 if self.percent:
54 value = value * 100.0
55 value = format % value
56
57 if self.percent:
58 value = value + "%"
59
60 return value
61
62class Print:
63 def __init__(self, **vals):
64 self.__dict__.update(vals)
65
66 def __str__(self):
67 value = Value(self.value, self.precision)
68 pdf = ''
69 cdf = ''
70 if self.__dict__.has_key('pdf'):
71 pdf = Value(self.pdf, 2, True)
72 if self.__dict__.has_key('cdf'):
73 cdf = Value(self.cdf, 2, True)
74
75 output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf)
76
77 if descriptions and self.__dict__.has_key('desc') and self.desc:
78 output = "%s # %s" % (output, self.desc)
79
80 return output
81
82 def doprint(self):
83 if display_all:
84 return True
85 if self.value == 0.0 and (self.flags & flags_nozero):
86 return False
87 if isinstance(self.value, str):
88 if self.value == 'NaN' and (self.flags & flags_nonan):
89 return False
90 return True
91
92 def display(self):
93 if self.doprint():
94 print self
95
96class VectorDisplay:
97 def display(self):
98 p = Print()
99 p.flags = self.flags
100 p.precision = self.precision
101
102 if isinstance(self.value, (list, tuple)):
103 if not len(self.value):
104 return
105
106 mytotal = reduce(lambda x,y: float(x) + float(y), self.value)
107 mycdf = 0.0
108
109 value = self.value
110
111 if display_all:
112 subnames = [ '[%d]' % i for i in range(len(value)) ]
113 else:
114 subnames = [''] * len(value)
115
116 if self.__dict__.has_key('subnames'):
117 for i,each in enumerate(self.subnames):
118 if len(each) > 0:
119 subnames[i] = '.%s' % each
120
121 subdescs = [self.desc]*len(value)
122 if self.__dict__.has_key('subdescs'):
123 for i in xrange(min(len(value), len(self.subdescs))):
124 subdescs[i] = self.subdescs[i]
125
126 for val,sname,sdesc in map(None, value, subnames, subdescs):
127 if mytotal > 0.0:
128 mypdf = float(val) / float(mytotal)
129 mycdf += mypdf
130 if (self.flags & flags_pdf):
131 p.pdf = mypdf
132 p.cdf = mycdf
133
134 if len(sname) == 0:
135 continue
136
137 p.name = self.name + sname
138 p.desc = sdesc
139 p.value = val
140 p.display()
141
142 if (self.flags & flags_total):
143 if (p.__dict__.has_key('pdf')): del p.__dict__['pdf']
144 if (p.__dict__.has_key('cdf')): del p.__dict__['cdf']
145 p.name = self.name + '.total'
146 p.desc = self.desc
147 p.value = mytotal
148 p.display()
149
150 else:
151 p.name = self.name
152 p.desc = self.desc
153 p.value = self.value
154 p.display()
155