o
    ˷ec-                     @   sv   d dl mZmZ d dlZddgZeddddd	d
Zdd Z	dddddd	d
ddZ				dddddd	d
ddZ
dS )    )chaincountNnode_link_datanode_link_graphsourcetargetidkeylinks)r   r   namer	   linkc                 C   s    t | ttfs	| S ttt| S )a7  Converts lists to tuples, including nested lists.

    All other non-list inputs are passed through unmodified. This function is
    intended to be used to convert potentially nested lists from json files
    into valid nodes.

    Examples
    --------
    >>> _to_tuple([1, 2, [3, 4]])
    (1, 2, (3, 4))
    )
isinstancetuplelistmap	_to_tuple)x r   ^/var/www/ideatree/venv/lib/python3.10/site-packages/networkx/readwrite/json_graph/node_link.pyr      s   r   c                   s  |dur0ddl }d}|j|tdd |dd|dd|dd|d	d	|d
d}  }	|	s8dnthdk rHtd  |	 j	 fdd D d}
|	rpfdd j
dddD |
|< |
S fdd j
ddD |
|< |
S )aq  Returns data in node-link format that is suitable for JSON serialization
    and use in Javascript documents.

    Parameters
    ----------
    G : NetworkX graph

    attrs : dict
        A dictionary that contains five keys 'source', 'target', 'name',
        'key' and 'link'.  The corresponding values provide the attribute
        names for storing NetworkX-internal graph data.  The values should
        be unique.  Default value::

            dict(source='source', target='target', name='id',
                 key='key', link='links')

        If some user-defined graph data use these attribute names as data keys,
        they may be silently dropped.

        .. deprecated:: 2.8.6

           The `attrs` keyword argument will be replaced with `source`, `target`, `name`,
           `key` and `link`. in networkx 3.2

           If the `attrs` keyword and the new keywords are both used in a single function call (not recommended)
           the `attrs` keyword argument will take precedence.

           The values of the keywords must be unique.

    source : string
        A string that provides the 'source' attribute name for storing NetworkX-internal graph data.
    target : string
        A string that provides the 'target' attribute name for storing NetworkX-internal graph data.
    name : string
        A string that provides the 'name' attribute name for storing NetworkX-internal graph data.
    key : string
        A string that provides the 'key' attribute name for storing NetworkX-internal graph data.
    link : string
        A string that provides the 'link' attribute name for storing NetworkX-internal graph data.

    Returns
    -------
    data : dict
       A dictionary with node-link formatted data.

    Raises
    ------
    NetworkXError
        If the values of 'source', 'target' and 'key' are not unique.

    Examples
    --------
    >>> G = nx.Graph([("A", "B")])
    >>> data1 = nx.node_link_data(G)
    >>> data1
    {'directed': False, 'multigraph': False, 'graph': {}, 'nodes': [{'id': 'A'}, {'id': 'B'}], 'links': [{'source': 'A', 'target': 'B'}]}

    To serialize with JSON

    >>> import json
    >>> s1 = json.dumps(data1)
    >>> s1
    '{"directed": false, "multigraph": false, "graph": {}, "nodes": [{"id": "A"}, {"id": "B"}], "links": [{"source": "A", "target": "B"}]}'

    A graph can also be serialized by passing `node_link_data` as an encoder function. The two methods are equivalent.

    >>> s1 = json.dumps(G, default=nx.node_link_data)
    >>> s1
    '{"directed": false, "multigraph": false, "graph": {}, "nodes": [{"id": "A"}, {"id": "B"}], "links": [{"source": "A", "target": "B"}]}'

    The attribute names for storing NetworkX-internal graph data can
    be specified as keyword options.

    >>> H = nx.gn_graph(2)
    >>> data2 = nx.node_link_data(H, link="edges", source="from", target="to")
    >>> data2
    {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'id': 0}, {'id': 1}], 'edges': [{'from': 1, 'to': 0}]}

    Notes
    -----
    Graph, node, and link attributes are stored in this format.  Note that
    attribute keys will be converted to strings in order to comply with JSON.

    Attribute 'key' is only used for multigraphs.

    To use `node_link_data` in conjunction with `node_link_graph`,
    the keyword names for the attributes must match.


    See Also
    --------
    node_link_graph, adjacency_data, tree_data
    Nr   a  

The `attrs` keyword argument of node_link_data is deprecated
and will be removed in networkx 3.2. It is replaced with explicit
keyword arguments: `source`, `target`, `name`, `key` and `link`.
To make this warning go away, and ensure usage is forward
compatible, replace `attrs` with the keywords. For example:

   >>> node_link_data(G, attrs={'target': 'foo', 'name': 'bar'})

should instead be written as

   >>> node_link_data(G, target='foo', name='bar')

in networkx 3.2.
The default values of the keywords will not change.
   
stacklevelr   r   r   r	   r   r
      zAttribute names are not unique.c                    s*   g | ]}t t j|  |fgqS r   )dictr   nodesitems).0n)Gr   r   r   
