Deleted Added
sdiff udiff text old ( 1758:74acd5b23964 ) new ( 1772:a3a83e812a5e )
full compact
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
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