parameters: (filename, results)
docstring: Output tabled benchmark results
  
      :Parameters:
        `filename` : ``str``
          Filename to write to
  
        `results` : ``list``
          Results
docstring_summary: Output tabled benchmark results
docstring_tokens: Output tabled benchmark results
function: def write_table(filename, results):
      """
      Output tabled benchmark results
  
      :Parameters:
        `filename` : ``str``
          Filename to write to
  
        `results` : ``list``
          Results
      """
      try:
          next
      except NameError:
          next = lambda i: (getattr(i, 'next', None) or i.__next__)()
      try:
          cmp
      except NameError:
          cmp = lambda a, b: (a > b) - (a < b)
  
      names = [
          ('simple_port', 'Simple Port'),
          ('jsmin_2_0_9', 'jsmin 2.0.9'),
          ('slimit_0_8_1', 'slimit 0.8.1'),
          ('slimit_0_8_1_mangle', 'slimit 0.8.1 (mangle)'),
          ('rjsmin', '|rjsmin|'),
          ('_rjsmin', r'_\ |rjsmin|'),
      ]
      benched_per_table = 2
  
      results = sorted(results, reverse=True)
  
      # First we transform our data into a table (list of lists)
      pythons, widths = [], [0] * (benched_per_table + 1)
      last_version = None
      for version, _, result in results:
          version = uni(version)
          if not(last_version is None or version.startswith('2.')):
              continue
          last_version = version
  
          namesub = _re.compile(r'(?:-\d+(?:\.\d+)*)?\.js$').sub
          result = iter(result)
          tables = []
  
          # given our data it's easier to create the table transposed...
          for benched in result:
              rows = [['Name'] + [desc for _, desc in names]]
              for _ in range(benched_per_table):
                  if _:
                      try:
                          benched = next(result)
                      except StopIteration:
                          rows.append([''] + ['' for _ in names])
                          continue
  
                  times = dict((
                      uni(port), (time, benched['sizes'][idx])
                  ) for idx, (port, time) in enumerate(benched['times']))
                  columns = ['%s (%.1f)' % (
                      namesub('', _os.path.basename(uni(benched['filename']))),
                      benched['size'] / 1024.0,
                  )]
                  for idx, (port, _) in enumerate(names):
                      if port not in times:
                          columns.append('n/a')
                          continue
                      time, size = times[port]
                      if time is None:
                          columns.append('(failed)')
                          continue
                      columns.append('%s%.2f ms (%.1f %s)' % (
                          idx == 0 and ' ' or '',
                          time,
                          size / 1024.0,
                          idx == 0 and '\\*' or ['=', '>', '<'][
                              cmp(size, benched['sizes'][0])
                          ],
                      ))
                  rows.append(columns)
  
              # calculate column widths (global for all tables)
              for idx, row in enumerate(rows):
                  widths[idx] = max(widths[idx], max(map(len, row)))
  
              # ... and transpose it back.
              tables.append(zip(*rows))
          pythons.append((version, tables))
  
          if last_version.startswith('2.'):
              break
  
      # Second we create a rest table from it
      lines = []
      separator = lambda c='-': '+'.join([''] + [
          c * (width + 2) for width in widths
      ] + [''])
  
      for idx, (version, tables) in enumerate(pythons):
          if idx:
              lines.append('')
              lines.append('')
  
          line = 'Python %s' % (version,)
          lines.append(line)
          lines.append('~' * len(line))
  
          for table in tables:
              lines.append('')
              lines.append('.. rst-class:: benchmark')
              lines.append('')
  
              for idx, row in enumerate(table):
                  if idx == 0:
                      # header
                      lines.append(separator())
                      lines.append('|'.join([''] + [
                          ' %s%*s ' % (col, len(col) - width, '')
                          for width, col in zip(widths, row)
                      ] + ['']))
                      lines.append(separator('='))
                  else: # data
                      lines.append('|'.join([''] + [
                          j == 0 and (
                              ' %s%*s ' % (col, len(col) - widths[j], '')
                          ) or (
                              ['%*s  ', ' %*s '][idx == 1] % (widths[j], col)
                          )
                          for j, col in enumerate(row)
                      ] + ['']))
                      lines.append(separator())
  
      fplines = []
      fp = open(filename)
      try:
          fpiter = iter(fp)
          for line in fpiter:
              line = line.rstrip()
              if line == '.. begin tables':
                  buf = []
                  for line in fpiter:
                      line = line.rstrip()
                      if line == '.. end tables':
                          fplines.append('.. begin tables')
                          fplines.append('')
                          fplines.extend(lines)
                          fplines.append('')
                          fplines.append('.. end tables')
                          buf = []
                          break
                      else:
                          buf.append(line)
                  else:
                      fplines.extend(buf)
                      _sys.stderr.write("Placeholder container not found!\n")
              else:
                  fplines.append(line)
      finally:
          fp.close()
  
      fp = open(filename, 'w')
      try:
          fp.write('\n'.join(fplines) + '\n')
      finally:
          fp.close()
