nodes: 0, module; 1, function_definition; 2, function_name:topological; 3, parameters; 4, block; 5, identifier:nodes; 6, expression_statement; 7, expression_statement; 8, function_definition; 9, while_statement; 10, return_statement; 11, comment:"""Return nodes in a topological order."""; 12, assignment; 13, function_name:dfs; 14, parameters; 15, block; 16, identifier:enter; 17, block; 18, identifier:order; 19, pattern_list; 20, expression_list; 21, identifier:node; 22, expression_statement; 23, expression_statement; 24, for_statement; 25, expression_statement; 26, expression_statement; 27, expression_statement; 28, identifier:order; 29, identifier:enter; 30, identifier:state; 31, call; 32, call; 33, dictionary; 34, comment:"""Visit nodes in depth-first order."""; 35, assignment; 36, identifier:parent; 37, call; 38, block; 39, call; 40, assignment; 41, call; 42, identifier:deque; 43, argument_list; 44, identifier:set; 45, argument_list; 46, subscript; 47, identifier:GRAY; 48, attribute; 49, argument_list; 50, expression_statement; 51, if_statement; 52, if_statement; 53, expression_statement; 54, expression_statement; 55, attribute; 56, argument_list; 57, subscript; 58, identifier:BLACK; 59, identifier:dfs; 60, argument_list; 61, identifier:nodes; 62, identifier:state; 63, identifier:node; 64, identifier:nodes; 65, identifier:get; 66, identifier:node; 67, tuple; 68, assignment; 69, comparison_operator:color == GRAY; 70, block; 71, comparison_operator:color == BLACK; 72, block; 73, call; 74, call; 75, identifier:order; 76, identifier:appendleft; 77, identifier:node; 78, identifier:state; 79, identifier:node; 80, call; 81, identifier:color; 82, call; 83, identifier:color; 84, identifier:GRAY; 85, raise_statement; 86, identifier:color; 87, identifier:BLACK; 88, continue_statement; 89, attribute; 90, argument_list; 91, identifier:dfs; 92, argument_list; 93, attribute; 94, argument_list; 95, attribute; 96, argument_list; 97, call; 98, identifier:enter; 99, identifier:discard; 100, identifier:parent; 101, identifier:parent; 102, identifier:enter; 103, identifier:pop; 104, identifier:state; 105, identifier:get; 106, identifier:parent; 107, None; 108, identifier:ValueError; 109, argument_list; 110, string; 111, string_content:cycle
code: def topological(nodes):
      """Return nodes in a topological order."""
      order, enter, state = deque(), set(nodes), {}
  
      def dfs(node):
          """Visit nodes in depth-first order."""
          state[node] = GRAY
          for parent in nodes.get(node, ()):
              color = state.get(parent, None)
              if color == GRAY:
                  raise ValueError('cycle')
              if color == BLACK:
                  continue
              enter.discard(parent)
              dfs(parent)
          order.appendleft(node)
          state[node] = BLACK
  
      while enter:
          dfs(enter.pop())
  
      return order