
    Vh,                        d Z g dZddlZddlmZmZ  edd      dd	       Z ed
      dd       Z ej                  ddd      	 dd       Z
 edd       ej                  ddd      	 	 	 	 	 	 	 dd              Zy)a  
********************
Bipartite Edge Lists
********************
Read and write NetworkX graphs as bipartite edge lists.

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

For each edge (u, v) the node u is assigned to part 0 and the node v to part 1.
)generate_edgelistwrite_edgelistparse_edgelistread_edgelist    N)not_implemented_for	open_file   wb)modeTc                 r    t        | ||      D ]'  }|dz  }|j                  |j                  |             ) y)a-  Write a bipartite graph as a list of edges.

    Parameters
    ----------
    G : Graph
       A NetworkX bipartite 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)
    >>> G.add_nodes_from([0, 2], bipartite=0)
    >>> G.add_nodes_from([1, 3], bipartite=1)
    >>> nx.write_edgelist(G, "test.edgelist")
    >>> fh = open("test.edgelist_open", "wb")
    >>> nx.write_edgelist(G, fh)
    >>> nx.write_edgelist(G, "test.edgelist.gz")
    >>> nx.write_edgelist(G, "test.edgelist_nodata.gz", data=False)

    >>> G = nx.Graph()
    >>> G.add_edge(1, 2, weight=7, color="red")
    >>> nx.write_edgelist(G, "test.edgelist_bigger_nodata", data=False)
    >>> nx.write_edgelist(G, "test.edgelist_color", data=["color"])
    >>> nx.write_edgelist(G, "test.edgelist_color_weight", data=["color", "weight"])

    See Also
    --------
    write_edgelist
    generate_edgelist
    
N)r   writeencode)Gpathcomments	delimiterdataencodinglines          V/home/dcms/DCMS/lib/python3.12/site-packages/networkx/algorithms/bipartite/edgelist.pyr   r       s;    Z "!Y5 *

4;;x()*    directedc              #   6  K   	 | j                   j                         D cg c]  \  }}|d   dk(  s| }}}|du s|du rA|D ];  }| j	                  ||      D ]#  }|j                  t        t        |             % = y|D ]^  }| j	                  |d      D ]F  \  }}	||	g}	 |j                  fd|D               |j                  t        t        |             H ` yc c}}w # t        $ r}t        d      |d}~ww xY w# t        $ r Y Tw xY ww)	a  Generate a single line of the bipartite graph G in edge list format.

    Parameters
    ----------
    G : NetworkX graph
       The graph is assumed to have node attribute `part` set to 0,1 representing
       the two graph parts

    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
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> G.add_nodes_from([0, 2], bipartite=0)
    >>> G.add_nodes_from([1, 3], bipartite=1)
    >>> G[1][2]["weight"] = 3
    >>> G[2][3]["capacity"] = 12
    >>> for line in bipartite.generate_edgelist(G, data=False):
    ...     print(line)
    0 1
    2 1
    2 3

    >>> for line in bipartite.generate_edgelist(G):
    ...     print(line)
    0 1 {}
    2 1 {'weight': 3}
    2 3 {'capacity': 12}

    >>> for line in bipartite.generate_edgelist(G, data=["weight"]):
    ...     print(line)
    0 1
    2 1 3
    2 3
    	bipartiter   z"Missing node attribute `bipartite`NTF)r   c              3   (   K   | ]	  }|     y wN ).0kds     r   	<genexpr>z$generate_edgelist.<locals>.<genexpr>   s     3!3s   )
