B
    ,["                 @   s   d Z ddlZddlZddlmZmZmZ ddddgZdd Z	ed	d
ddddZ
eddddddZdd Zdd Zdd ZdS )a
  
*****
Pajek
*****
Read graphs in Pajek format.

This implementation handles directed and undirected graphs including
those with self loops and parallel edges.

Format
------
See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
for format information.

    N)is_string_like	open_filemake_str
read_pajekparse_pajekgenerate_pajekwrite_pajekc          
   c   s  | j dkrd}n| j }d|   V  t| }tt|tdt|d }x|D ]}| j|i 	 }|
dd}|
dd}t|
d|| }|||< |
d	d
}	dtt|||||	f}
xb| D ]V\}}t|r| dkr|
dt|t|f 7 }
qtd|t|rdndf  qW |
V  qNW |  r>dV  ndV  x| jddD ]\}}}|	 }|
dd}dtt|| || |f}
xh| D ]\\}}t|r| dkr|
dt|t|f 7 }
n td|t|rdndf  qW |
V  qRW dS )zGenerate lines in Pajek graph format.

    Parameters
    ----------
    G : graph
       A Networkx graph

    References
    ----------
    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.
     ZNetworkXz*vertices %s   xg        yidshapeZellipse z %s %sz'Node attribute %s is not processed. %s.zEmpty attributezNon-string attributez*arcsz*edgesT)dataweightg      ?z'Edge attribute %s is not processed. %s.N)nameorderlistdictziprangelennodesgetcopypopintjoinmap	make_qstritemsr   stripwarningswarnZis_directedZedges)Gr   r   Z
nodenumbernZnar   r   r   r   skvuZedgedatadvalue r-   7lib/python3.7/site-packages/networkx/readwrite/pajek.pyr   !   sJ    



r
   wb)modeUTF-8c             C   s.   x(t | D ]}|d7 }||| q
W dS )a  Write graph in Pajek format to path.

    Parameters
    ----------
    G : graph
       A Networkx graph
    path : file or string
       File or filename to write.
       Filenames ending in .gz or .bz2 will be compressed.

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> nx.write_pajek(G, "test.net")

    Warnings
    --------
    Optional node attributes and edge attributes must be non-empty strings.
    Otherwise it will not be written into the file. You will need to
    convert those attributes to strings if you want to keep them.

    References
    ----------
    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.
    
N)r   writeencode)r%   pathencodingliner-   r-   r.   r   d   s    rbc                s    fdd| D }t |S )aT  Read graph in Pajek format from path.

    Parameters
    ----------
    path : file or string
       File or filename to write.
       Filenames ending in .gz or .bz2 will be uncompressed.

    Returns
    -------
    G : NetworkX MultiGraph or MultiDiGraph.

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> nx.write_pajek(G, "test.net")
    >>> G=nx.read_pajek("test.net")

    To create a Graph instead of a MultiGraph use

    >>> G1=nx.Graph(G)

    References
    ----------
    See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm
    for format information.
    c             3   s   | ]}|  V  qd S )N)decode).0r7   )r6   r-   r.   	<genexpr>   s    zread_pajek.<locals>.<genexpr>)r   )r5   r6   linesr-   )r6   r.   r      s    c          	      sv  ddl }t| rt| d} tdd | D } t }g  x2| rpyt| }W n   P Y nX | dry|dd\}}W n t	k
r   Y nX ||j
d< q@| d	ri }| \}}xtt|D ] }t| }y"d
d |t|dD }	W n$ tk
r*   |t|}	Y nX |	dd \}
} | || |||
< |
|j| d< y6|	dd \}}}|j| t|t||d W n   Y nX t|	ddd |	ddd }|j| | qW q@| ds| dr6| drt|}| dr0| }x:| D ]}y"dd |t|dD }	W n$ tk
r   |t|}	Y nX t|	dk rq8|	dd \}}|||}|||}i }y&|	dd }|dt|d i W n   Y nX t|	ddd |	ddd }|| |j||f| q8W q@| dr@t|} fddt| D }|| q@W |S )zParse Pajek format graph from string or iterable.

    Parameters
    ----------
    lines : string or iterable
       Data in Pajek format.

    Returns
    -------
    G : NetworkX graph

    See Also
    --------
    read_pajek()

    r   Nr2   c             S   s   g | ]}| d qS )r2   )rstrip)r:   r7   r-   r-   r.   
<listcomp>   s    zparse_pajek.<locals>.<listcomp>z*networkr
   r   z	*verticesc             S   s   g | ]}| d qS )zutf-8)r9   )r:   r   r-   r-   r.   r>      s    zutf-8   r      )r   r   r      z*edgesz*arcsz*edgec             S   s   g | ]}| d qS )zutf-8)r9   )r:   r   r-   r-   r.   r>      s       r      z*matrixc             3   sN   | ]F\}}t | D ]0\}}t|d kr |  | dt|ifV  qqdS )r   r   N)	enumeratesplitr   )r:   rowr7   colr   )labelsr-   r.   r;     s   zparse_pajek.<locals>.<genexpr>)shlexr   iterrE   nxZMultiDiGraphnextlower
startswith
ValueErrorZgraphr   r   r   r4   AttributeErrorstrappendZadd_noder   updatefloatr   Z
MultiGraphZto_directedr   r   Zadd_edgeZDiGraphrD   Zadd_edges_from)r<   rI   r%   lZlabelr   Z
nodelabelsZnnodesiZ	splitliner   r   r   r   Z
extra_attrZuiZvir*   r)   Z	edge_datawZadj_listr-   )rH   r.   r      s    


 



c             C   s$   t | st| } d| kr d|  } | S )zbReturn the string representation of t.
    Add outer double-quotes if the string has a space.
    r   z"%s")r   rQ   )tr-   r-   r.   r      s
    r    c             C   s   dd l }|d d S )Nr   ztest.net)osunlink)modulerY   r-   r-   r.   teardown_module  s    r\   )r1   )r1   )__doc__r#   ZnetworkxrK   Znetworkx.utilsr   r   r   __all__r   r   r   r   r    r\   r-   r-   r-   r.   <module>   s   C
 
 j