<s> """<STR_LIT>""" <EOL> import networkx as nx <EOL> from piped import util <EOL> class DirectedGraphWithOrderedEdges ( nx . DiGraph ) : <EOL> """<STR_LIT>""" <EOL> ordered_dictionary_factory = util . OrderedDictionary <EOL> def __init__ ( self ) : <EOL> super ( DirectedGraphWithOrderedEdges , self ) . __init__ ( ) <EOL> self . adj = self . edge = self . succ = self . ordered_dictionary_factory ( ) <EOL> self . pred = self . ordered_dictionary_factory ( ) <EOL> self . node = self . ordered_dictionary_factory ( ) <EOL> def _ensure_ordered_dicts_for_node ( self , node ) : <EOL> self . succ . setdefault ( node , self . ordered_dictionary_factory ( ) ) <EOL> self . pred . setdefault ( node , self . ordered_dictionary_factory ( ) ) <EOL> self . node . setdefault ( node , dict ( ) ) <EOL> def add_node ( self , node , * a , ** kw ) : <EOL> self . _ensure_ordered_dicts_for_node ( node ) <EOL> super ( DirectedGraphWithOrderedEdges , self ) . add_node ( node , * a , ** kw ) <EOL> def add_nodes_from ( self , nodes , ** kw ) : <EOL> for node in nodes : <EOL> self . _ensure_ordered_dicts_for_node ( node ) <EOL> super ( DirectedGraphWithOrderedEdges , self ) . add_nodes_from ( nodes , ** kw ) <EOL> def add_edge ( self , u , v , attr_dict = None , ** attr ) : <EOL> self . _ensure_ordered_dicts_for_node ( u ) <EOL> self . _ensure_ordered_dicts_for_node ( v ) <EOL> super ( DirectedGraphWithOrderedEdges , self ) . add_edge ( u , v , attr_dict , ** attr ) <EOL> def add_edges_from ( self , ebunch , * a , ** kw ) : <EOL> for u , v , _ in ebunch : <EOL> self . _ensure_ordered_dicts_for_node ( u ) <EOL> self . _ensure_ordered_dicts_for_node ( v ) <EOL> super ( DirectedGraphWithOrderedEdges , self ) . add_edges_from ( ebunch , * a , ** kw ) <EOL> def replace_edge_from ( self , from_node , old_to , new_to , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_edge ( from_node , old_to ) : <EOL> raise ValueError ( '<STR_LIT>' % ( from_node , old_to ) ) <EOL> if self . has_edge ( from_node , new_to ) : <EOL> raise ValueError ( '<STR_LIT>' % ( from_node , new_to ) ) <EOL> self . _ensure_ordered_dicts_for_node ( new_to ) <EOL> self . succ [ from_node ] . replace_key ( old_to , new_to , kw ) <EOL> self . pred [ new_to ] [ from_node ] = kw <EOL> del self . pred [ old_to ] [ from_node ] </s>
