nodes: 0, module; 1, function_definition; 2, function_name:_normalize_sort_SQL; 3, parameters; 4, block; 5, identifier:self; 6, identifier:field_name; 7, identifier:field_vals; 8, identifier:sort_dir_str; 9, expression_statement; 10, expression_statement; 11, if_statement; 12, expression_statement; 13, expression_statement; 14, for_statement; 15, expression_statement; 16, expression_statement; 17, return_statement; 18, comment:"""
          allow sorting by a set of values
  
          http://stackoverflow.com/questions/3303851/sqlite-and-custom-order-by
          """; 19, assignment; 20, comparison_operator:sort_dir_str == 'ASC'; 21, block; 22, else_clause; 23, assignment; 24, assignment; 25, pattern_list; 26, identifier:fvi; 27, block; 28, call; 29, assignment; 30, expression_list; 31, identifier:fvi; 32, None; 33, identifier:sort_dir_str; 34, string; 35, expression_statement; 36, block; 37, identifier:query_sort_str; 38, list; 39, identifier:query_args; 40, list; 41, identifier:i; 42, identifier:v; 43, expression_statement; 44, expression_statement; 45, attribute; 46, argument_list; 47, identifier:query_sort_str; 48, call; 49, identifier:query_sort_str; 50, identifier:query_args; 51, string_content:ASC; 52, assignment; 53, expression_statement; 54, call; 55, call; 56, call; 57, identifier:query_sort_str; 58, identifier:append; 59, string; 60, attribute; 61, argument_list; 62, identifier:fvi; 63, generator_expression; 64, assignment; 65, attribute; 66, argument_list; 67, attribute; 68, argument_list; 69, attribute; 70, argument_list; 71, string_content:END; 72, string:"\n"; 73, identifier:join; 74, identifier:query_sort_str; 75, identifier:t; 76, for_in_clause; 77, identifier:fvi; 78, generator_expression; 79, string; 80, identifier:format; 81, call; 82, identifier:query_sort_str; 83, identifier:append; 84, call; 85, identifier:query_args; 86, identifier:append; 87, identifier:v; 88, identifier:t; 89, call; 90, identifier:t; 91, for_in_clause; 92, string_content:CASE {}; 93, attribute; 94, argument_list; 95, attribute; 96, argument_list; 97, identifier:enumerate; 98, argument_list; 99, identifier:t; 100, call; 101, identifier:self; 102, identifier:_normalize_name; 103, identifier:field_name; 104, string; 105, identifier:format; 106, attribute; 107, identifier:i; 108, identifier:field_vals; 109, identifier:enumerate; 110, argument_list; 111, string_content:WHEN {} THEN {}; 112, identifier:self; 113, identifier:val_placeholder; 114, call; 115, identifier:reversed; 116, argument_list; 117, identifier:field_vals
code: def _normalize_sort_SQL(self, field_name, field_vals, sort_dir_str):
          """
          allow sorting by a set of values
  
          http://stackoverflow.com/questions/3303851/sqlite-and-custom-order-by
          """
          fvi = None
          if sort_dir_str == 'ASC':
              fvi = (t for t in enumerate(field_vals)) 
  
          else:
              fvi = (t for t in enumerate(reversed(field_vals))) 
  
          query_sort_str = ['  CASE {}'.format(self._normalize_name(field_name))]
          query_args = []
          for i, v in fvi:
              query_sort_str.append('    WHEN {} THEN {}'.format(self.val_placeholder, i))
              query_args.append(v)
  
          query_sort_str.append('  END')
          query_sort_str = "\n".join(query_sort_str)
          return query_sort_str, query_args