<s> """<STR_LIT>""" <EOL> from ... utils . log import logger <EOL> from . graphs import Edge <EOL> class EdgeVisitor ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> def visit ( self , edge ) : <EOL> pass <EOL> class Walker ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph , visitor , backwards = False ) : <EOL> self . _graph = graph <EOL> self . _visitor = visitor <EOL> self . _backwards = backwards <EOL> self . worklist = None <EOL> @ property <EOL> def graph ( self ) : <EOL> return self . _graph <EOL> @ graph . setter <EOL> def graph ( self , value ) : <EOL> self . _graph = value <EOL> @ property <EOL> def visitor ( self ) : <EOL> return self . _visitor <EOL> @ visitor . setter <EOL> def visitor ( self , value ) : <EOL> self . _visitor = value <EOL> def traverse ( self , root ) : <EOL> self . worklist = [ ] <EOL> self . __run ( root ) <EOL> def __run ( self , root = None ) : <EOL> visited = set ( ) <EOL> if root is not None : <EOL> self . __process ( root ) <EOL> while self . worklist : <EOL> current = self . worklist . pop ( <NUM_LIT:0> ) <EOL> if current in visited : <EOL> continue <EOL> self . __process ( current ) <EOL> visited . add ( current ) <EOL> def __process ( self , current ) : <EOL> cur_node = None <EOL> if isinstance ( current , Edge ) : <EOL> cur_node = current . dest if not self . _backwards else current . source <EOL> self . visitor . visit ( current ) <EOL> else : <EOL> cur_node = current <EOL> list_edges = self . graph . out_edges ( cur_node ) if not self . _backwards else self . graph . in_edges ( cur_node ) <EOL> for edge in list_edges : <EOL> self . worklist . insert ( <NUM_LIT:0> , edge ) <EOL> def dfs_postorder_nodes ( graph , root ) : <EOL> import sys <EOL> sys . setrecursionlimit ( <NUM_LIT> ) <EOL> visited = set ( ) <EOL> def _dfs ( node , _visited ) : <EOL> _visited . add ( node ) <EOL> for edge in graph . out_edges ( node ) : <EOL> dest_node = edge . dest <EOL> if dest_node not in _visited : <EOL> for child in _dfs ( dest_node , _visited ) : <EOL> yield child <EOL> yield node <EOL> return [ n for n in _dfs ( root , visited ) ] </s>