nodesitemsBaseExceptionAttributeErroredgesjoinmapstrextendKeyError)
r   r   r   nr!   part0erredgeuvs
       `     r   r   r   R   s+    bL ww}}Ftq!!K.A2EFF t|tu} 	5A- 5nnSd^445	5  	5A771470 51a1vKK3d33  nnSd^445	5 G LABKL   sa   DC- C'C'C- A,D'D
&D'C- -	D6DDD
	DDDDbipartite_parse_edgelist)namegraphsreturns_graphc                    ddl m} t        j                  d|      }| D ]c  }|j	                  |      }	|	dk\  r|d|	 }t        |      s+|j                  d      j                  |      }
t        |
      dk  rZ|
j                  d      }|
j                  d      }|
}|	  ||      } ||      }t        |      dk(  s|d
u ri }n|du r"	 t         |dj                  |                  }n^t        |      t        |      k7  rt        d| d| d      i }t        ||      D ]$  \  \  }}}	  ||      }|j                  ||i       & |j!                  |d       |j!                  |d        |j"                  ||fi | f |S # t        $ r}t        d| d| d| d	      |d}~ww xY w# t        $ r}t        d| d      |d}~ww xY w# t        $ r}t        d| d| d| d	      |d}~ww xY w)a  Parse lines of an edge list representation of a bipartite 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 bipartite graph corresponding to lines

    Examples
    --------
    Edgelist with no data:

    >>> from networkx.algorithms import bipartite
    >>> lines = ["1 2", "2 3", "3 4"]
    >>> G = bipartite.parse_edgelist(lines, nodetype=int)
    >>> sorted(G.nodes())
    [1, 2, 3, 4]
    >>> sorted(G.nodes(data=True))
    [(1, {'bipartite': 0}), (2, {'bipartite': 0}), (3, {'bipartite': 0}), (4, {'bipartite': 1})]
    >>> sorted(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 = bipartite.parse_edgelist(lines, nodetype=int)
    >>> sorted(G.nodes())
    [1, 2, 3, 4]
    >>> sorted(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 = bipartite.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
    >>> sorted(G.nodes())
    [1, 2, 3, 4]
    >>> sorted(G.edges(data=True))
    [(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]

    See Also
    --------
    r   )literal_evalNr      zFailed to convert nodes ,z	 to type .FT zFailed to convert edge data (z) to dictionary.z
Edge data z and data_keys z are not the same lengthzFailed to convert z data )r   r	   )astr8   nxempty_graphfindlenrstripsplitpopr%   	TypeErrordictr(   
IndexErrorzipupdateadd_nodeadd_edge)linesr   r   create_usingnodetyper   r8   r   r   psr1   r2   r!   r/   edgedataedge_key	edge_type
edge_values                      r   r   r      sP   ~ !
q,'A 3%IIh68D4yKK##I.q6A:EE!HEE!HQKQK q6Q;$%-HT\SXXa[ 9: 1vT"  ?4&8PQ  H58q\ 81%9z!*:!6J : 678 	


1
"	

1
"

1a$8$g3%h HG ! .qc1#YxjJ ! 3A36FG % #,XJf%,i	{!= sH    F
 F/,G	F,F''F,/	G8GG	G4G//G4rbbipartite_read_edgelistc                 <    fd| D        }t        ||||||      S )a  Read a bipartite 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 decompressed.
    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
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> G.add_nodes_from([0, 2], bipartite=0)
    >>> G.add_nodes_from([1, 3], bipartite=1)
    >>> bipartite.write_edgelist(G, "test.edgelist")
    >>> G = bipartite.read_edgelist("test.edgelist")

    >>> fh = open("test.edgelist", "rb")
    >>> G = bipartite.read_edgelist(fh)
    >>> fh.close()

    >>> G = bipartite.read_edgelist("test.edgelist", nodetype=int)

    Edgelist with data in a list:

    >>> textline = "1 2 3"
    >>> fh = open("test.edgelist", "w")
    >>> d = fh.write(textline)
    >>> fh.close()
    >>> G = bipartite.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.)
    c              3   @   K   | ]  }|j                          y wr   )decode)r   r   r   s     r   r"   z read_edgelist.<locals>.<genexpr>`  s     4tT[["4s   )r   r   rM   rN   r   )r   )	r   r   r   rM   rN   r   edgetyper   rL   s	          ` r   r   r     s/    b 5t4E! r   )#r<   Tutf-8)r<   T)r[   NNNT)r[   NNNTNr\   )__doc____all__networkxr>   networkx.utilsr   r   r   r   _dispatchabler   r   r   r   r   <module>rb      s   2 U  9 14.* .*b Z @5 !@5F 1$dSPTu Tup 140TR 	W S Wr   