nodes: 0, module; 1, function_definition; 2, function_name:sort; 3, parameters; 4, block; 5, identifier:imports; 6, default_parameter; 7, default_parameter; 8, dictionary_splat_pattern; 9, expression_statement; 10, if_statement; 11, if_statement; 12, comment:# Partition the imports; 13, expression_statement; 14, for_statement; 15, comment:# sort each of the segments; 16, for_statement; 17, return_statement; 18, identifier:separate; 19, True; 20, identifier:import_before_from; 21, True; 22, identifier:classify_kwargs; 23, comment:"""Sort import objects into groups.
  
      :param list imports: FromImport / ImportImport objects
      :param bool separate: Whether to classify and return separate segments
          of imports based on classification.
      :param bool import_before_from: Whether to sort `import ...` imports before
          `from ...` imports.
  
      For example:
          from os import path
          from aspy import refactor_imports
          import sys
          import pyramid
  
      separate = True, import_before_from = True
  
          import sys
          from os import path
  
          import pyramid
  
          from aspy import refactor_imports
  
      separate = True, import_before_from = False
  
          from os import path
          import sys
  
          import pyramid
  
          from aspy import refactor_imports
  
      separate = False, import_before_from = True
  
          import pyramid
          import sys
          from aspy import refactor_imports
          from os import path
  
      separate = False, import_before_from = False
  
          from aspy import refactor_imports
          from os import path
          import pyramid
          import sys
      """; 24, identifier:separate; 25, block; 26, else_clause; 27, identifier:import_before_from; 28, block; 29, else_clause; 30, assignment; 31, identifier:import_obj; 32, identifier:imports; 33, block; 34, pattern_list; 35, call; 36, block; 37, call; 38, function_definition; 39, expression_statement; 40, comment:# A little cheaty, this allows future imports to sort before others; 41, block; 42, function_definition; 43, block; 44, identifier:imports_partitioned; 45, call; 46, expression_statement; 47, identifier:segment_key; 48, identifier:val; 49, attribute; 50, argument_list; 51, expression_statement; 52, identifier:tuple; 53, generator_expression; 54, function_name:classify_func; 55, parameters; 56, block; 57, assignment; 58, function_definition; 59, expression_statement; 60, function_name:sort_within; 61, parameters; 62, block; 63, function_definition; 64, attribute; 65, argument_list; 66, call; 67, identifier:imports_partitioned; 68, identifier:items; 69, assignment; 70, call; 71, for_in_clause; 72, if_clause; 73, identifier:obj; 74, return_statement; 75, identifier:types; 76, attribute; 77, function_name:classify_func; 78, parameters; 79, block; 80, assignment; 81, identifier:obj; 82, return_statement; 83, function_name:sort_within; 84, parameters; 85, block; 86, identifier:collections; 87, identifier:defaultdict; 88, identifier:list; 89, attribute; 90, argument_list; 91, subscript; 92, call; 93, identifier:tuple; 94, argument_list; 95, identifier:key; 96, identifier:types; 97, comparison_operator:key in imports_partitioned; 98, call; 99, identifier:ImportType; 100, identifier:__all__; 101, identifier:obj; 102, return_statement; 103, identifier:types; 104, list; 105, binary_operator:(CLS_TO_INDEX[type(obj)],) + obj.sort_key; 106, identifier:obj; 107, return_statement; 108, subscript; 109, identifier:append; 110, identifier:import_obj; 111, identifier:imports_partitioned; 112, identifier:segment_key; 113, identifier:sorted; 114, argument_list; 115, subscript; 116, identifier:key; 117, identifier:imports_partitioned; 118, identifier:classify_import; 119, argument_list; 120, comparison_operator:classify_import(
                  obj.import_statement.module, **classify_kwargs
              ) == ImportType.FUTURE; 121, True; 122, False; 123, tuple; 124, attribute; 125, call; 126, identifier:imports_partitioned; 127, call; 128, identifier:val; 129, keyword_argument; 130, identifier:imports_partitioned; 131, identifier:key; 132, attribute; 133, dictionary_splat; 134, call; 135, attribute; 136, subscript; 137, identifier:obj; 138, identifier:sort_key; 139, identifier:tuple; 140, argument_list; 141, identifier:classify_func; 142, argument_list; 143, identifier:key; 144, identifier:sort_within; 145, attribute; 146, identifier:module; 147, identifier:classify_kwargs; 148, identifier:classify_import; 149, argument_list; 150, identifier:ImportType; 151, identifier:FUTURE; 152, identifier:CLS_TO_INDEX; 153, call; 154, attribute; 155, identifier:import_obj; 156, identifier:obj; 157, identifier:import_statement; 158, attribute; 159, dictionary_splat; 160, identifier:type; 161, argument_list; 162, identifier:obj; 163, identifier:sort_key; 164, attribute; 165, identifier:module; 166, identifier:classify_kwargs; 167, identifier:obj; 168, identifier:obj; 169, identifier:import_statement
code: def sort(imports, separate=True, import_before_from=True, **classify_kwargs):
      """Sort import objects into groups.
  
      :param list imports: FromImport / ImportImport objects
      :param bool separate: Whether to classify and return separate segments
          of imports based on classification.
      :param bool import_before_from: Whether to sort `import ...` imports before
          `from ...` imports.
  
      For example:
          from os import path
          from aspy import refactor_imports
          import sys
          import pyramid
  
      separate = True, import_before_from = True
  
          import sys
          from os import path
  
          import pyramid
  
          from aspy import refactor_imports
  
      separate = True, import_before_from = False
  
          from os import path
          import sys
  
          import pyramid
  
          from aspy import refactor_imports
  
      separate = False, import_before_from = True
  
          import pyramid
          import sys
          from aspy import refactor_imports
          from os import path
  
      separate = False, import_before_from = False
  
          from aspy import refactor_imports
          from os import path
          import pyramid
          import sys
      """
      if separate:
          def classify_func(obj):
              return classify_import(
                  obj.import_statement.module, **classify_kwargs
              )
          types = ImportType.__all__
      else:
          # A little cheaty, this allows future imports to sort before others
          def classify_func(obj):
              return classify_import(
                  obj.import_statement.module, **classify_kwargs
              ) == ImportType.FUTURE
          types = [True, False]
  
      if import_before_from:
          def sort_within(obj):
              return (CLS_TO_INDEX[type(obj)],) + obj.sort_key
      else:
          def sort_within(obj):
              return tuple(obj.sort_key)
  
      # Partition the imports
      imports_partitioned = collections.defaultdict(list)
      for import_obj in imports:
          imports_partitioned[classify_func(import_obj)].append(import_obj)
  
      # sort each of the segments
      for segment_key, val in imports_partitioned.items():
          imports_partitioned[segment_key] = sorted(val, key=sort_within)
  
      return tuple(
          tuple(imports_partitioned[key])
          for key in types if key in imports_partitioned
      )