<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import networkx as nx <EOL> from networkx . utils import open_file , make_str , not_implemented_for <EOL> from networkx . convert import _prep_create_using <EOL> @ open_file ( <NUM_LIT:1> , mode = '<STR_LIT:wb>' ) <EOL> def write_edgelist ( G , path , comments = "<STR_LIT:#>" , delimiter = '<STR_LIT:U+0020>' , data = True , <EOL> encoding = '<STR_LIT:utf-8>' ) : <EOL> """<STR_LIT>""" <EOL> for line in generate_edgelist ( G , delimiter , data ) : <EOL> line += '<STR_LIT:\n>' <EOL> path . write ( line . encode ( encoding ) ) <EOL> @ not_implemented_for ( '<STR_LIT>' ) <EOL> def generate_edgelist ( G , delimiter = '<STR_LIT:U+0020>' , data = True ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> part0 = [ n for n , d in G . node . items ( ) if d [ '<STR_LIT>' ] == <NUM_LIT:0> ] <EOL> except : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> if data is True or data is False : <EOL> for n in part0 : <EOL> for e in G . edges ( n , data = data ) : <EOL> yield delimiter . join ( map ( make_str , e ) ) <EOL> else : <EOL> for n in part0 : <EOL> for u , v , d in G . edges ( n , data = True ) : <EOL> e = [ u , v ] <EOL> try : <EOL> e . extend ( d [ k ] for k in data ) <EOL> except KeyError : <EOL> pass <EOL> yield delimiter . join ( map ( make_str , e ) ) <EOL> def parse_edgelist ( lines , comments = '<STR_LIT:#>' , delimiter = None , <EOL> create_using = None , nodetype = None , data = True ) : <EOL> """<STR_LIT>""" <EOL> from ast import literal_eval <EOL> G = _prep_create_using ( create_using ) <EOL> for line in lines : <EOL> p = line . find ( comments ) <EOL> if p >= <NUM_LIT:0> : <EOL> line = line [ : p ] <EOL> if not len ( line ) : <EOL> continue <EOL> s = line . strip ( ) . split ( delimiter ) <EOL> if len ( s ) < <NUM_LIT:2> : <EOL> continue <EOL> u = s . pop ( <NUM_LIT:0> ) <EOL> v = s . pop ( <NUM_LIT:0> ) <EOL> d = s <EOL> if nodetype is not None : <EOL> try : <EOL> u = nodetype ( u ) <EOL> v = nodetype ( v ) <EOL> except : <EOL> raise TypeError ( "<STR_LIT>" <EOL> % ( u , v , nodetype ) ) <EOL> if len ( d ) == <NUM_LIT:0> or data is False : <EOL> edgedata = { } <EOL> elif data is True : <EOL> try : <EOL> edgedata = dict ( literal_eval ( '<STR_LIT:U+0020>' . join ( d ) ) ) <EOL> except : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % ( d ) ) <EOL> else : <EOL> if len ( d ) != len ( data ) : <EOL> raise IndexError ( <EOL> "<STR_LIT>" % <EOL> ( d , data ) ) <EOL> edgedata = { } <EOL> for ( edge_key , edge_type ) , edge_value in zip ( data , d ) : <EOL> try : <EOL> edge_value = edge_type ( edge_value ) <EOL> except : <EOL> raise TypeError ( <EOL> "<STR_LIT>" <EOL> % ( edge_key , edge_value , edge_type ) ) <EOL> edgedata . update ( { edge_key : edge_value } ) <EOL> G . add_node ( u , bipartite = <NUM_LIT:0> ) <EOL> G . add_node ( v , bipartite = <NUM_LIT:1> ) <EOL> G . add_edge ( u , v , attr_dict = edgedata ) <EOL> return G <EOL> @ open_file ( <NUM_LIT:0> , mode = '<STR_LIT:rb>' ) <EOL> def read_edgelist ( path , comments = "<STR_LIT:#>" , <EOL> delimiter = None , create_using = None , <EOL> nodetype = None , data = True , edgetype = None , <EOL> encoding = '<STR_LIT:utf-8>' ) : <EOL> """<STR_LIT>""" <EOL> lines = ( line . decode ( encoding ) for line in path ) <EOL> return parse_edgelist ( lines , comments = comments , <EOL> delimiter = delimiter , <EOL> create_using = create_using , <EOL> nodetype = nodetype , <EOL> data = data ) </s>
