nodes: 0, module; 1, function_definition; 2, function_name:render_queries; 3, parameters; 4, block; 5, identifier:queries; 6, identifier:sort; 7, expression_statement; 8, expression_statement; 9, if_statement; 10, if_statement; 11, print_statement; 12, expression_statement; 13, for_statement; 14, expression_statement; 15, for_statement; 16, return_statement; 17, comment:"""
      Returns a StringIO containing the formatted SQL queries.
  
      _sort_ is a field to sort by.
      """; 18, assignment; 19, comparison_operator:sort == 'order'; 20, block; 21, comparison_operator:sort == 'time'; 22, block; 23, elif_clause; 24, else_clause; 25, chevron; 26, string:"  queries     time query"; 27, assignment; 28, identifier:query; 29, identifier:queries; 30, block; 31, assignment; 32, identifier:result; 33, identifier:results; 34, block; 35, identifier:output; 36, identifier:output; 37, call; 38, identifier:sort; 39, string; 40, print_statement; 41, for_statement; 42, return_statement; 43, identifier:sort; 44, string; 45, function_definition; 46, comparison_operator:sort == 'queries'; 47, block; 48, block; 49, identifier:output; 50, identifier:results; 51, dictionary; 52, try_statement; 53, identifier:results; 54, call; 55, print_statement; 56, identifier:StringIO; 57, argument_list; 58, string_content:order; 59, chevron; 60, string:"     time query"; 61, identifier:query; 62, identifier:queries; 63, block; 64, identifier:output; 65, string_content:time; 66, function_name:sorter; 67, parameters; 68, block; 69, identifier:sort; 70, string; 71, function_definition; 72, raise_statement; 73, block; 74, except_clause; 75, identifier:sorted; 76, argument_list; 77, chevron; 78, binary_operator:" %8d %8.3f %s" % (
              result[1][0], result[1][1], result[0]
          ); 79, identifier:output; 80, print_statement; 81, identifier:x; 82, identifier:y; 83, return_statement; 84, string_content:queries; 85, function_name:sorter; 86, parameters; 87, block; 88, call; 89, expression_statement; 90, expression_statement; 91, expression_statement; 92, identifier:KeyError; 93, block; 94, call; 95, keyword_argument; 96, keyword_argument; 97, identifier:output; 98, string:" %8d %8.3f %s"; 99, tuple; 100, chevron; 101, binary_operator:" %8s %s" % (query["time"], query["sql"]); 102, call; 103, identifier:x; 104, identifier:y; 105, return_statement; 106, identifier:RuntimeError; 107, argument_list; 108, assignment; 109, augmented_assignment; 110, augmented_assignment; 111, expression_statement; 112, attribute; 113, argument_list; 114, identifier:cmp; 115, identifier:sorter; 116, identifier:reverse; 117, True; 118, subscript; 119, subscript; 120, subscript; 121, identifier:output; 122, string:" %8s %s"; 123, tuple; 124, identifier:cmp; 125, argument_list; 126, call; 127, binary_operator:"Unknown sort: %s" % sort; 128, identifier:result; 129, subscript; 130, subscript; 131, integer:1; 132, subscript; 133, call; 134, assignment; 135, identifier:results; 136, identifier:iteritems; 137, subscript; 138, integer:0; 139, subscript; 140, integer:1; 141, identifier:result; 142, integer:0; 143, subscript; 144, subscript; 145, subscript; 146, subscript; 147, identifier:cmp; 148, argument_list; 149, string:"Unknown sort: %s"; 150, identifier:sort; 151, identifier:results; 152, subscript; 153, identifier:result; 154, integer:0; 155, identifier:result; 156, integer:1; 157, identifier:Decimal; 158, argument_list; 159, subscript; 160, list; 161, identifier:result; 162, integer:1; 163, identifier:result; 164, integer:1; 165, identifier:query; 166, string:"time"; 167, identifier:query; 168, string:"sql"; 169, subscript; 170, integer:1; 171, subscript; 172, integer:1; 173, subscript; 174, subscript; 175, identifier:query; 176, string:"sql"; 177, subscript; 178, identifier:results; 179, subscript; 180, integer:1; 181, call; 182, identifier:x; 183, integer:1; 184, identifier:y; 185, integer:1; 186, subscript; 187, integer:0; 188, subscript; 189, integer:0; 190, identifier:query; 191, string:"time"; 192, identifier:query; 193, string:"sql"; 194, identifier:Decimal; 195, argument_list; 196, identifier:x; 197, integer:1; 198, identifier:y; 199, integer:1; 200, subscript; 201, identifier:query; 202, string:"time"
code: def render_queries(queries, sort):
      """
      Returns a StringIO containing the formatted SQL queries.
  
      _sort_ is a field to sort by.
      """
      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