3
d!                 @   st   d Z ddlZddlZddl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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   sh  | j dkrd}n| j }d| j  V  t| }tt|tdt|d }x0|D ]&}| jj|i j	 }|j
dd}|j
dd}yt|j
d|| }W n2 tk
r }	 z|	 jd7  _ W Y d
d
}	~	X nX |||< |j
dd}
djtt|||||
f}xv|j D ]j\}}t|trF|j dkrF|dt| dt| 7 }n(tjd| dt|trbdnd d qW |V  qRW | j rdV  ndV  x| jddD ]\}}}|j	 }|j
dd}djtt|| || |f}xv|j D ]j\}}t|tr*|j dkr*|dt| dt| 7 }n(tjd| dt|trFdnd d qW |V  qW 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    xg        yidSPajek 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.z*arcsz*edgesT)dataweightg      ?zEdge attribute )r   )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   er   skvuZedgedatadvalue r4   </tmp/pip-build-7vycvbft/networkx/networkx/readwrite/pajek.pyr      sL    
*

*r   wb)modeUTF-8c             C   s.   x(t | D ]}|d7 }|j|j| 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liner4   r4   r5   r   c   s    rbc                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   | ]}|j  V  qd S )N)decode).0r>   )r=   r4   r5   	<genexpr>   s    zread_pajek.<locals>.<genexpr>)r   )r<   r=   linesr4   )r=   r5   r      s    c                sx  ddl }t| tr t| jd} tdd | D } tj }g  x2| rryt| }W n   P Y nX |j j	dry|jdd\}}W n t
k
r   Y nX ||jd< qB|j j	d	ri }|j \}}xtt|D ] }t| }y"d
d |jt|jdD }	W n$ tk
r,   |jt|}	Y nX |	dd \}
} j| |j| |||
< |
|j| d< y6|	dd \}}}|j| jt|t||d W n   Y nX t|	ddd |	ddd }|j| j| qW qB|j j	ds |j j	dr8|j j	drtj|}|j j	dr2|j }x:| D ]}y"dd |jt|jdD }	W n$ tk
r   |jt|}	Y nX t|	dk rq:|	dd \}}|j||}|j||}i }y&|	dd }|jdt|d i W n   Y nX t|	ddd |	ddd }|j| |j||f| q:W qB|j j	drBtj|} fddt| D }|j| qBW |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   Nr9   c             S   s   g | ]}|j d qS )r9   )rstrip)rA   r>   r4   r4   r5   
<listcomp>   s    zparse_pajek.<locals>.<listcomp>z*networkr   r   z	*verticesc             S   s   g | ]}|j d qS )zutf-8)r@   )rA   r	   r4   r4   r5   rE      s    zutf-8   r      )r	   r
   r      z*edgesz*arcsz*edgec             S   s   g | ]}|j d qS )zutf-8)r@   )rA   r	   r4   r4   r5   rE      s       r      z*matrixc             3   sN   | ]F\}}t |j D ]0\}}t|d kr |  | dt|ifV  qqdS )r   r   N)	enumeratesplitr   )rA   rowr>   colr   )labelsr4   r5   rB   
  s   zparse_pajek.<locals>.<genexpr>)shlexr$   r%   iterrL   nxZMultiDiGraphnextlower
startswithr   graphr   r   r;   AttributeErrorappendadd_noder   updatefloatr   Z
MultiGraphZto_directedr   r   Zadd_edgeZDiGraphrK   Zadd_edges_from)rC   rP   r*   llabelr   Z
nodelabelsZnnodesiZ	splitliner   r	   r
   r   Z
extra_attruivir1   r0   Z	edge_datawZadj_listr4   )rO   r5   r      s    

"


 
"


c             C   s*   t | tst| } d| kr&d|  d} | S )zcReturns the string representation of t.
    Add outer double-quotes if the string has a space.
    r   ")r$   r%   )tr4   r4   r5   r"     s
    
r"   )r8   )r8   )__doc__r'   ZnetworkxrR   Znetworkx.utilsr   __all__r   r   r   r   r"   r4   r4   r4   r5   <module>   s   J
 
 o