Hello Reimar. I added a new feature: You can choose the number format that is printed. Your default format is percent, but with -format=%d or any other python format string you can choose your own. HEre the changed code:

   1 class Parser:
   2 
   3     def __init__(self, raw, request, **kw):
   4         self.raw = raw
   5         self.request = request
   6 
   7     def format(self, formatter):
   8         raw = self.raw
   9         raw = raw.split('\n')
  10         request = self.request
  11 
  12         title = ''
  13 
  14         html = """
  15         <table width='100%' class='graph' cellspacing='3' border='0' cellpadding='0'>
  16         <thead>"""
  17 
  18         numFormat = 'percent' # '%d', '%5.2f'  
  19         sum = 0 
  20         n = 0 
  21         for arg in raw: 
  22             if arg.startswith('-loadcss'): 
  23                 request.write(barcss()) 
  24             elif arg.startswith('-format='):
  25                 numFormat = arg.split('=')[1]
  26             elif arg.startswith('-title'): 
  27                 cmd, title = arg.split(' ', 1) 
  28                 html += "<tr><th colspan='3'>%(title)s</th></tr>" % {"title": title}
  29             elif '=' in arg:  
  30                 name, value = arg.split('=', 1) 
  31                 sum += float(value)  
  32                 n += 1  
  33             else: 
  34                 name, value = '(format error: %s)' % arg, 0 
  35  
  36  
  37         html += "</thead><tbody>" 
  38  
  39         for arg in raw: 
  40             if not arg.startswith('-') and '=' in arg:
  41                 pvalue = str(round((float(value)/sum) * 100, 1))+'%'
  42                 if numFormat == 'percent':
  43                     nvalue = pvalue                                     
  44                 else: 
  45                     nvalue = numFormat % float(value)
  46                 name, value = arg.split('=', 1)
  47                 line = """ 
  48                 <tr> 
  49                 <td width='100'>%(name)s</td><td width='100%%' class='bar'><div style='width: %(pvalue)s'>&nbsp;</div></td><td>%(nvalue)s</td></tr>""" % {
  50                 "name": name, 
  51                 "pvalue": pvalue,
  52                 "nvalue": nvalue, 
  53                 } 
  54 
  55                 html += line
  56 
  57         html += "</tbody></table>"
  58 
  59         request.write(html)

-- towi, 2024-05-05 21:37:13 <towi SPAM @ NOTHERE geocities DOT com>

MoinMoin: ParserMarket/BarChart/Hints (last edited 2008-03-02 20:31:56 by ReimarBauer)