function_tokens: def write_table ( filename , results ) : try : next except NameError : next = lambda i : ( getattr ( i , 'next' , None ) or i . __next__ ) ( ) try : cmp except NameError : cmp = lambda a , b : ( a > b ) - ( a < b ) names = [ ( 'simple_port' , 'Simple Port' ) , ( 'jsmin_2_0_9' , 'jsmin 2.0.9' ) , ( 'slimit_0_8_1' , 'slimit 0.8.1' ) , ( 'slimit_0_8_1_mangle' , 'slimit 0.8.1 (mangle)' ) , ( 'rjsmin' , '|rjsmin|' ) , ( '_rjsmin' , r'_\ |rjsmin|' ) , ] benched_per_table = 2 results = sorted ( results , reverse = True ) # First we transform our data into a table (list of lists) pythons , widths = [ ] , [ 0 ] * ( benched_per_table + 1 ) last_version = None for version , _ , result in results : version = uni ( version ) if not ( last_version is None or version . startswith ( '2.' ) ) : continue last_version = version namesub = _re . compile ( r'(?:-\d+(?:\.\d+)*)?\.js$' ) . sub result = iter ( result ) tables = [ ] # given our data it's easier to create the table transposed... for benched in result : rows = [ [ 'Name' ] + [ desc for _ , desc in names ] ] for _ in range ( benched_per_table ) : if _ : try : benched = next ( result ) except StopIteration : rows . append ( [ '' ] + [ '' for _ in names ] ) continue times = dict ( ( uni ( port ) , ( time , benched [ 'sizes' ] [ idx ] ) ) for idx , ( port , time ) in enumerate ( benched [ 'times' ] ) ) columns = [ '%s (%.1f)' % ( namesub ( '' , _os . path . basename ( uni ( benched [ 'filename' ] ) ) ) , benched [ 'size' ] / 1024.0 , ) ] for idx , ( port , _ ) in enumerate ( names ) : if port not in times : columns . append ( 'n/a' ) continue time , size = times [ port ] if time is None : columns . append ( '(failed)' ) continue columns . append ( '%s%.2f ms (%.1f %s)' % ( idx == 0 and ' ' or '' , time , size / 1024.0 , idx == 0 and '\\*' or [ '=' , '>' , '<' ] [ cmp ( size , benched [ 'sizes' ] [ 0 ] ) ] , ) ) rows . append ( columns ) # calculate column widths (global for all tables) for idx , row in enumerate ( rows ) : widths [ idx ] = max ( widths [ idx ] , max ( map ( len , row ) ) ) # ... and transpose it back. tables . append ( zip ( * rows ) ) pythons . append ( ( version , tables ) ) if last_version . startswith ( '2.' ) : break # Second we create a rest table from it lines = [ ] separator = lambda c = '-' : '+' . join ( [ '' ] + [ c * ( width + 2 ) for width in widths ] + [ '' ] ) for idx , ( version , tables ) in enumerate ( pythons ) : if idx : lines . append ( '' ) lines . append ( '' ) line = 'Python %s' % ( version , ) lines . append ( line ) lines . append ( '~' * len ( line ) ) for table in tables : lines . append ( '' ) lines . append ( '.. rst-class:: benchmark' ) lines . append ( '' ) for idx , row in enumerate ( table ) : if idx == 0 : # header lines . append ( separator ( ) ) lines . append ( '|' . join ( [ '' ] + [ ' %s%*s ' % ( col , len ( col ) - width , '' ) for width , col in zip ( widths , row ) ] + [ '' ] ) ) lines . append ( separator ( '=' ) ) else : # data lines . append ( '|' . join ( [ '' ] + [ j == 0 and ( ' %s%*s ' % ( col , len ( col ) - widths [ j ] , '' ) ) or ( [ '%*s  ' , ' %*s ' ] [ idx == 1 ] % ( widths [ j ] , col ) ) for j , col in enumerate ( row ) ] + [ '' ] ) ) lines . append ( separator ( ) ) fplines = [ ] fp = open ( filename ) try : fpiter = iter ( fp ) for line in fpiter : line = line . rstrip ( ) if line == '.. begin tables' : buf = [ ] for line in fpiter : line = line . rstrip ( ) if line == '.. end tables' : fplines . append ( '.. begin tables' ) fplines . append ( '' ) fplines . extend ( lines ) fplines . append ( '' ) fplines . append ( '.. end tables' ) buf = [ ] break else : buf . append ( line ) else : fplines . extend ( buf ) _sys . stderr . write ( "Placeholder container not found!\n" ) else : fplines . append ( line ) finally : fp . close ( ) fp = open ( filename , 'w' ) try : fp . write ( '\n' . join ( fplines ) + '\n' ) finally : fp . close ( )