display.py revision 1929
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 27class Value: 28 def __init__(self, value, precision, percent = False): 29 self.value = float(value) 30 self.precision = precision 31 self.percent = percent 32 def __str__(self): 33 if isinstance(self.value, str): 34 if self.value.lower() == 'nan': 35 value = 'NaN' 36 if self.value.lower() == 'inf': 37 value = 'Inf' 38 else: 39 if self.precision >= 0: 40 format = "%%.%df" % self.precision 41 elif self.value == 0.0: 42 format = "%.0f" 43 elif self.value % 1.0 == 0.0: 44 format = "%.0f" 45 else: 46 format = "%f" 47 value = self.value 48 if self.percent: 49 value = value * 100.0 50 value = format % value 51 52 if self.percent: 53 value = value + "%" 54 55 return value 56 57class Print: 58 def __init__(self, **vals): 59 self.__dict__.update(vals) 60 61 def __str__(self): 62 value = Value(self.value, self.precision) 63 pdf = '' 64 cdf = '' 65 if self.__dict__.has_key('pdf'): 66 pdf = Value(self.pdf, 2, True) 67 if self.__dict__.has_key('cdf'): 68 cdf = Value(self.cdf, 2, True) 69 70 output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf) 71 72 if descriptions and self.__dict__.has_key('desc') and self.desc: 73 output = "%s # %s" % (output, self.desc) 74 75 return output 76 77 def doprint(self): 78 if display_all: 79 return True 80 if self.value == 0.0 and (self.flags & flags_nozero): 81 return False 82 if isinstance(self.value, str): 83 if self.value == 'NaN' and (self.flags & flags_nonan): 84 return False 85 return True 86 87 def display(self): 88 if self.doprint(): 89 print self 90 91class VectorDisplay: 92 def display(self): 93 if not self.value: 94 return 95 96 p = Print() 97 p.flags = self.flags 98 p.precision = self.precision 99 100 if not isinstance(self.value, (list, tuple)): 101 p.name = self.name 102 p.desc = self.desc 103 p.value = self.value 104 p.display() 105 return 106 107 mytotal = reduce(lambda x,y: float(x) + float(y), self.value) 108 mycdf = 0.0 109 110 value = self.value 111 112 if display_all: 113 subnames = [ '[%d]' % i for i in range(len(value)) ] 114 else: 115 subnames = [''] * len(value) 116 117 if self.__dict__.has_key('subnames'): 118 for i,each in enumerate(self.subnames): 119 if len(each) > 0: 120 subnames[i] = '.%s' % each 121 122 subdescs = [self.desc]*len(value) 123 if self.__dict__.has_key('subdescs'): 124 for i in xrange(min(len(value), len(self.subdescs))): 125 subdescs[i] = self.subdescs[i] 126 127 for val,sname,sdesc in map(None, value, subnames, subdescs): 128 if mytotal > 0.0: 129 mypdf = float(val) / float(mytotal) 130 mycdf += mypdf 131 if (self.flags & flags_pdf): 132 p.pdf = mypdf 133 p.cdf = mycdf 134 135 if len(sname) == 0: 136 continue 137 138 p.name = self.name + sname 139 p.desc = sdesc 140 p.value = val 141 p.display() 142 143 if (self.flags & flags_total): 144 if (p.__dict__.has_key('pdf')): del p.__dict__['pdf'] 145 if (p.__dict__.has_key('cdf')): del p.__dict__['cdf'] 146 p.name = self.name + '.total' 147 p.desc = self.desc 148 p.value = mytotal 149 p.display() 150