nodes: 0, module; 1, function_definition; 2, function_name:clean_line_profile_text; 3, parameters; 4, block; 5, identifier:text; 6, expression_statement; 7, comment:#; 8, expression_statement; 9, comment:#profile_block_list = fix_rawprofile_blocks(profile_block_list); 10, comment:#---; 11, comment:# FIXME can be written much nicer; 12, expression_statement; 13, comment:# Sort the blocks by time; 14, expression_statement; 15, expression_statement; 16, for_statement; 17, comment:# Rejoin output text; 18, expression_statement; 19, comment:#---; 20, comment:# Hack in a profile summary; 21, expression_statement; 22, expression_statement; 23, return_statement; 24, comment:"""
      Sorts the output from line profile by execution time
      Removes entries which were not run
      """; 25, assignment; 26, assignment; 27, assignment; 28, assignment; 29, pattern_list; 30, identifier:sorted_lists; 31, block; 32, assignment; 33, assignment; 34, assignment; 35, expression_list; 36, identifier:profile_block_list; 37, call; 38, pattern_list; 39, call; 40, identifier:sorted_lists; 41, call; 42, identifier:newlist; 43, subscript; 44, identifier:key; 45, identifier:val; 46, expression_statement; 47, identifier:output_text; 48, call; 49, identifier:summary_text; 50, call; 51, identifier:output_text; 52, identifier:output_text; 53, identifier:output_text; 54, identifier:summary_text; 55, identifier:parse_rawprofile_blocks; 56, argument_list; 57, identifier:prefix_list; 58, identifier:timemap; 59, identifier:parse_timemap_from_blocks; 60, argument_list; 61, identifier:sorted; 62, argument_list; 63, identifier:prefix_list; 64, slice; 65, call; 66, attribute; 67, argument_list; 68, identifier:get_summary; 69, argument_list; 70, identifier:text; 71, identifier:profile_block_list; 72, call; 73, keyword_argument; 74, attribute; 75, argument_list; 76, string; 77, identifier:join; 78, identifier:newlist; 79, identifier:profile_block_list; 80, attribute; 81, argument_list; 82, identifier:key; 83, call; 84, identifier:newlist; 85, identifier:extend; 86, identifier:val; 87, string_content; 88, identifier:six; 89, identifier:iteritems; 90, identifier:timemap; 91, attribute; 92, argument_list; 93, escape_sequence:\n; 94, identifier:operator; 95, identifier:itemgetter; 96, integer:0
code: def clean_line_profile_text(text):
      """
      Sorts the output from line profile by execution time
      Removes entries which were not run
      """
      #
      profile_block_list = parse_rawprofile_blocks(text)
      #profile_block_list = fix_rawprofile_blocks(profile_block_list)
      #---
      # FIXME can be written much nicer
      prefix_list, timemap = parse_timemap_from_blocks(profile_block_list)
      # Sort the blocks by time
      sorted_lists = sorted(six.iteritems(timemap), key=operator.itemgetter(0))
      newlist = prefix_list[:]
      for key, val in sorted_lists:
          newlist.extend(val)
      # Rejoin output text
      output_text = '\n'.join(newlist)
      #---
      # Hack in a profile summary
      summary_text = get_summary(profile_block_list)
      output_text = output_text
      return output_text, summary_text