nodes: 0, module; 1, function_definition; 2, function_name:S; 3, parameters; 4, block; 5, identifier:Document; 6, list_splat_pattern; 7, expression_statement; 8, expression_statement; 9, for_statement; 10, return_statement; 11, identifier:fields; 12, comment:"""Generate a MongoDB sort order list using the Django ORM style."""; 13, assignment; 14, identifier:field; 15, identifier:fields; 16, block; 17, identifier:result; 18, identifier:result; 19, list; 20, if_statement; 21, expression_statement; 22, if_statement; 23, if_statement; 24, if_statement; 25, expression_statement; 26, expression_statement; 27, call; 28, comment:# Unpack existing tuple.; 29, block; 30, assignment; 31, not_operator; 32, block; 33, comparison_operator:field[0] == '-'; 34, block; 35, comparison_operator:field[0] in ('+', '-'); 36, block; 37, assignment; 38, call; 39, identifier:isinstance; 40, argument_list; 41, expression_statement; 42, expression_statement; 43, continue_statement; 44, identifier:direction; 45, identifier:ASCENDING; 46, call; 47, expression_statement; 48, subscript; 49, string; 50, expression_statement; 51, subscript; 52, tuple; 53, expression_statement; 54, identifier:_field; 55, call; 56, attribute; 57, argument_list; 58, identifier:field; 59, identifier:tuple; 60, assignment; 61, call; 62, attribute; 63, argument_list; 64, assignment; 65, identifier:field; 66, integer:0; 67, string_content:-; 68, assignment; 69, identifier:field; 70, integer:0; 71, string; 72, string; 73, assignment; 74, identifier:traverse; 75, argument_list; 76, identifier:result; 77, identifier:append; 78, tuple; 79, pattern_list; 80, identifier:field; 81, attribute; 82, argument_list; 83, identifier:field; 84, identifier:startswith; 85, string; 86, identifier:field; 87, call; 88, identifier:direction; 89, identifier:DESCENDING; 90, string_content:+; 91, string_content:-; 92, identifier:field; 93, subscript; 94, identifier:Document; 95, identifier:field; 96, keyword_argument; 97, conditional_expression:(~_field) if _field else field; 98, identifier:direction; 99, identifier:field; 100, identifier:direction; 101, identifier:result; 102, identifier:append; 103, tuple; 104, string_content:__; 105, attribute; 106, argument_list; 107, identifier:field; 108, slice; 109, identifier:default; 110, None; 111, parenthesized_expression; 112, identifier:_field; 113, identifier:field; 114, identifier:field; 115, identifier:direction; 116, identifier:field; 117, identifier:replace; 118, string; 119, string; 120, integer:1; 121, unary_operator; 122, string_content:__; 123, string_content:.; 124, identifier:_field
code: def S(Document, *fields):
  	"""Generate a MongoDB sort order list using the Django ORM style."""
  	
  	result = []
  	
  	for field in fields:
  		if isinstance(field, tuple):  # Unpack existing tuple.
  			field, direction = field
  			result.append((field, direction))
  			continue
  		
  		direction = ASCENDING
  		
  		if not field.startswith('__'):
  			field = field.replace('__', '.')
  		
  		if field[0] == '-':
  			direction = DESCENDING
  		
  		if field[0] in ('+', '-'):
  			field = field[1:]
  		
  		_field = traverse(Document, field, default=None)
  		
  		result.append(((~_field) if _field else field, direction))
  	
  	return result