<s> """<STR_LIT>""" <EOL> from pygraph . classes . digraph import digraph <EOL> from pygraph . classes . exceptions import InvalidGraphType <EOL> from pygraph . classes . graph import graph <EOL> from pygraph . classes . hypergraph import hypergraph <EOL> from xml . dom . minidom import Document , parseString <EOL> def write ( G ) : <EOL> """<STR_LIT>""" <EOL> grxml = Document ( ) <EOL> if ( type ( G ) == graph ) : <EOL> grxmlr = grxml . createElement ( '<STR_LIT>' ) <EOL> elif ( type ( G ) == digraph ) : <EOL> grxmlr = grxml . createElement ( '<STR_LIT>' ) <EOL> elif ( type ( G ) == hypergraph ) : <EOL> return write_hypergraph ( G ) <EOL> else : <EOL> raise InvalidGraphType <EOL> grxml . appendChild ( grxmlr ) <EOL> for each_node in G . nodes ( ) : <EOL> node = grxml . createElement ( '<STR_LIT>' ) <EOL> node . setAttribute ( '<STR_LIT:id>' , str ( each_node ) ) <EOL> grxmlr . appendChild ( node ) <EOL> for each_attr in G . node_attributes ( each_node ) : <EOL> attr = grxml . createElement ( '<STR_LIT>' ) <EOL> attr . setAttribute ( '<STR_LIT>' , each_attr [ <NUM_LIT:0> ] ) <EOL> attr . setAttribute ( '<STR_LIT:value>' , each_attr [ <NUM_LIT:1> ] ) <EOL> node . appendChild ( attr ) <EOL> for edge_from , edge_to in G . edges ( ) : <EOL> edge = grxml . createElement ( '<STR_LIT>' ) <EOL> edge . setAttribute ( '<STR_LIT>' , str ( edge_from ) ) <EOL> edge . setAttribute ( '<STR_LIT:to>' , str ( edge_to ) ) <EOL> edge . setAttribute ( '<STR_LIT>' , str ( G . edge_weight ( ( edge_from , edge_to ) ) ) ) <EOL> edge . setAttribute ( '<STR_LIT:label>' , str ( G . edge_label ( ( edge_from , edge_to ) ) ) ) <EOL> grxmlr . appendChild ( edge ) <EOL> for attr_name , attr_value in G . edge_attributes ( ( edge_from , edge_to ) ) : <EOL> attr = grxml . createElement ( '<STR_LIT>' ) <EOL> attr . setAttribute ( '<STR_LIT>' , attr_name ) <EOL> attr . setAttribute ( '<STR_LIT:value>' , attr_value ) <EOL> edge . appendChild ( attr ) <EOL> return grxml . toprettyxml ( ) <EOL> def read ( string ) : <EOL> """<STR_LIT>""" <EOL> dom = parseString ( string ) <EOL> if dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> G = graph ( ) <EOL> elif dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> G = digraph ( ) <EOL> elif dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> return read_hypergraph ( string ) <EOL> else : <EOL> raise InvalidGraphType <EOL> for each_node in dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> G . add_node ( each_node . getAttribute ( '<STR_LIT:id>' ) ) <EOL> for each_attr in each_node . getElementsByTagName ( "<STR_LIT>" ) : <EOL> G . add_node_attribute ( each_node . getAttribute ( '<STR_LIT:id>' ) , <EOL> ( each_attr . getAttribute ( '<STR_LIT>' ) , <EOL> each_attr . getAttribute ( '<STR_LIT:value>' ) ) ) <EOL> for each_edge in dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> if ( not G . has_edge ( ( each_edge . getAttribute ( '<STR_LIT>' ) , each_edge . getAttribute ( '<STR_LIT:to>' ) ) ) ) : <EOL> G . add_edge ( ( each_edge . getAttribute ( '<STR_LIT>' ) , each_edge . getAttribute ( '<STR_LIT:to>' ) ) , wt = float ( each_edge . getAttribute ( '<STR_LIT>' ) ) , label = each_edge . getAttribute ( '<STR_LIT:label>' ) ) <EOL> for each_attr in each_edge . getElementsByTagName ( "<STR_LIT>" ) : <EOL> attr_tuple = ( each_attr . getAttribute ( '<STR_LIT>' ) , each_attr . getAttribute ( '<STR_LIT:value>' ) ) <EOL> if ( attr_tuple not in G . edge_attributes ( ( each_edge . getAttribute ( '<STR_LIT>' ) , each_edge . getAttribute ( '<STR_LIT:to>' ) ) ) ) : <EOL> G . add_edge_attribute ( ( each_edge . getAttribute ( '<STR_LIT>' ) , each_edge . getAttribute ( '<STR_LIT:to>' ) ) , attr_tuple ) <EOL> return G <EOL> def write_hypergraph ( hgr ) : <EOL> """<STR_LIT>""" <EOL> grxml = Document ( ) <EOL> grxmlr = grxml . createElement ( '<STR_LIT>' ) <EOL> grxml . appendChild ( grxmlr ) <EOL> nodes = hgr . nodes ( ) <EOL> hyperedges = hgr . hyperedges ( ) <EOL> for each_node in ( nodes + hyperedges ) : <EOL> if ( each_node in nodes ) : <EOL> node = grxml . createElement ( '<STR_LIT>' ) <EOL> else : <EOL> node = grxml . createElement ( '<STR_LIT>' ) <EOL> node . setAttribute ( '<STR_LIT:id>' , str ( each_node ) ) <EOL> grxmlr . appendChild ( node ) <EOL> if each_node in nodes : <EOL> for each_edge in hgr . links ( each_node ) : <EOL> edge = grxml . createElement ( '<STR_LIT>' ) <EOL> edge . setAttribute ( '<STR_LIT:to>' , str ( each_edge ) ) <EOL> node . appendChild ( edge ) <EOL> return grxml . toprettyxml ( ) <EOL> def read_hypergraph ( string ) : <EOL> """<STR_LIT>""" <EOL> hgr = hypergraph ( ) <EOL> dom = parseString ( string ) <EOL> for each_node in dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> hgr . add_node ( each_node . getAttribute ( '<STR_LIT:id>' ) ) <EOL> for each_node in dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> hgr . add_hyperedge ( each_node . getAttribute ( '<STR_LIT:id>' ) ) <EOL> dom = parseString ( string ) <EOL> for each_node in dom . getElementsByTagName ( "<STR_LIT>" ) : <EOL> for each_edge in each_node . getElementsByTagName ( "<STR_LIT>" ) : <EOL> hgr . link ( str ( each_node . getAttribute ( '<STR_LIT:id>' ) ) , str ( each_edge . getAttribute ( '<STR_LIT:to>' ) ) ) <EOL> return hgr </s>
