nodes: 0, module; 1, function_definition; 2, function_name:order_dict_by; 3, parameters; 4, block; 5, identifier:dict_; 6, identifier:key_order; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, expression_statement; 12, return_statement; 13, comment:r"""
      Reorders items in a dictionary according to a custom key order
  
      Args:
          dict_ (dict_):  a dictionary
          key_order (list): custom key order
  
      Returns:
          OrderedDict: sorted_dict
  
      CommandLine:
          python -m utool.util_dict --exec-order_dict_by
  
      Example:
          >>> # ENABLE_DOCTEST
          >>> from utool.util_dict import *  # NOQA
          >>> import utool as ut
          >>> dict_ = {1: 1, 2: 2, 3: 3, 4: 4}
          >>> key_order = [4, 2, 3, 1]
          >>> sorted_dict = order_dict_by(dict_, key_order)
          >>> result = ('sorted_dict = %s' % (ut.repr4(sorted_dict, nl=False),))
          >>> print(result)
          >>> assert result == 'sorted_dict = {4: 4, 2: 2, 3: 3, 1: 1}'
  
      """; 14, assignment; 15, assignment; 16, assignment; 17, assignment; 18, identifier:sorted_dict; 19, identifier:dict_keys; 20, call; 21, identifier:other_keys; 22, binary_operator:dict_keys - set(key_order); 23, identifier:key_order; 24, call; 25, identifier:sorted_dict; 26, call; 27, identifier:set; 28, argument_list; 29, identifier:dict_keys; 30, call; 31, attribute; 32, argument_list; 33, identifier:OrderedDict; 34, generator_expression; 35, call; 36, identifier:set; 37, argument_list; 38, identifier:it; 39, identifier:chain; 40, identifier:key_order; 41, identifier:other_keys; 42, tuple; 43, for_in_clause; 44, if_clause; 45, attribute; 46, argument_list; 47, identifier:key_order; 48, identifier:key; 49, subscript; 50, identifier:key; 51, identifier:key_order; 52, comparison_operator:key in dict_keys; 53, identifier:dict_; 54, identifier:keys; 55, identifier:dict_; 56, identifier:key; 57, identifier:key; 58, identifier:dict_keys
code: def order_dict_by(dict_, key_order):
      r"""
      Reorders items in a dictionary according to a custom key order
  
      Args:
          dict_ (dict_):  a dictionary
          key_order (list): custom key order
  
      Returns:
          OrderedDict: sorted_dict
  
      CommandLine:
          python -m utool.util_dict --exec-order_dict_by
  
      Example:
          >>> # ENABLE_DOCTEST
          >>> from utool.util_dict import *  # NOQA
          >>> import utool as ut
          >>> dict_ = {1: 1, 2: 2, 3: 3, 4: 4}
          >>> key_order = [4, 2, 3, 1]
          >>> sorted_dict = order_dict_by(dict_, key_order)
          >>> result = ('sorted_dict = %s' % (ut.repr4(sorted_dict, nl=False),))
          >>> print(result)
          >>> assert result == 'sorted_dict = {4: 4, 2: 2, 3: 3, 1: 1}'
  
      """
      dict_keys = set(dict_.keys())
      other_keys = dict_keys - set(key_order)
      key_order = it.chain(key_order, other_keys)
      sorted_dict = OrderedDict(
          (key, dict_[key]) for key in key_order if key in dict_keys
      )
      return sorted_dict