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