a
    h!                     @   s   d Z ddlZddlZddlmZ g dZdd Zeddd	dddZeddd	ej	dddddZ
ej	dd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)	open_file)
read_pajekparse_pajekgenerate_pajekwrite_pajekc                 c   sX  | j dkrd}n| j }d|   V  t| }tt|tdt|d }|D ]$}| j|i 	 }|
dd}|
dd}zt|
d|| }W n4 ty }	 z|	 jd	7  _ W Y d
}	~	n
d
}	~	0 0 |||< |
dd}
dtt|||||
f}| D ]j\}}t|trB| dkrB|dt| dt| 7 }n(td| dt|tr^dnd d q|V  qN|  rdV  ndV  | jddD ]\}}}|	 }|
dd}dtt|| || |f}| D ]j\}}t|tr| dkr|dt| dt| 7 }n(td| dt|tr:dnd d q|V  qd
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    xg        yid)zSPajek format requires 'id' to be an int(). Refer to the 'Relabeling nodes' section.NshapeZellipse zNode attribute z is not processed. zEmpty attributezNon-string attribute.*arcs*edgesT)dataweightg      ?zEdge attribute )nameorderlistdictziprangelennodesgetcopypopint
ValueErrorargsjoinmap	make_qstritems
isinstancestrstripwarningswarnZis_directededges)Gr   r   Z
nodenumbernnar	   r
   r   errr   skvuZedgedatadvalue r5   F/var/www/auris/lib/python3.9/site-packages/networkx/readwrite/pajek.pyr      sN    

 
 r   r   wb)modeUTF-8c                 C   s*   t | D ]}|d7 }||| q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liner5   r5   r6   r   c   s    r   rb)Zgraphsc                    s    fdd| D }t |S )aZ  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.0r?   r>   r5   r6   	<genexpr>       zread_pajek.<locals>.<genexpr>)r   )r=   r>   linesr5   rE   r6   r      s    r   c              	      sf  ddl }t| tr t| d} tdd | D } t }g  | rbzt| }W n   Y qbY n0 | 	drz|dd\}}W n t
y   Y n0 ||jd< q>| 	d	ri }| \}}tt|D ]}t| }z"d
d |t|dD }	W n" ty"   |t|}	Y n0 |	dd \}
} | || |||
< |
|j| d< z6|	dd \}}}|j| t|t||d W n   Y n0 t|	ddd |	ddd }|j| | qq>| 	ds| 	dr(| 	drt|}| 	dr&| }| D ]}z"dd |t|dD }	W n" tyr   |t|}	Y n0 t|	dk rq*|	dd \}}|||}|||}i }z&|	dd }|dt|d i W n   Y n0 t|	ddd |	ddd }|| |j||fi | q*q>| 	dr>t|} fddt| D }|| q>|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   Nr:   c                 S   s   g | ]}| d qS )r:   )rstriprC   r5   r5   r6   
<listcomp>   rG   zparse_pajek.<locals>.<listcomp>z*networkr   r   z	*verticesc                 S   s   g | ]}| d qS utf-8rA   rD   r	   r5   r5   r6   rJ      s   rL      r      )r	   r
   r      r   r   z*edgec                 S   s   g | ]}| d qS rK   rA   rM   r5   r5   r6   rJ      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   )rD   rowr?   colr   labelsr5   r6   rF     s   zparse_pajek.<locals>.<genexpr>)shlexr%   r&   iterrT   nxZMultiDiGraphnextlower
startswithr   graphr   r   r<   AttributeErrorappendadd_noder   updatefloatr   Z
MultiGraphZto_directedr   r   Zadd_edgeZDiGraphrS   Zadd_edges_from)rH   rY   r+   llabelr   Z
nodelabelsZnnodesiZ	splitliner   r	   r
   r   Z
extra_attrZuivir2   r1   Z	edge_datawZadj_listr5   rW   r6   r      s    




 




r   c                 C   s*   t | tst| } d| v r&d|  d} | S )zcReturns the string representation of t.
    Add outer double-quotes if the string has a space.
    r   ")r%   r&   )tr5   r5   r6   r#     s
    
r#   )r9   )r9   )__doc__r(   Znetworkxr[   Znetworkx.utilsr   __all__r   r   	_dispatchr   r   r#   r5   r5   r5   r6   <module>   s   J
 

 

o