ó ŸÃÒYc@suddlmZmZddlZddlmZdZddgZe ddddƒZ e d „Z e d „Z dS( iÿÿÿÿ(tchaintcountN(tmake_strs Aric Hagberg (hagberg@lanl.gov))t tree_datat tree_graphtidtchildrencsÏ|jƒ|jƒdkr+tdƒ‚n|jƒsFtdƒ‚n|d‰|d‰ˆˆkrxtjdƒ‚n‡‡‡fd†‰tt|j|j ƒˆ|fgƒƒ}ˆ||ƒ|ˆ<|S(s€Return data in tree format that is suitable for JSON serialization and use in Javascript documents. Parameters ---------- G : NetworkX graph G must be an oriented tree root : node The root of the tree attrs : dict A dictionary that contains two keys 'id' and 'children'. The corresponding values provide the attribute names for storing NetworkX-internal graph data. The values should be unique. Default value: :samp:`dict(id='id', children='children')`. If some user-defined graph data use these attribute names as data keys, they may be silently dropped. Returns ------- data : dict A dictionary with node-link formatted data. Raises ------ NetworkXError If values in attrs are not unique. Examples -------- >>> from networkx.readwrite import json_graph >>> G = nx.DiGraph([(1,2)]) >>> data = json_graph.tree_data(G,root=1) To serialize with json >>> import json >>> s = json.dumps(data) Notes ----- Node attributes are stored in this format but keys for attributes must be strings if you want to serialize with JSON. Graph and edge attributes are not stored. The default value of attrs will be changed in a future release of NetworkX. See Also -------- tree_graph, node_link_data, node_link_data isG is not a tree.sG is not directed.RRsAttribute names are not unique.cs•||}t|ƒdkr gSg}xh|D]`}tt|j|jƒˆ|fgƒƒ}ˆ||ƒ}|r€||ˆ>> from networkx.readwrite import json_graph >>> G = nx.DiGraph([(1,2)]) >>> data = json_graph.tree_data(G,root=1) >>> H = json_graph.tree_graph(data) Notes ----- The default value of attrs will be changed in a future release of NetworkX. See Also -------- tree_graph, node_link_data, adjacency_data RRcsŒx…|D]}}|ˆ}ˆj||ƒ|jˆgƒ}|rOˆ||ƒnt‡‡fd†|jƒDƒƒ}ˆj||qWdS(Nc3s?|]5\}}|ˆkr|ˆkrt|ƒ|fVqdS(N(R(t.0tktv(RR(sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/json_graph/tree.pys s (tadd_edgetgetRR tadd_node(tparentRRRt grandchildrentnodedata(RRtgraphR(sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/json_graph/tree.pyRˆs  %c3s?|]5\}}|ˆkr|ˆkrt|ƒ|fVqdS(N(R(RRR (RR(sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/json_graph/tree.pys •s (RtDiGraphR"RR R#(RRRRR&((RRR'Rsx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/json_graph/tree.pyRcs!    % ( t itertoolsRRtnetworkxRtnetworkx.utilsRt __author__t__all__Rt_attrsRR(((sx/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/readwrite/json_graph/tree.pyts   S