nodes: 0, module; 1, function_definition; 2, function_name:topological_sort; 3, parameters; 4, block; 5, identifier:unsorted_dict; 6, expression_statement; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, while_statement; 12, return_statement; 13, comment:"""Sort objects by dependency.
  
      Sort a dict of obsoleting PID to obsoleted PID to a list of PIDs in order of
      obsolescence.
  
      Args:
        unsorted_dict : dict
          Dict that holds obsolescence information. Each ``key/value`` pair establishes
          that the PID in ``key`` identifies an object that obsoletes an object identifies
          by the PID in ``value``.
  
  
      Returns:
        tuple of sorted_list, unconnected_dict :
  
        ``sorted_list``: A list of PIDs ordered so that all PIDs that obsolete an object
        are listed after the object they obsolete.
  
        ``unconnected_dict``: A dict of PID to obsoleted PID of any objects that could not
        be added to a revision chain. These items will have obsoletes PIDs that directly
        or indirectly reference a PID that could not be sorted.
  
      Notes:
        ``obsoletes_dict`` is modified by the sort and on return holds any items that
        could not be sorted.
  
        The sort works by repeatedly iterating over an unsorted list of PIDs and
        moving PIDs to the sorted list as they become available. A PID is available to
        be moved to the sorted list if it does not obsolete a PID or if the PID it
        obsoletes is already in the sorted list.
  
      """; 14, assignment; 15, assignment; 16, assignment; 17, assignment; 18, identifier:found; 19, block; 20, expression_list; 21, identifier:sorted_list; 22, list; 23, identifier:sorted_set; 24, call; 25, identifier:found; 26, True; 27, identifier:unconnected_dict; 28, call; 29, expression_statement; 30, for_statement; 31, identifier:sorted_list; 32, identifier:unconnected_dict; 33, identifier:set; 34, argument_list; 35, attribute; 36, argument_list; 37, assignment; 38, pattern_list; 39, call; 40, block; 41, identifier:unsorted_dict; 42, identifier:copy; 43, identifier:found; 44, False; 45, identifier:pid; 46, identifier:obsoletes_pid; 47, identifier:list; 48, argument_list; 49, if_statement; 50, call; 51, boolean_operator; 52, block; 53, attribute; 54, argument_list; 55, comparison_operator:obsoletes_pid is None; 56, comparison_operator:obsoletes_pid in sorted_set; 57, expression_statement; 58, expression_statement; 59, expression_statement; 60, delete_statement; 61, identifier:unconnected_dict; 62, identifier:items; 63, identifier:obsoletes_pid; 64, None; 65, identifier:obsoletes_pid; 66, identifier:sorted_set; 67, assignment; 68, call; 69, call; 70, subscript; 71, identifier:found; 72, True; 73, attribute; 74, argument_list; 75, attribute; 76, argument_list; 77, identifier:unconnected_dict; 78, identifier:pid; 79, identifier:sorted_list; 80, identifier:append; 81, identifier:pid; 82, identifier:sorted_set; 83, identifier:add; 84, identifier:pid
code: def topological_sort(unsorted_dict):
      """Sort objects by dependency.
  
      Sort a dict of obsoleting PID to obsoleted PID to a list of PIDs in order of
      obsolescence.
  
      Args:
        unsorted_dict : dict
          Dict that holds obsolescence information. Each ``key/value`` pair establishes
          that the PID in ``key`` identifies an object that obsoletes an object identifies
          by the PID in ``value``.
  
  
      Returns:
        tuple of sorted_list, unconnected_dict :
  
        ``sorted_list``: A list of PIDs ordered so that all PIDs that obsolete an object
        are listed after the object they obsolete.
  
        ``unconnected_dict``: A dict of PID to obsoleted PID of any objects that could not
        be added to a revision chain. These items will have obsoletes PIDs that directly
        or indirectly reference a PID that could not be sorted.
  
      Notes:
        ``obsoletes_dict`` is modified by the sort and on return holds any items that
        could not be sorted.
  
        The sort works by repeatedly iterating over an unsorted list of PIDs and
        moving PIDs to the sorted list as they become available. A PID is available to
        be moved to the sorted list if it does not obsolete a PID or if the PID it
        obsoletes is already in the sorted list.
  
      """
      sorted_list = []
      sorted_set = set()
      found = True
      unconnected_dict = unsorted_dict.copy()
      while found:
          found = False
          for pid, obsoletes_pid in list(unconnected_dict.items()):
              if obsoletes_pid is None or obsoletes_pid in sorted_set:
                  found = True
                  sorted_list.append(pid)
                  sorted_set.add(pid)
                  del unconnected_dict[pid]
      return sorted_list, unconnected_dict