<s> """<STR_LIT>""" <EOL> from nose . tools import * <EOL> from networkx import * <EOL> from networkx . convert import * <EOL> from networkx . algorithms . operators import * <EOL> from networkx . generators . classic import barbell_graph , cycle_graph <EOL> class TestConvert ( ) : <EOL> def edgelists_equal ( self , e1 , e2 ) : <EOL> return sorted ( sorted ( e ) for e in e1 ) == sorted ( sorted ( e ) for e in e2 ) <EOL> def test_simple_graphs ( self ) : <EOL> for dest , source in [ ( to_dict_of_dicts , from_dict_of_dicts ) , <EOL> ( to_dict_of_lists , from_dict_of_lists ) ] : <EOL> G = barbell_graph ( <NUM_LIT:10> , <NUM_LIT:3> ) <EOL> dod = dest ( G ) <EOL> GG = source ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = Graph ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> P4 = path_graph ( <NUM_LIT:4> ) <EOL> P3 = path_graph ( <NUM_LIT:3> ) <EOL> dod = dest ( P4 , nodelist = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> Gdod = Graph ( dod ) <EOL> assert_equal ( sorted ( Gdod . nodes ( ) ) , sorted ( P3 . nodes ( ) ) ) <EOL> assert_equal ( sorted ( Gdod . edges ( ) ) , sorted ( P3 . edges ( ) ) ) <EOL> def test_digraphs ( self ) : <EOL> for dest , source in [ ( to_dict_of_dicts , from_dict_of_dicts ) , <EOL> ( to_dict_of_lists , from_dict_of_lists ) ] : <EOL> G = cycle_graph ( <NUM_LIT:10> ) <EOL> dod = dest ( G ) <EOL> GG = source ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = Graph ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> G = cycle_graph ( <NUM_LIT:10> , create_using = DiGraph ( ) ) <EOL> dod = dest ( G ) <EOL> GG = source ( dod , create_using = DiGraph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dod , create_using = DiGraph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = DiGraph ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> def test_graph ( self ) : <EOL> G = cycle_graph ( <NUM_LIT:10> ) <EOL> e = G . edges ( ) <EOL> source = [ u for u , v in e ] <EOL> dest = [ v for u , v in e ] <EOL> ex = zip ( source , dest , source ) <EOL> G = Graph ( ) <EOL> G . add_weighted_edges_from ( ex ) <EOL> dod = to_dict_of_dicts ( G ) <EOL> GG = from_dict_of_dicts ( dod , create_using = Graph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dod , create_using = Graph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = Graph ( dod ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> dol = to_dict_of_lists ( G ) <EOL> GG = from_dict_of_lists ( dol , create_using = Graph ( ) ) <EOL> enone = [ ( u , v , { } ) for ( u , v , d ) in G . edges ( data = True ) ] <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( enone , sorted ( GG . edges ( data = True ) ) ) <EOL> GW = to_networkx_graph ( dol , create_using = Graph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( enone , sorted ( GW . edges ( data = True ) ) ) <EOL> GI = Graph ( dol ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( enone , sorted ( GI . edges ( data = True ) ) ) <EOL> def test_with_multiedges_self_loops ( self ) : <EOL> G = cycle_graph ( <NUM_LIT:10> ) <EOL> e = G . edges ( ) <EOL> source , dest = list ( zip ( * e ) ) <EOL> ex = list ( zip ( source , dest , source ) ) <EOL> XG = Graph ( ) <EOL> XG . add_weighted_edges_from ( ex ) <EOL> XGM = MultiGraph ( ) <EOL> XGM . add_weighted_edges_from ( ex ) <EOL> XGM . add_edge ( <NUM_LIT:0> , <NUM_LIT:1> , weight = <NUM_LIT:2> ) <EOL> XGS = Graph ( ) <EOL> XGS . add_weighted_edges_from ( ex ) <EOL> XGS . add_edge ( <NUM_LIT:0> , <NUM_LIT:0> , weight = <NUM_LIT:100> ) <EOL> dod = to_dict_of_dicts ( XGS ) <EOL> GG = from_dict_of_dicts ( dod , create_using = Graph ( ) ) <EOL> assert_equal ( sorted ( XGS . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( XGS . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dod , create_using = Graph ( ) ) <EOL> assert_equal ( sorted ( XGS . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( XGS . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = Graph ( dod ) <EOL> assert_equal ( sorted ( XGS . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( XGS . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> dol = to_dict_of_lists ( XGS ) <EOL> GG = from_dict_of_lists ( dol , create_using = Graph ( ) ) <EOL> enone = [ ( u , v , { } ) for ( u , v , d ) in XGS . edges ( data = True ) ] <EOL> assert_equal ( sorted ( XGS . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( enone , sorted ( GG . edges ( data = True ) ) ) <EOL> GW = to_networkx_graph ( dol , create_using = Graph ( ) ) <EOL> assert_equal ( sorted ( XGS . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( enone , sorted ( GW . edges ( data = True ) ) ) <EOL> GI = Graph ( dol ) <EOL> assert_equal ( sorted ( XGS . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( enone , sorted ( GI . edges ( data = True ) ) ) <EOL> dod = to_dict_of_dicts ( XGM ) <EOL> GG = from_dict_of_dicts ( dod , create_using = MultiGraph ( ) , <EOL> multigraph_input = True ) <EOL> assert_equal ( sorted ( XGM . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( XGM . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dod , create_using = MultiGraph ( ) , multigraph_input = True ) <EOL> assert_equal ( sorted ( XGM . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( XGM . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = MultiGraph ( dod ) <EOL> assert_equal ( sorted ( XGM . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_false ( sorted ( XGM . edges ( ) ) == sorted ( GI . edges ( ) ) ) <EOL> GE = from_dict_of_dicts ( dod , create_using = MultiGraph ( ) , <EOL> multigraph_input = False ) <EOL> assert_equal ( sorted ( XGM . nodes ( ) ) , sorted ( GE . nodes ( ) ) ) <EOL> assert_not_equal ( sorted ( XGM . edges ( ) ) , sorted ( GE . edges ( ) ) ) <EOL> GI = MultiGraph ( XGM ) <EOL> assert_equal ( sorted ( XGM . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( XGM . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> GM = MultiGraph ( G ) <EOL> assert_equal ( sorted ( GM . nodes ( ) ) , sorted ( G . nodes ( ) ) ) <EOL> assert_equal ( sorted ( GM . edges ( ) ) , sorted ( G . edges ( ) ) ) <EOL> dol = to_dict_of_lists ( G ) <EOL> GG = from_dict_of_lists ( dol , create_using = MultiGraph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GG . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GG . edges ( ) ) ) <EOL> GW = to_networkx_graph ( dol , create_using = MultiGraph ( ) ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GW . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GW . edges ( ) ) ) <EOL> GI = MultiGraph ( dol ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( GI . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( GI . edges ( ) ) ) <EOL> def test_edgelists ( self ) : <EOL> P = path_graph ( <NUM_LIT:4> ) <EOL> e = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] <EOL> G = Graph ( e ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( P . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( P . edges ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( data = True ) ) , sorted ( P . edges ( data = True ) ) ) <EOL> e = [ ( <NUM_LIT:0> , <NUM_LIT:1> , { } ) , ( <NUM_LIT:1> , <NUM_LIT:2> , { } ) , ( <NUM_LIT:2> , <NUM_LIT:3> , { } ) ] <EOL> G = Graph ( e ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( P . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( P . edges ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( data = True ) ) , sorted ( P . edges ( data = True ) ) ) <EOL> e = ( ( n , n + <NUM_LIT:1> ) for n in range ( <NUM_LIT:3> ) ) <EOL> G = Graph ( e ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( P . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( P . edges ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( data = True ) ) , sorted ( P . edges ( data = True ) ) ) <EOL> def test_directed_to_undirected ( self ) : <EOL> edges1 = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:0> ) ] <EOL> edges2 = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:2> ) ] <EOL> assert_true ( self . edgelists_equal ( nx . Graph ( nx . DiGraph ( edges1 ) ) . edges ( ) , edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . Graph ( nx . DiGraph ( edges2 ) ) . edges ( ) , edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . MultiGraph ( nx . DiGraph ( edges1 ) ) . edges ( ) , edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . MultiGraph ( nx . DiGraph ( edges2 ) ) . edges ( ) , edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . MultiGraph ( nx . MultiDiGraph ( edges1 ) ) . edges ( ) , <EOL> edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . MultiGraph ( nx . MultiDiGraph ( edges2 ) ) . edges ( ) , <EOL> edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . Graph ( nx . MultiDiGraph ( edges1 ) ) . edges ( ) , edges1 ) ) <EOL> assert_true ( self . edgelists_equal ( nx . Graph ( nx . MultiDiGraph ( edges2 ) ) . edges ( ) , edges1 ) ) </s>
