<s> """<STR_LIT>""" <EOL> from nose . tools import assert_equal , assert_raises , assert_not_equal <EOL> import io <EOL> import tempfile <EOL> import os <EOL> import networkx as nx <EOL> from networkx . testing import ( assert_edges_equal , assert_nodes_equal , <EOL> assert_graphs_equal ) <EOL> class TestEdgelist : <EOL> def setUp ( self ) : <EOL> self . G = nx . Graph ( name = "<STR_LIT:test>" ) <EOL> e = [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) , ( '<STR_LIT:c>' , '<STR_LIT:d>' ) , ( '<STR_LIT:d>' , '<STR_LIT:e>' ) , ( '<STR_LIT:e>' , '<STR_LIT:f>' ) , ( '<STR_LIT:a>' , '<STR_LIT:f>' ) ] <EOL> self . G . add_edges_from ( e ) <EOL> self . G . add_node ( '<STR_LIT:g>' ) <EOL> self . DG = nx . DiGraph ( self . G ) <EOL> self . XG = nx . MultiGraph ( ) <EOL> self . XG . add_weighted_edges_from ( [ ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:1> ) , ( <NUM_LIT:3> , <NUM_LIT:3> , <NUM_LIT> ) ] ) <EOL> self . XDG = nx . MultiDiGraph ( self . XG ) <EOL> def test_read_edgelist_1 ( self ) : <EOL> s = b"""<STR_LIT>""" <EOL> bytesIO = io . BytesIO ( s ) <EOL> G = nx . read_edgelist ( bytesIO , nodetype = int ) <EOL> assert_edges_equal ( G . edges ( ) , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_read_edgelist_2 ( self ) : <EOL> s = b"""<STR_LIT>""" <EOL> bytesIO = io . BytesIO ( s ) <EOL> G = nx . read_edgelist ( bytesIO , nodetype = int , data = False ) <EOL> assert_edges_equal ( G . edges ( ) , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> bytesIO = io . BytesIO ( s ) <EOL> G = nx . read_weighted_edgelist ( bytesIO , nodetype = int ) <EOL> assert_edges_equal ( G . edges ( data = True ) , <EOL> [ ( <NUM_LIT:1> , <NUM_LIT:2> , { '<STR_LIT>' : <NUM_LIT> } ) , ( <NUM_LIT:2> , <NUM_LIT:3> , { '<STR_LIT>' : <NUM_LIT> } ) ] ) <EOL> def test_read_edgelist_3 ( self ) : <EOL> s = b"""<STR_LIT>""" <EOL> bytesIO = io . BytesIO ( s ) <EOL> G = nx . read_edgelist ( bytesIO , nodetype = int , data = False ) <EOL> assert_edges_equal ( G . edges ( ) , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> bytesIO = io . BytesIO ( s ) <EOL> G = nx . read_edgelist ( bytesIO , nodetype = int , data = True ) <EOL> assert_edges_equal ( G . edges ( data = True ) , <EOL> [ ( <NUM_LIT:1> , <NUM_LIT:2> , { '<STR_LIT>' : <NUM_LIT> } ) , ( <NUM_LIT:2> , <NUM_LIT:3> , { '<STR_LIT>' : <NUM_LIT> } ) ] ) <EOL> def test_write_edgelist_1 ( self ) : <EOL> fh = io . BytesIO ( ) <EOL> G = nx . Graph ( ) <EOL> G . add_edges_from ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> nx . write_edgelist ( G , fh , data = False ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> assert_equal ( fh . read ( ) , b"<STR_LIT>" ) <EOL> def test_write_edgelist_2 ( self ) : <EOL> fh = io . BytesIO ( ) <EOL> G = nx . Graph ( ) <EOL> G . add_edges_from ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> nx . write_edgelist ( G , fh , data = True ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> assert_equal ( fh . read ( ) , b"<STR_LIT>" ) <EOL> def test_write_edgelist_3 ( self ) : <EOL> fh = io . BytesIO ( ) <EOL> G = nx . Graph ( ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , weight = <NUM_LIT> ) <EOL> G . add_edge ( <NUM_LIT:2> , <NUM_LIT:3> , weight = <NUM_LIT> ) <EOL> nx . write_edgelist ( G , fh , data = True ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> assert_equal ( fh . read ( ) , b"<STR_LIT>" ) <EOL> def test_write_edgelist_4 ( self ) : <EOL> fh = io . BytesIO ( ) <EOL> G = nx . Graph ( ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , weight = <NUM_LIT> ) <EOL> G . add_edge ( <NUM_LIT:2> , <NUM_LIT:3> , weight = <NUM_LIT> ) <EOL> nx . write_edgelist ( G , fh , data = [ ( '<STR_LIT>' ) ] ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> assert_equal ( fh . read ( ) , b"<STR_LIT>" ) <EOL> def test_unicode ( self ) : <EOL> G = nx . Graph ( ) <EOL> try : <EOL> name1 = chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) <EOL> name2 = chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) <EOL> except ValueError : <EOL> name1 = unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) <EOL> name2 = unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) <EOL> G . add_edge ( name1 , '<STR_LIT>' , attr_dict = { name2 : <NUM_LIT:3> } ) <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname ) <EOL> assert_graphs_equal ( G , H ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_latin1_issue ( self ) : <EOL> G = nx . Graph ( ) <EOL> try : <EOL> name1 = chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) <EOL> name2 = chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) + chr ( <NUM_LIT> ) <EOL> except ValueError : <EOL> name1 = unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) <EOL> name2 = unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) + unichr ( <NUM_LIT> ) <EOL> G . add_edge ( name1 , '<STR_LIT>' , attr_dict = { name2 : <NUM_LIT:3> } ) <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> assert_raises ( UnicodeEncodeError , <EOL> nx . write_edgelist , <EOL> G , fname , encoding = '<STR_LIT>' ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_latin1 ( self ) : <EOL> G = nx . Graph ( ) <EOL> try : <EOL> blurb = chr ( <NUM_LIT> ) <EOL> name1 = '<STR_LIT>' + chr ( <NUM_LIT> ) + '<STR_LIT>' <EOL> name2 = chr ( <NUM_LIT> ) + '<STR_LIT>' <EOL> except ValueError : <EOL> name1 = '<STR_LIT>' + unichr ( <NUM_LIT> ) + '<STR_LIT>' <EOL> name2 = unichr ( <NUM_LIT> ) + '<STR_LIT>' <EOL> G . add_edge ( name1 , '<STR_LIT>' , attr_dict = { name2 : <NUM_LIT:3> } ) <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname , encoding = '<STR_LIT>' ) <EOL> H = nx . read_edgelist ( fname , encoding = '<STR_LIT>' ) <EOL> assert_graphs_equal ( G , H ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_edgelist_graph ( self ) : <EOL> G = self . G <EOL> ( fd , fname ) = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname ) <EOL> H2 = nx . read_edgelist ( fname ) <EOL> assert_not_equal ( H , H2 ) <EOL> G . remove_node ( '<STR_LIT:g>' ) <EOL> assert_nodes_equal ( list ( H ) , list ( G ) ) <EOL> assert_edges_equal ( list ( H . edges ( ) ) , list ( G . edges ( ) ) ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_edgelist_digraph ( self ) : <EOL> G = self . DG <EOL> ( fd , fname ) = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname , create_using = nx . DiGraph ( ) ) <EOL> H2 = nx . read_edgelist ( fname , create_using = nx . DiGraph ( ) ) <EOL> assert_not_equal ( H , H2 ) <EOL> G . remove_node ( '<STR_LIT:g>' ) <EOL> assert_nodes_equal ( list ( H ) , list ( G ) ) <EOL> assert_edges_equal ( list ( H . edges ( ) ) , list ( G . edges ( ) ) ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_edgelist_integers ( self ) : <EOL> G = nx . convert_node_labels_to_integers ( self . G ) <EOL> ( fd , fname ) = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname , nodetype = int ) <EOL> G . remove_nodes_from ( list ( nx . isolates ( G ) ) ) <EOL> assert_nodes_equal ( list ( H ) , list ( G ) ) <EOL> assert_edges_equal ( list ( H . edges ( ) ) , list ( G . edges ( ) ) ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_edgelist_digraph ( self ) : <EOL> G = self . DG <EOL> ( fd , fname ) = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname , create_using = nx . DiGraph ( ) ) <EOL> G . remove_node ( '<STR_LIT:g>' ) <EOL> H2 = nx . read_edgelist ( fname , create_using = nx . DiGraph ( ) ) <EOL> assert_not_equal ( H , H2 ) <EOL> assert_nodes_equal ( list ( H ) , list ( G ) ) <EOL> assert_edges_equal ( list ( H . edges ( ) ) , list ( G . edges ( ) ) ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_edgelist_multigraph ( self ) : <EOL> G = self . XG <EOL> ( fd , fname ) = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname , nodetype = int , create_using = nx . MultiGraph ( ) ) <EOL> H2 = nx . read_edgelist ( fname , nodetype = int , create_using = nx . MultiGraph ( ) ) <EOL> assert_not_equal ( H , H2 ) <EOL> assert_nodes_equal ( list ( H ) , list ( G ) ) <EOL> assert_edges_equal ( list ( H . edges ( ) ) , list ( G . edges ( ) ) ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) <EOL> def test_edgelist_multidigraph ( self ) : <EOL> G = self . XDG <EOL> ( fd , fname ) = tempfile . mkstemp ( ) <EOL> nx . write_edgelist ( G , fname ) <EOL> H = nx . read_edgelist ( fname , nodetype = int , create_using = nx . MultiDiGraph ( ) ) <EOL> H2 = nx . read_edgelist ( fname , nodetype = int , create_using = nx . MultiDiGraph ( ) ) <EOL> assert_not_equal ( H , H2 ) <EOL> assert_nodes_equal ( list ( H ) , list ( G ) ) <EOL> assert_edges_equal ( list ( H . edges ( ) ) , list ( G . edges ( ) ) ) <EOL> os . close ( fd ) <EOL> os . unlink ( fname ) </s>
