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
30 def __init__(self
, value
, precision
, percent
= False):
31 self
.value
= float(value
)
32 self
.precision
= precision
33 self
.percent
= percent
35 if isinstance(self
.value
, str):
36 if self
.value
.lower() == 'nan':
38 if self
.value
.lower() == 'inf':
41 if self
.precision
>= 0:
42 format
= "%%.%df" % self
.precision
43 elif self
.value
== 0.0:
45 elif self
.value
% 1.0 == 0.0:
52 value
= format
% value
60 def __init__(self
, **vals
):
61 self
.__dict
__.update(vals
)
64 value
= Value(self
.value
, self
.precision
)
67 if 'pdf' in self
.__dict
__:
68 pdf
= Value(self
.pdf
, 2, True)
69 if 'cdf' in self
.__dict
__:
70 cdf
= Value(self
.cdf
, 2, True)
72 output
= "%-40s %12s %8s %8s" % (self
.name
, value
, pdf
, cdf
)
74 if descriptions
and 'desc' in self
.__dict
__ and self
.desc
:
75 output
= "%s # %s" % (output
, self
.desc
)
82 if self
.value
== 0.0 and (self
.flags
& flags_nozero
):
84 if isinstance(self
.value
, str):
85 if self
.value
== 'NaN' and (self
.flags
& flags_nonan
):
100 p
.precision
= self
.precision
102 if not isinstance(self
.value
, (list, tuple)):
109 mytotal
= reduce(lambda x
,y
: float(x
) + float(y
), self
.value
)
115 subnames
= [ '[%d]' % i
for i
in range(len(value
)) ]
117 subnames
= [''] * len(value
)
119 if 'subnames' in self
.__dict
__:
120 for i
,each
in enumerate(self
.subnames
):
122 subnames
[i
] = '.%s' % each
124 subdescs
= [self
.desc
]*len(value
)
125 if 'subdescs' in self
.__dict
__:
126 for i
in range(min(len(value
), len(self
.subdescs
))):
127 subdescs
[i
] = self
.subdescs
[i
]
129 for val
,sname
,sdesc
in map(None, value
, subnames
, subdescs
):
131 mypdf
= float(val
) / float(mytotal
)
133 if (self
.flags
& flags_pdf
):
140 p
.name
= self
.name
+ sname
145 if (self
.flags
& flags_total
):
146 if ('pdf' in p
.__dict
__): del p
.__dict
__['pdf']
147 if ('cdf' in p
.__dict
__): del p
.__dict
__['cdf']
148 p
.name
= self
.name
+ '.total'