<s> import io <EOL> import time <EOL> from nose import SkipTest <EOL> from nose . tools import * <EOL> import networkx as nx <EOL> class TestGEXF ( object ) : <EOL> @ classmethod <EOL> def setupClass ( cls ) : <EOL> try : <EOL> import xml . etree . ElementTree <EOL> except ImportError : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> def setUp ( self ) : <EOL> self . simple_directed_data = """<STR_LIT>""" <EOL> self . simple_directed_graph = nx . DiGraph ( ) <EOL> self . simple_directed_graph . add_node ( '<STR_LIT:0>' , label = '<STR_LIT>' ) <EOL> self . simple_directed_graph . add_node ( '<STR_LIT:1>' , label = '<STR_LIT>' ) <EOL> self . simple_directed_graph . add_edge ( '<STR_LIT:0>' , '<STR_LIT:1>' , id = '<STR_LIT:0>' ) <EOL> self . simple_directed_fh = io . BytesIO ( self . simple_directed_data . encode ( '<STR_LIT>' ) ) <EOL> self . attribute_data = """<STR_LIT>""" <EOL> self . attribute_graph = nx . DiGraph ( ) <EOL> self . attribute_graph . graph [ '<STR_LIT>' ] = { '<STR_LIT>' : True } <EOL> self . attribute_graph . add_node ( '<STR_LIT:0>' , <EOL> label = '<STR_LIT>' , <EOL> url = '<STR_LIT>' , <EOL> indegree = <NUM_LIT:1> , frog = False ) <EOL> self . attribute_graph . add_node ( '<STR_LIT:1>' , <EOL> label = '<STR_LIT>' , <EOL> url = '<STR_LIT>' , <EOL> indegree = <NUM_LIT:2> , frog = False ) <EOL> self . attribute_graph . add_node ( '<STR_LIT:2>' , <EOL> label = '<STR_LIT>' , <EOL> url = '<STR_LIT>' , <EOL> indegree = <NUM_LIT:1> , frog = True ) <EOL> self . attribute_graph . add_node ( '<STR_LIT:3>' , <EOL> label = '<STR_LIT>' , <EOL> url = '<STR_LIT>' , <EOL> indegree = <NUM_LIT:1> , frog = True ) <EOL> self . attribute_graph . add_edge ( '<STR_LIT:0>' , '<STR_LIT:1>' , id = '<STR_LIT:0>' ) <EOL> self . attribute_graph . add_edge ( '<STR_LIT:0>' , '<STR_LIT:2>' , id = '<STR_LIT:1>' ) <EOL> self . attribute_graph . add_edge ( '<STR_LIT:1>' , '<STR_LIT:0>' , id = '<STR_LIT:2>' ) <EOL> self . attribute_graph . add_edge ( '<STR_LIT:2>' , '<STR_LIT:1>' , id = '<STR_LIT:3>' ) <EOL> self . attribute_graph . add_edge ( '<STR_LIT:0>' , '<STR_LIT:3>' , id = '<STR_LIT:4>' ) <EOL> self . attribute_fh = io . BytesIO ( self . attribute_data . encode ( '<STR_LIT>' ) ) <EOL> self . simple_undirected_data = """<STR_LIT>""" <EOL> self . simple_undirected_graph = nx . Graph ( ) <EOL> self . simple_undirected_graph . add_node ( '<STR_LIT:0>' , label = '<STR_LIT>' ) <EOL> self . simple_undirected_graph . add_node ( '<STR_LIT:1>' , label = '<STR_LIT>' ) <EOL> self . simple_undirected_graph . add_edge ( '<STR_LIT:0>' , '<STR_LIT:1>' , id = '<STR_LIT:0>' ) <EOL> self . simple_undirected_fh = io . BytesIO ( self . simple_undirected_data . encode ( '<STR_LIT>' ) ) <EOL> def test_read_simple_directed_graphml ( self ) : <EOL> G = self . simple_directed_graph <EOL> H = nx . read_gexf ( self . simple_directed_fh ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( H . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( H . edges ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( data = True ) ) , <EOL> sorted ( H . edges ( data = True ) ) ) <EOL> self . simple_directed_fh . seek ( <NUM_LIT:0> ) <EOL> def test_write_read_simple_directed_graphml ( self ) : <EOL> G = self . simple_directed_graph <EOL> fh = io . BytesIO ( ) <EOL> nx . write_gexf ( G , fh ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> H = nx . read_gexf ( fh ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( H . nodes ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( ) ) , sorted ( H . edges ( ) ) ) <EOL> assert_equal ( sorted ( G . edges ( data = True ) ) , <EOL> sorted ( H . edges ( data = True ) ) ) <EOL> self . simple_directed_fh . seek ( <NUM_LIT:0> ) <EOL> def test_read_simple_undirected_graphml ( self ) : <EOL> G = self . simple_undirected_graph <EOL> H = nx . read_gexf ( self . simple_undirected_fh ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( H . nodes ( ) ) ) <EOL> assert_equal ( <EOL> sorted ( sorted ( e ) for e in G . edges ( ) ) , <EOL> sorted ( sorted ( e ) for e in H . edges ( ) ) ) <EOL> self . simple_undirected_fh . seek ( <NUM_LIT:0> ) <EOL> def test_read_attribute_graphml ( self ) : <EOL> G = self . attribute_graph <EOL> H = nx . read_gexf ( self . attribute_fh ) <EOL> assert_equal ( sorted ( G . nodes ( True ) ) , sorted ( H . nodes ( data = True ) ) ) <EOL> ge = sorted ( G . edges ( data = True ) ) <EOL> he = sorted ( H . edges ( data = True ) ) <EOL> for a , b in zip ( ge , he ) : <EOL> assert_equal ( a , b ) <EOL> self . attribute_fh . seek ( <NUM_LIT:0> ) <EOL> def test_directed_edge_in_undirected ( self ) : <EOL> s = """<STR_LIT>""" <EOL> fh = io . BytesIO ( s . encode ( '<STR_LIT>' ) ) <EOL> assert_raises ( nx . NetworkXError , nx . read_gexf , fh ) <EOL> def test_undirected_edge_in_directed ( self ) : <EOL> s = """<STR_LIT>""" <EOL> fh = io . BytesIO ( s . encode ( '<STR_LIT>' ) ) <EOL> assert_raises ( nx . NetworkXError , nx . read_gexf , fh ) <EOL> def test_key_raises ( self ) : <EOL> s = """<STR_LIT>""" <EOL> fh = io . BytesIO ( s . encode ( '<STR_LIT>' ) ) <EOL> assert_raises ( nx . NetworkXError , nx . read_gexf , fh ) <EOL> def test_relabel ( self ) : <EOL> s = """<STR_LIT>""" <EOL> fh = io . BytesIO ( s . encode ( '<STR_LIT>' ) ) <EOL> G = nx . read_gexf ( fh , relabel = True ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def test_default_attribute ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_node ( <NUM_LIT:1> , label = '<STR_LIT:1>' , color = '<STR_LIT>' ) <EOL> nx . add_path ( G , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , foo = <NUM_LIT:3> ) <EOL> G . graph [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT>' } <EOL> G . graph [ '<STR_LIT>' ] = { '<STR_LIT:foo>' : <NUM_LIT:7> } <EOL> fh = io . BytesIO ( ) <EOL> nx . write_gexf ( G , fh ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> H = nx . read_gexf ( fh , node_type = int ) <EOL> assert_equal ( sorted ( G . nodes ( ) ) , sorted ( H . nodes ( ) ) ) <EOL> assert_equal ( <EOL> sorted ( sorted ( e ) for e in G . edges ( ) ) , <EOL> sorted ( sorted ( e ) for e in H . edges ( ) ) ) <EOL> del H . graph [ '<STR_LIT>' ] <EOL> assert_equal ( G . graph , H . graph ) <EOL> def test_serialize_ints_to_strings ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_node ( <NUM_LIT:1> , id = <NUM_LIT:7> , label = <NUM_LIT> ) <EOL> fh = io . BytesIO ( ) <EOL> nx . write_gexf ( G , fh ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> H = nx . read_gexf ( fh , node_type = int ) <EOL> assert_equal ( list ( H ) , [ <NUM_LIT:7> ] ) <EOL> assert_equal ( H . node [ <NUM_LIT:7> ] [ '<STR_LIT:label>' ] , '<STR_LIT>' ) <EOL> def test_write_with_node_attributes ( self ) : <EOL> G = nx . path_graph ( <NUM_LIT:4> ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> G . node [ i ] [ '<STR_LIT:id>' ] = i <EOL> G . node [ i ] [ '<STR_LIT:label>' ] = i <EOL> G . node [ i ] [ '<STR_LIT>' ] = i <EOL> expected = """<STR_LIT>""" . format ( nx . __version__ , time . strftime ( '<STR_LIT>' ) ) <EOL> obtained = '<STR_LIT:\n>' . join ( nx . generate_gexf ( G ) ) <EOL> assert_equal ( expected , obtained ) <EOL> def test_bool ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_node ( <NUM_LIT:1> , testattr = True ) <EOL> fh = io . BytesIO ( ) <EOL> nx . write_gexf ( G , fh ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> H = nx . read_gexf ( fh , node_type = int ) <EOL> assert_equal ( H . node [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , True ) </s>