<listcomp>      * z"node_link_data.<locals>.<listcomp>)directed
multigraphgraphr   c              	      s8   g | ]\}}}}t t| |f|f |fgqS r   r   r   r   )r   uvkdr	   r   r   r   r   r      s    
"T)keysdatac                    s0   g | ]\}}}t t|  |f|fgqS r   r$   )r   r%   r&   r(   r   r   r   r   r      s    )r+   )warningswarnDeprecationWarninggetis_multigraphlennxNetworkXErroris_directedr#   edges)r   attrsr   r   r   r	   r   r-   msgr"   r+   r   )r   r	   r   r   r   r   r      s8   h

	

FTc                   s  |dur0ddl }	d}
|	j|
tdd |dd|dd|dd|d	d	 |d
d}| d|}| d|}|rCt }nt }|rM| }|sQdn  | di |_t	 }| d D ] }t
|t|}fdd| D }|j|fi | qa| | D ][}t| trt| n| }t| trt| n| }|sÇfdd| D }|j||fi | q| d} fdd| D }|j|||fi | q|S )a
  Returns graph from node-link data format.
    Useful for de-serialization from JSON.

    Parameters
    ----------
    data : dict
        node-link formatted graph data

    directed : bool
        If True, and direction not specified in data, return a directed graph.

    multigraph : bool
        If True, and multigraph not specified in data, return a multigraph.

    attrs : dict
        A dictionary that contains five keys 'source', 'target', 'name',
        'key' and 'link'.  The corresponding values provide the attribute
        names for storing NetworkX-internal graph data.  Default value:

            dict(source='source', target='target', name='id',
                key='key', link='links')

        .. deprecated:: 2.8.6

           The `attrs` keyword argument will be replaced with the individual keywords: `source`, `target`, `name`,
           `key` and `link`. in networkx 3.2.

           If the `attrs` keyword and the new keywords are both used in a single function call (not recommended)
           the `attrs` keyword argument will take precedence.

           The values of the keywords must be unique.

    source : string
        A string that provides the 'source' attribute name for storing NetworkX-internal graph data.
    target : string
        A string that provides the 'target' attribute name for storing NetworkX-internal graph data.
    name : string
        A string that provides the 'name' attribute name for storing NetworkX-internal graph data.
    key : string
        A string that provides the 'key' attribute name for storing NetworkX-internal graph data.
    link : string
        A string that provides the 'link' attribute name for storing NetworkX-internal graph data.

    Returns
    -------
    G : NetworkX graph
        A NetworkX graph object

    Examples
    --------

    Create data in node-link format by converting a graph.

    >>> G = nx.Graph([('A', 'B')])
    >>> data = nx.node_link_data(G)
    >>> data
    {'directed': False, 'multigraph': False, 'graph': {}, 'nodes': [{'id': 'A'}, {'id': 'B'}], 'links': [{'source': 'A', 'target': 'B'}]}

    Revert data in node-link format to a graph.

    >>> H = nx.node_link_graph(data)
    >>> print(H.edges)
    [('A', 'B')]

    To serialize and deserialize a graph with JSON,

    >>> import json
    >>> d = json.dumps(node_link_data(G))
    >>> H = node_link_graph(json.loads(d))
    >>> print(G.edges, H.edges)
    [('A', 'B')] [('A', 'B')]


    Notes
    -----
    Attribute 'key' is only used for multigraphs.

    To use `node_link_data` in conjunction with `node_link_graph`,
    the keyword names for the attributes must match.

    See Also
    --------
    node_link_data, adjacency_data, tree_data
    Nr   a  

The `attrs` keyword argument of node_link_graph is deprecated
and will be removed in networkx 3.2. It is replaced with explicit
keyword arguments: `source`, `target`, `name`, `key` and `link`.
To make this warning go away, and ensure usage is forward
compatible, replace `attrs` with the keywords. For example:

   >>> node_link_graph(data, attrs={'target': 'foo', 'name': 'bar'})

should instead be written as

   >>> node_link_graph(data, target='foo', name='bar')

in networkx 3.2.
The default values of the keywords will not change.
r   r   r   r   r   r	   r   r
   r"   r!   r#   r   c                    s"   i | ]\}}| krt ||qS r   strr   r'   r&   )r   r   r   
<dictcomp>=  s   " z#node_link_graph.<locals>.<dictcomp>c                    s*   i | ]\}}| kr|krt ||qS r   r9   r;   r,   r   r   r<   C  r    c                    s2   i | ]\}}|kr|kr| krt ||qS r   r9   r;   r)   r   r   r<   G  s
    )r-   r.   r/   r0   r3   
MultiGraphGraphto_directedr#   r   r   nextr   add_noder   r   r   add_edge)r+   r!   r"   r7   r   r   r   r	   r   r-   r8   r#   cr(   nodenodedatasrctgtedgedatakyr   )r	   r   r   r   r   r      sH   a
"")N)FTN)	itertoolsr   r   networkxr3   __all__r   _attrsr   r   r   r   r   r   r   <module>   s0     