ó ŸÃÒYc@sÔdZdjdddgƒZddddgZd d lmZmZd d lZd d „Z edddƒd ddd„ƒZ dd d d d d„Z edddƒdd d d d dd„ƒZ d„Zd S(sû ************************* Multi-line Adjacency List ************************* Read and write NetworkX graphs as multi-line adjacency lists. The multi-line adjacency list format is useful for graphs with nodes that can be meaningfully represented as strings. With this format simple edge data can be stored but node or graph data is not. Format ------ The first label in a line is the source node label followed by the node degree d. The next d lines are target node labels and optional edge data. That pattern repeats for all nodes in the graph. The graph with edges a-b, a-c, d-e can be represented as the following adjacency list (anything following the # in a line is a comment):: # example.multiline-adjlist a 2 b c d 1 e s sAric Hagberg s Dan Schult s'Loïc Séguin-C. tgenerate_multiline_adjlisttwrite_multiline_adjlisttparse_multiline_adjlisttread_multiline_adjlistiÿÿÿÿ(tmake_strt open_fileNt c cs^|jƒr€|jƒrëxb|jƒD]¿\}}g|jƒD]1\}}|jƒD]\}}||f^qTq>}t|ƒ} t|ƒ|t| ƒVxG|D]?\}} | dkrÇt|ƒVq¡t|ƒ|t| ƒVq¡Wq%WqZxl|jƒD]\}}t|ƒ} t|ƒ|t| ƒVxM|jƒD]?\}} | dkr\t|ƒVq6t|ƒ|t| ƒVq6WqøWnÚ|jƒrtƒ} xÂ|jƒD]Ø\}}g|jƒD]=\}}|| kr»|jƒD]\}}||f^qÝq»}t|ƒ} t|ƒ|t| ƒVxG|D]?\}} | dkrPt|ƒVq*t|ƒ|t| ƒVq*W| j |ƒq¢WnÙtƒ} xÍ|jƒD]¿\}}g|jƒD]$\}} || kr°|| f^q°}t|ƒ} t|ƒ|t| ƒVxG|D]?\}} | dkr,t|ƒVqt|ƒ|t| ƒVqW| j |ƒq—WdS(s‹Generate a single line of the graph G in multiline adjacency list format. Parameters ---------- G : NetworkX graph delimiter : string, optional Separator for node labels Returns ------- lines : string Lines of data in multiline adjlist format. Examples -------- >>> G = nx.lollipop_graph(4, 3) >>> for line in nx.generate_multiline_adjlist(G): ... print(line) 0 3 1 {} 2 {} 3 {} 1 2 2 {} 3 {} 2 1 3 {} 3 1 4 {} 4 1 5 {} 5 1 6 {} 6 0 See Also -------- write_multiline_adjlist, read_multiline_adjlist N( t is_directedt is_multigrapht adjacencytitemstlenRtstrtNonetsettadd( tGt delimitertstnbrstutdatadicttkeytdatat nbr_edgestdegtdtseen((sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pyR/sV)  +  $  $   +   7  itmodetwbt#sutf-8c CsÅddl}ddl}|dj|jƒ}dj|ƒ|dj|j|jƒƒƒ|dj|jƒ}|j|j |ƒƒx7t ||ƒD]&} | d7} |j| j |ƒƒq—WdS(s# Write the graph G in multiline adjacency list format to path Parameters ---------- G : NetworkX graph comments : string, optional Marker for comment lines delimiter : string, optional Separator for node labels encoding : string, optional Text encoding. Examples -------- >>> G=nx.path_graph(4) >>> nx.write_multiline_adjlist(G,"test.adjlist") The path can be a file handle or a string with the name of the file. If a file handle is provided, it has to be opened in 'wb' mode. >>> fh=open("test.adjlist",'wb') >>> nx.write_multiline_adjlist(G,fh) Filenames ending in .gz or .bz2 will be compressed. >>> nx.write_multiline_adjlist(G,"test.adjlist.gz") See Also -------- read_multiline_adjlist iÿÿÿÿNRs{} s GMT {} s {} s ( tsysttimetjointargvtformattasctimetgmtimetnametwritetencodeR( RtpathRtcommentstencodingRR tpargstheadert multiline((sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pyRŒs%  C cCsØddlm}|d kr+tjƒ}n*y|}|jƒWntdƒ‚nXx||D]t}|j|ƒ} | dkrŠ|| }n|s–q\ny+|jƒj |ƒ\} } t | ƒ} Wntdj |ƒƒ‚nX|d k r!y|| ƒ} Wq!tdj | |ƒƒ‚q!Xn|j | ƒxŸt | ƒD]‘} x{tr¾yt|ƒ}Wn,tk rˆdj | ƒ} t| ƒ‚nX|j|ƒ} | dkr±|| }n|rDPqDqDW|jƒj |ƒ}t|ƒ}|dkròq;n|jdƒ}d j|ƒ}|d k rQy||ƒ}WqQtdj ||ƒƒ‚qQXn|d k r™yi||ƒd 6}Wq¹td j ||ƒƒ‚q¹Xn y||ƒ}Wn i}nX|j| ||q;Wq\W|S( sÃParse lines of a multiline adjacency list representation of a graph. Parameters ---------- lines : list or iterator of strings Input data in multiline adjlist format create_using: NetworkX graph container Use given NetworkX graph for holding nodes or edges. nodetype : Python type, optional Convert nodes to this type. comments : string, optional Marker for comment lines delimiter : string, optional Separator for node labels. The default is whitespace. Returns ------- G: NetworkX graph The graph corresponding to the lines in multiline adjacency list format. Examples -------- >>> lines = ['1 2', ... "2 {'weight':3, 'name': 'Frodo'}", ... "3 {}", ... "2 1", ... "5 {'weight':6, 'name': 'Saruman'}"] >>> G = nx.parse_multiline_adjlist(iter(lines), nodetype=int) >>> list(G) [1, 2, 3, 5] iÿÿÿÿ(t literal_evals(Input graph is not a networkx graph typeis+Failed to read node and degree on line ({})s&Failed to convert node ({}) to type {}s%Failed to find neighbor for node ({})ittweights+Failed to convert edge data ({}) to type {}N(tastR/R tnxtGraphtcleart TypeErrortfindtstriptsplittintR#tadd_nodetrangetTruetnextt StopIterationR tpopR!tadd_edge(tlinesR*Rt create_usingtnodetypetedgetypeR/RtlinetpRRtitmsgtvlisttnumbtvRtedgedata((sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pyR¿s€'                itrbc s>‡fd†|Dƒ}t|d|d|d|d|d|ƒS(s£Read graph in multi-line adjacency list format from path. Parameters ---------- path : string or file Filename or file handle to read. Filenames ending in .gz or .bz2 will be uncompressed. create_using: NetworkX graph container Use given NetworkX graph for holding nodes or edges. nodetype : Python type, optional Convert nodes to this type. edgetype : Python type, optional Convert edge data to this type. comments : string, optional Marker for comment lines delimiter : string, optional Separator for node labels. The default is whitespace. Returns ------- G: NetworkX graph Examples -------- >>> G=nx.path_graph(4) >>> nx.write_multiline_adjlist(G,"test.adjlist") >>> G=nx.read_multiline_adjlist("test.adjlist") The path can be a file or a string with the name of the file. If a file s provided, it has to be opened in 'rb' mode. >>> fh=open("test.adjlist", 'rb') >>> G=nx.read_multiline_adjlist(fh) Filenames ending in .gz or .bz2 will be compressed. >>> nx.write_multiline_adjlist(G,"test.adjlist.gz") >>> G=nx.read_multiline_adjlist("test.adjlist.gz") The optional nodetype is a function to convert node strings to nodetype. For example >>> G=nx.read_multiline_adjlist("test.adjlist", nodetype=int) will attempt to convert all nodes to integer type. The optional edgetype is a function to convert edge data strings to edgetype. >>> G=nx.read_multiline_adjlist("test.adjlist") The optional create_using parameter is a NetworkX graph container. The default is Graph(), an undirected graph. To read the data as a directed graph use >>> G=nx.read_multiline_adjlist("test.adjlist", create_using=nx.DiGraph()) Notes ----- This format does not store graph, node, or edge data. See Also -------- write_multiline_adjlist c3s|]}|jˆƒVqdS(N(tdecode(t.0RF(R+(sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pys xsR*RRCRDRE(R(R)R*RRCRDRER+RB((R+sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pyR,sL cCsIddl}x6ddgD](}|jj|ƒr|j|ƒqqWdS(Niÿÿÿÿs test.adjliststest.adjlist.gz(tosR)tisfiletunlink(tmoduleRQtfname((sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pytteardown_module‚s (t__doc__R!t __author__t__all__tnetworkx.utilsRRtnetworkxR3RRR RRRV(((sz/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/multiline_adjlist.pyts,     ]1 kR