o
    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dZ
ej	d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   s>   | 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 n ty_ }	 z|	 jd	7  _ d
}	~	ww |||< |
dd}
dtt|||||
f}| D ]1\}}t|tr| dkr|dt| dt| 7 }q{td| dt|trdnd d q{|V  q(|  rdV  ndV  | jddD ]Z\}}}|	 }|
dd}dtt|| || |f}| D ]4\}}t|tr| dkr|dt| dt| 7 }qt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.
     NetworkXz
*vertices    xg        yid)zSPajek format requires 'id' to be an int(). Refer to the 'Relabeling nodes' section.Nshape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warnis_directededges)Gr   r   
nodenumbernnar
   r   r   errr   skvuedgedatadvalue r:   l/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/networkx/readwrite/pajek.pyr      sV   
 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liner:   r:   r;   r   c   s   r   rbT)graphsreturns_graphc                    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.0rD   rC   r:   r;   	<genexpr>   s    zread_pajek.<locals>.<genexpr>)r   )rB   rC   linesr:   rL   r;   r      s   r   c              	      sZ  ddl }t| trt| d} tdd | D } t }g  | rzt| }W n   Y |S | 	drRz
|dd\}}W n
 t
yJ   Y n_w ||jd< nW| 	d	ri }| \}}tt|D ]|}t| }zd
d |t|dD }	W n ty   |t|}	Y nw |	dd \}
} | || |||
< |
|j| d< z|	dd \}}}|j| t|t||d W n   Y t|	ddd |	ddd }|j| | qgn| 	ds| 	dr| 	drt|}| 	dr| }| D ]{}zdd |t|dD }	W n ty3   |t|}	Y nw t|	dk r=q|	dd \}}|||}|||}i }z|	dd }|dt|d i W n   Y t|	ddd |	ddd }|| |j||fi | qn| 	drt|} fddt| D }|| | s"|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      g | ]}| d qS )r?   )rstriprJ   r:   r:   r;   
<listcomp>   s    zparse_pajek.<locals>.<listcomp>z*networkr	   r   z	*verticesc                 S   rO   utf-8rH   rK   r
   r:   r:   r;   rQ          
rS      r      )r
   r   r      r   r   z*edgec                 S   rO   rR   rH   rT   r:   r:   r;   rQ      rU      r      z*matrixc                 3   sP    | ]#\}}t | D ]\}}t|d kr |  | dt|ifV  qqdS )r   r   N)	enumeratesplitr    )rK   rowrD   colr   labelsr:   r;   rM     s    
zparse_pajek.<locals>.<genexpr>)shlexr'   r(   iterr\   nxMultiDiGraphnextlower
startswithr!   graphr   r    rA   AttributeErrorappendadd_noder   updatefloatr   
MultiGraphto_directedr   r   add_edgeDiGraphr[   add_edges_from)rN   ra   r.   llabelr   
nodelabelsnnodesi	splitliner   r
   r   r   
extra_attruivir6   r5   	edge_datawadj_listr:   r_   r;   r      s   
O









Sr   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(   )tr:   r:   r;   r%     s
   
r%   )r>   )__doc__r*   networkxrc   networkx.utilsr   __all__r   r   _dispatchabler   r   r%   r:   r:   r:   r;   <module>   s    
J
  
o