display.py revision 1049
1class Value: 2 def __init__(self, value, precision, percent = False): 3 self.value = value 4 self.precision = precision 5 self.percent = percent 6 def __str__(self): 7 if isinstance(self.value, str): 8 if self.value.lower() == 'nan': 9 value = 'NaN' 10 if self.value.lower() == 'inf': 11 value = 'Inf' 12 else: 13 if self.precision >= 0: 14 format = "%%.%df" % self.precision 15 elif self.value == 0.0: 16 format = "%.0f" 17 elif self.value % 1.0 == 0.0: 18 format = "%.0f" 19 else: 20 format = "%f" 21 value = self.value 22 if self.percent: 23 value = value * 100.0 24 value = format % value 25 26 if self.percent: 27 value = value + "%" 28 29 return value 30 31class Print: 32 def __init__(self, **vals): 33 self.__dict__.update(vals) 34 35 def __str__(self): 36 value = Value(self.value, self.precision) 37 pdf = '' 38 cdf = '' 39 if self.__dict__.has_key('pdf'): 40 pdf = Value(self.pdf, 2, True) 41 if self.__dict__.has_key('cdf'): 42 cdf = Value(self.cdf, 2, True) 43 44 output = "%-40s %12s %8s %8s" % (self.name, value, pdf, cdf) 45 46 if descriptions and self.__dict__.has_key('desc') and self.desc: 47 output = "%s # %s" % (output, self.desc) 48 49 return output 50 51 def doprint(self): 52 if display_all: 53 return True 54 if self.value == 0.0 and (self.flags & flags_nozero): 55 return False 56 if isinstance(self.value, str): 57 if self.value == 'NaN' and (self.flags & flags_nonan): 58 return False 59 return True 60 61 def display(self): 62 if self.doprint(): 63 print self 64 65class VectorDisplay: 66 def display(self): 67 p = Print() 68 p.flags = self.flags 69 p.precision = self.precision 70 71 if issequence(self.value): 72 if not len(self.value): 73 return 74 75 mytotal = reduce(lambda x,y: float(x) + float(y), self.value) 76 mycdf = 0.0 77 78 value = self.value 79 80 if display_all: 81 subnames = [ '[%d]' % i for i in range(len(value)) ] 82 else: 83 subnames = [''] * len(value) 84 85 if self.__dict__.has_key('subnames'): 86 for i,each in enumerate(self.subnames): 87 if len(each) > 0: 88 subnames[i] = '.%s' % each 89 90 subdescs = [self.desc]*len(value) 91 if self.__dict__.has_key('subdescs'): 92 for i in xrange(min(len(value), len(self.subdescs))): 93 subdescs[i] = self.subdescs[i] 94 95 for val,sname,sdesc in map(None, value, subnames, subdescs): 96 if mytotal > 0.0: 97 mypdf = float(val) / float(mytotal) 98 mycdf += mypdf 99 if (self.flags & flags_pdf): 100 p.pdf = mypdf 101 p.cdf = mycdf 102 103 if len(sname) == 0: 104 continue 105 106 p.name = self.name + sname 107 p.desc = sdesc 108 p.value = val 109 p.display() 110 111 if (self.flags & flags_total): 112 if (p.__dict__.has_key('pdf')): del p.__dict__['pdf'] 113 if (p.__dict__.has_key('cdf')): del p.__dict__['cdf'] 114 p.name = self.name + '.total' 115 p.desc = self.desc 116 p.value = mytotal 117 p.display() 118 119 else: 120 p.name = self.name 121 p.desc = self.desc 122 p.value = self.value 123 p.display() 124 125