code: def render_queries(queries, sort):
      output = StringIO()
      if sort == 'order':
          print >>output, "     time query"
          for query in queries:
              print >>output, " %8s %s" % (query["time"], query["sql"])
          return output
      if sort == 'time':
          def sorter(x, y):
              return cmp(x[1][1], y[1][1])
      elif sort == 'queries':
          def sorter(x, y):
              return cmp(x[1][0], y[1][0])
      else:
          raise RuntimeError("Unknown sort: %s" % sort)
      print >>output, "  queries     time query"
      results = {}
      for query in queries:
          try:
              result = results[query["sql"]]
              result[0] += 1
              result[1] += Decimal(query["time"])
          except KeyError:
              results[query["sql"]] = [1, Decimal(query["time"])]
      results = sorted(results.iteritems(), cmp=sorter, reverse=True)
      for result in results:
          print >>output, " %8d %8.3f %s" % (
              result[1][0], result[1][1], result[0]
          )
      return output