ó ŸĂŇYc@sÎdZdjdddgƒZddddgZd d lmZmZd d lZd d „Z edddƒdd dd„ƒZ dd d d d„Z edddƒdd d d dd„ƒZ d„Zd S(sş ************** Adjacency List ************** Read and write NetworkX graphs as adjacency lists. Adjacency list format is useful for graphs without data associated with nodes or edges and for nodes that can be meaningfully represented as strings. Format ------ The adjacency list format consists of lines with node labels. The first label in a line is the source node. Further labels in the line are considered target nodes and are added to the graph along with an edge between the source node and target node. 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):: a b c # source target target d e s sAric Hagberg s Dan Schult s'LoĂŻc SĂŠguin-C. tgenerate_adjlistt write_adjlistt parse_adjlistt read_adjlisti˙˙˙˙(tmake_strt open_fileNt c csđ|jƒ}tƒ}xÔ|jƒD]Ć\}}t|ƒ|}x|jƒD]s\}}| rp||krpqKn|jƒrŞx?|jƒD]} |t|ƒ|7}q‰WqK|t|ƒ|7}qKW|sŘ|j|ƒn|t|ƒ Vq"WdS(s Generate a single line of the graph G in adjacency list format. Parameters ---------- G : NetworkX graph delimiter : string, optional Separator for node labels Returns ------- lines : string Lines of data in adjlist format. Examples -------- >>> G = nx.lollipop_graph(4, 3) >>> for line in nx.generate_adjlist(G): ... print(line) 0 1 2 3 1 2 3 2 3 3 4 4 5 5 6 6 See Also -------- write_adjlist, read_adjlist N( t is_directedtsett adjacencyRtitemst is_multigraphtvaluestaddtlen( tGt delimitertdirectedtseentstnbrstlinetttdatatd((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pyR,s!   itmodetwbt#sutf-8c CsŔddl}ddl}|dj|jƒd}||dj|j|jƒƒƒ|dj|jƒ}|j|j |ƒƒx7t ||ƒD]&} | d7} |j| j |ƒƒq’WdS(sxWrite graph G in single-line adjacency-list format to path. Parameters ---------- G : NetworkX graph path : string or file Filename or file handle for data output. Filenames ending in .gz or .bz2 will be compressed. 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_adjlist(G,"test.adjlist") The path can be a filehandle or a string with the name of the file. If a filehandle is provided, it has to be opened in 'wb' mode. >>> fh=open("test.adjlist",'wb') >>> nx.write_adjlist(G, fh) Notes ----- This format does not store graph, node, or edge data. See Also -------- read_adjlist, generate_adjlist i˙˙˙˙NRs s GMT {} s {} ( tsysttimetjointargvtformattasctimetgmtimetnametwritetencodeR( RtpathtcommentsRtencodingRRtpargstheaderR((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pyR^s)  : c Csy|dkrtjƒ}n*y|}|jƒWntdƒ‚nXx-|D]%}|j|ƒ}|dkrz|| }nt|ƒsŒqLn|jƒj|ƒ}|j dƒ} |dk rńy|| ƒ} Wqńtdj | |ƒƒ‚qńXn|j | ƒ|dk rKyt ||ƒ}WqKtdj dj |ƒ|ƒƒ‚qKXn|jg|D]} | | f^qXƒqLW|S(s@Parse lines of a graph adjacency list representation. Parameters ---------- lines : list or iterator of strings Input data in 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 adjacency list format. Examples -------- >>> lines = ['1 2 5', ... '2 3 4', ... '3 5', ... '4', ... '5'] >>> G = nx.parse_adjlist(lines, nodetype=int) >>> nodes = [1, 2, 3, 4, 5] >>> all(node in G for node in nodes) True >>> edges = [(1, 2), (1, 5), (2, 3), (2, 4), (3, 5)] >>> all((u, v) in G.edges() or (v, u) in G.edges() for (u, v) in edges) True See Also -------- read_adjlist s(Input graph is not a NetworkX graph typeis&Failed to convert node ({}) to type {}s'Failed to convert nodes ({}) to type {}t,N(tNonetnxtGraphtcleart TypeErrortfindRtstriptsplittpopR tadd_nodetmapRtadd_edges_from( tlinesR'Rt create_usingtnodetypeRRtptvlisttutv((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pyR”s<.          *itrbc s8‡fd†|Dƒ}t|d|d|d|d|ƒS(sRRead graph in 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. 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 adjacency list format. Examples -------- >>> G=nx.path_graph(4) >>> nx.write_adjlist(G, "test.adjlist") >>> G=nx.read_adjlist("test.adjlist") The path can be a filehandle or a string with the name of the file. If a filehandle is provided, it has to be opened in 'rb' mode. >>> fh=open("test.adjlist", 'rb') >>> G=nx.read_adjlist(fh) Filenames ending in .gz or .bz2 will be compressed. >>> nx.write_adjlist(G,"test.adjlist.gz") >>> G=nx.read_adjlist("test.adjlist.gz") The optional nodetype is a function to convert node strings to nodetype. For example >>> G=nx.read_adjlist("test.adjlist", nodetype=int) will attempt to convert all nodes to integer type. Since nodes must be hashable, the function nodetype must return hashable types (e.g. int, float, str, frozenset - or tuples of those, etc.) 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_adjlist("test.adjlist", create_using=nx.DiGraph()) Notes ----- This format does not store graph or node data. See Also -------- write_adjlist c3s|]}|jˆƒVqdS(N(tdecode(t.0R(R((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pys +sR'RR9R:(R(R&R'RR9R:R(R8((R(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pyRĺs F cCsIddl}x6ddgD](}|jj|ƒr|j|ƒqqWdS(Ni˙˙˙˙s test.adjliststest.adjlist.gz(tosR&tisfiletunlink(tmoduleRBtfname((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pytteardown_module5s (t__doc__Rt __author__t__all__tnetworkx.utilsRRtnetworkxR-RRR,RRRG(((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/adjlist.pyts$     25P N