ó ŸÃÒYc @sûdZdZddddddgZdd lmZmZdd lZd ed „Z ed ddƒdd edd„ƒZ dd d d ed„Z edddƒdd d d ed dd„ƒZ dd dd„Zdd d d dd„Zd„Zd S(sM ********** Edge Lists ********** Read and write NetworkX graphs as edge lists. The multi-line adjacency list format is useful for graphs with nodes that can be meaningfully represented as strings. With the edgelist format simple edge data can be stored but node or graph data is not. There is no way of representing isolated nodes unless the node has a self-loop edge. Format ------ You can read or write three formats of edge lists with these functions. Node pairs with no data:: 1 2 Python dictionary as data:: 1 2 {'weight':7, 'color':'green'} Arbitrary data:: 1 2 7 green s@Aric Hagberg (hagberg@lanl.gov) Dan Schult (dschult@colgate.edu)tgenerate_edgelisttwrite_edgelisttparse_edgelistt read_edgelisttread_weighted_edgelisttwrite_weighted_edgelistiÿÿÿÿ(t open_filetmake_strNt c#s8|tkrax%|jdtƒD];\}}‰||tˆƒf}|jtt|ƒƒVqWnÓ|tkr¶xÄ|jdtƒD]/\}}||f}|jtt|ƒƒVq€Wn~x{|jdtƒD]g\}}‰||g}y!|j‡fd†|DƒƒWntk rnX|jtt|ƒƒVqÉWdS(s–Generate a single line of the graph G in edge list format. Parameters ---------- G : NetworkX graph delimiter : string, optional Separator for node labels data : bool or list of keys If False generate no edge data. If True use a dictionary representation of edge data. If a list of keys use a list of data values corresponding to the keys. Returns ------- lines : string Lines of data in adjlist format. Examples -------- >>> G = nx.lollipop_graph(4, 3) >>> G[1][2]['weight'] = 3 >>> G[3][4]['capacity'] = 12 >>> for line in nx.generate_edgelist(G, data=False): ... print(line) 0 1 0 2 0 3 1 2 1 3 2 3 3 4 4 5 5 6 >>> for line in nx.generate_edgelist(G): ... print(line) 0 1 {} 0 2 {} 0 3 {} 1 2 {'weight': 3} 1 3 {} 2 3 {} 3 4 {'capacity': 12} 4 5 {} 5 6 {} >>> for line in nx.generate_edgelist(G,data=['weight']): ... print(line) 0 1 0 2 0 3 1 2 3 1 3 2 3 3 4 4 5 5 6 See Also -------- write_adjlist, read_adjlist tdatac3s|]}ˆ|VqdS(N((t.0tk(td(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pys |sN( tTruetedgestdicttjointmapRtFalsetextendtKeyError(tGt delimiterR tutvte((R sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyR/sA "  " ! itmodetwbt#sutf-8cCsAx:t|||ƒD]&}|d7}|j|j|ƒƒqWdS(s¢Write graph as a list of edges. Parameters ---------- G : graph A NetworkX graph path : file or string File or filename to write. If a file is provided, it must be opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed. comments : string, optional The character used to indicate the start of a comment delimiter : string, optional The string used to separate values. The default is whitespace. data : bool or list, optional If False write no edge data. If True write a string representation of the edge data dictionary.. If a list (or other iterable) is provided, write the keys specified in the list. encoding: string, optional Specify which encoding to use when writing file. Examples -------- >>> G=nx.path_graph(4) >>> nx.write_edgelist(G, "test.edgelist") >>> G=nx.path_graph(4) >>> fh=open("test.edgelist",'wb') >>> nx.write_edgelist(G, fh) >>> nx.write_edgelist(G, "test.edgelist.gz") >>> nx.write_edgelist(G, "test.edgelist.gz", data=False) >>> G=nx.Graph() >>> G.add_edge(1,2,weight=7,color='red') >>> nx.write_edgelist(G,'test.edgelist',data=False) >>> nx.write_edgelist(G,'test.edgelist',data=['color']) >>> nx.write_edgelist(G,'test.edgelist',data=['color','weight']) See Also -------- write_edgelist() write_weighted_edgelist() s N(Rtwritetencode(RtpathtcommentsRR tencodingtline((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyRs. cCsiddlm}|d kr+tjƒ}n*y|}|jƒWntdƒ‚nXx |D]}|j|ƒ} | dkrŠ|| }nt|ƒsœq\n|j ƒj |ƒ} t| ƒdkrÉq\n| j dƒ} | j dƒ} | } |d k r;y|| ƒ} || ƒ} Wq;td| | |fƒ‚q;Xnt| ƒdksY|t krbi}nì|t krªyt|dj| ƒƒƒ}WqNtd| ƒ‚qNXn¤t| ƒt|ƒkrÛtd | |fƒ‚ni}xjt|| ƒD]Y\\}}}y||ƒ}Wn td |||fƒ‚nX|ji||6ƒqñW|j| | |q\W|S( s,Parse lines of an edge list representation of a graph. Parameters ---------- lines : list or iterator of strings Input data in edgelist format comments : string, optional Marker for comment lines delimiter : string, optional Separator for node labels create_using: NetworkX graph container, optional Use given NetworkX graph for holding nodes or edges. nodetype : Python type, optional Convert nodes to this type. data : bool or list of (label,type) tuples If False generate no edge data or if True use a dictionary representation of edge data or a list tuples specifying dictionary key names and types for edge data. Returns ------- G: NetworkX Graph The graph corresponding to lines Examples -------- Edgelist with no data: >>> lines = ["1 2", ... "2 3", ... "3 4"] >>> G = nx.parse_edgelist(lines, nodetype = int) >>> list(G) [1, 2, 3, 4] >>> list(G.edges()) [(1, 2), (2, 3), (3, 4)] Edgelist with data in Python dictionary representation: >>> lines = ["1 2 {'weight':3}", ... "2 3 {'weight':27}", ... "3 4 {'weight':3.0}"] >>> G = nx.parse_edgelist(lines, nodetype = int) >>> list(G) [1, 2, 3, 4] >>> list(G.edges(data=True)) [(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})] Edgelist with data in a list: >>> lines = ["1 2 3", ... "2 3 27", ... "3 4 3.0"] >>> G = nx.parse_edgelist(lines, nodetype = int, data=(('weight',float),)) >>> list(G) [1, 2, 3, 4] >>> list(G.edges(data=True)) [(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})] See Also -------- read_weighted_edgelist iÿÿÿÿ(t literal_evals/create_using input is not a NetworkX graph typeiis)Failed to convert nodes %s,%s to type %s.Rs/Failed to convert edge data (%s) to dictionary.s5Edge data %s and data_keys %s are not the same lengths(Failed to convert %s data %s to type %s.N(tastR#tNonetnxtGraphtcleart TypeErrortfindtlentstriptsplittpopRR RRt IndexErrortziptupdatetadd_edge(tlinesR Rt create_usingtnodetypeR R#RR"tptsRRR tedgedatatedge_keyt edge_typet edge_value((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyR³sdB         "itrbc s>‡fd†|Dƒ}t|d|d|d|d|d|ƒS(sBRead a graph from a list of edges. Parameters ---------- path : file or string File or filename to read. If a file is provided, it must be opened in 'rb' mode. Filenames ending in .gz or .bz2 will be uncompressed. comments : string, optional The character used to indicate the start of a comment. delimiter : string, optional The string used to separate values. The default is whitespace. create_using : Graph container, optional, Use specified container to build graph. The default is networkx.Graph, an undirected graph. nodetype : int, float, str, Python type, optional Convert node data from strings to specified type data : bool or list of (label,type) tuples Tuples specifying dictionary key names and types for edge data edgetype : int, float, str, Python type, optional OBSOLETE Convert edge data from strings to specified type and use as 'weight' encoding: string, optional Specify which encoding to use when reading file. Returns ------- G : graph A networkx Graph or other type specified with create_using Examples -------- >>> nx.write_edgelist(nx.path_graph(4), "test.edgelist") >>> G=nx.read_edgelist("test.edgelist") >>> fh=open("test.edgelist", 'rb') >>> G=nx.read_edgelist(fh) >>> fh.close() >>> G=nx.read_edgelist("test.edgelist", nodetype=int) >>> G=nx.read_edgelist("test.edgelist",create_using=nx.DiGraph()) Edgelist with data in a list: >>> textline = '1 2 3' >>> fh = open('test.edgelist','w') >>> d = fh.write(textline) >>> fh.close() >>> G = nx.read_edgelist('test.edgelist', nodetype=int, data=(('weight',float),)) >>> list(G) [1, 2] >>> list(G.edges(data=True)) [(1, 2, {'weight': 3.0})] See parse_edgelist() for more examples of formatting. See Also -------- parse_edgelist Notes ----- Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.) c3s|]}|jˆƒVqdS(N(tdecode(R R"(R!(sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pys ssR RR4R5R (R( RR RR4R5R tedgetypeR!R3((R!sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyR0sC c Cs)t||d|d|ddd|ƒdS(sEWrite graph G as a list of edges with numeric weights. Parameters ---------- G : graph A NetworkX graph path : file or string File or filename to write. If a file is provided, it must be opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed. comments : string, optional The character used to indicate the start of a comment delimiter : string, optional The string used to separate values. The default is whitespace. encoding: string, optional Specify which encoding to use when writing file. Examples -------- >>> G=nx.Graph() >>> G.add_edge(1,2,weight=7) >>> nx.write_weighted_edgelist(G, 'test.weighted.edgelist') See Also -------- read_edgelist() write_edgelist() write_weighted_edgelist() R RR tweightR!N(R?(R(RRR RR!((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyRys cCs7t|d|d|d|d|ddtffd|ƒS(sRead a graph as list of edges with numeric weights. Parameters ---------- path : file or string File or filename to read. If a file is provided, it must be opened in 'rb' mode. Filenames ending in .gz or .bz2 will be uncompressed. comments : string, optional The character used to indicate the start of a comment. delimiter : string, optional The string used to separate values. The default is whitespace. create_using : Graph container, optional, Use specified container to build graph. The default is networkx.Graph, an undirected graph. nodetype : int, float, str, Python type, optional Convert node data from strings to specified type encoding: string, optional Specify which encoding to use when reading file. Returns ------- G : graph A networkx Graph or other type specified with create_using Notes ----- Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.) Example edgelist file format. With numeric edge data:: # read with # >>> G=nx.read_weighted_edgelist(fh) # source target data a b 1 a c 3.14159 d e 42 R RR4R5R R?R!(Rtfloat(RR RR4R5R!((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyRœs, cCsLddl}x9dddgD](}|jj|ƒr|j|ƒqqWdS(Niÿÿÿÿs test.edgeliststest.edgelist.gzstest.weighted.edgelist(tosRtisfiletunlink(tmoduleRAtfname((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pytteardown_moduleÓs    (t__doc__t __author__t__all__tnetworkx.utilsRRtnetworkxR&R RRR%RRRRRF(((sq/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/edgelist.pyts.  R 0| G"6