1 # Copyright (c) 2003-2004 The Regents of The University of Michigan
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.
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.
27 from functools
import reduce
33 def __init__(self
, value
, precision
, percent
= False):
35 self
.precision
= precision
36 self
.percent
= percent
38 if isinstance(self
.value
, str):
39 if self
.value
.lower() == 'nan':
41 if self
.value
.lower() == 'inf':
44 if self
.precision
>= 0:
45 format
= "%%.%df" % self
.precision
46 elif self
.value
== 0.0:
48 elif self
.value
% 1.0 == 0.0:
55 value
= format
% value
63 def __init__(self
, **vals
):
64 self
.__dict
__.update(vals
)
67 value
= Value(self
.value
, self
.precision
)
70 if 'pdf' in self
.__dict
__:
71 pdf
= Value(self
.pdf
, 2, True)
72 if 'cdf' in self
.__dict
__:
73 cdf
= Value(self
.cdf
, 2, True)
75 output
= "%-40s %12s %8s %8s" % (self
.name
, value
, pdf
, cdf
)
77 if descriptions
and 'desc' in self
.__dict
__ and self
.desc
:
78 output
= "%s # %s" % (output
, self
.desc
)
85 if self
.value
== 0.0 and (self
.flags
& flags_nozero
):
87 if isinstance(self
.value
, str):
88 if self
.value
== 'NaN' and (self
.flags
& flags_nonan
):
100 p
.precision
= self
.precision
102 if isinstance(self
.value
, (list, tuple)):
103 if not len(self
.value
):
106 mytotal
= reduce(lambda x
,y
: float(x
) + float(y
), self
.value
)
112 subnames
= [ '[%d]' % i
for i
in range(len(value
)) ]
114 subnames
= [''] * len(value
)
116 if 'subnames' in self
.__dict
__:
117 for i
,each
in enumerate(self
.subnames
):
119 subnames
[i
] = '.%s' % each
121 subdescs
= [self
.desc
]*len(value
)
122 if 'subdescs' in self
.__dict
__:
123 for i
in range(min(len(value
), len(self
.subdescs
))):
124 subdescs
[i
] = self
.subdescs
[i
]
126 for val
,sname
,sdesc
in map(None, value
, subnames
, subdescs
):
128 mypdf
= float(val
) / float(mytotal
)
130 if (self
.flags
& flags_pdf
):
137 p
.name
= self
.name
+ sname
142 if (self
.flags
& flags_total
):
143 if ('pdf' in p
.__dict
__): del p
.__dict
__['pdf']
144 if ('cdf' in p
.__dict
__): del p
.__dict
__['cdf']
145 p
.name
= self
.name
+ '.total'