ó
ú£Õ\c           @   sÔ   d  Z  d j d d d g ƒ Z d d d d g Z d	 d
 l m Z m Z d	 d l Z d d „ Z	 e d d d ƒd d d d „ ƒ Z
 d d d d d d „ Z e d d d ƒd d d d d d d „ ƒ Z d „  Z d S(   s   
*************************
Multi-line Adjacency List
*************************
Read and write NetworkX graphs as multi-line adjacency lists.

The multi-line adjacency list format is useful for graphs with 
nodes that can be meaningfully represented as strings.  With this format 
simple edge data can be stored but node or graph data is not. 

Format
------
The first label in a line is the source node label followed by the node degree
d.  The next d lines are target node labels and optional edge data.
That pattern repeats for all nodes in the graph.

The graph with edges a-b, a-c, d-e can be represented as the following 
adjacency list (anything following the # in a line is a comment):: 

     # example.multiline-adjlist
     a 2
     b
     c
     d 1
     e
s   
s   Aric Hagberg <hagberg@lanl.gov>s    Dan Schult <dschult@colgate.edu>s'   LoÃ¯c SÃ©guin-C. <loicseguin@gmail.com>t   generate_multiline_adjlistt   write_multiline_adjlistt   parse_multiline_adjlistt   read_multiline_adjlistiÿÿÿÿ(   t   make_strt	   open_fileNt    c         c   sV  |  j  ƒ  r||  j ƒ  ré x^|  j ƒ  D]½ \ } } g  | j ƒ  D]1 \ } } | j ƒ  D] \ } } | | f ^ qT q> } t | ƒ }	 t | ƒ | d |	 VxG | D]? \ } }
 |
 d k rÅ t | ƒ VqŸ t | ƒ | t |
 ƒ VqŸ Wq% WqRxf|  j ƒ  D] \ } } t | ƒ }	 t | ƒ | d |	 VxM | j ƒ  D]? \ } }
 |
 d k rXt | ƒ Vq2t | ƒ | t |
 ƒ Vq2Wqö WnÖ|  j ƒ  r{t ƒ  } x¾|  j ƒ  D]Ö \ } } g  | j ƒ  D]= \ } } | | k r·| j ƒ  D] \ } } | | f ^ qÙq·} t | ƒ }	 t | ƒ | d |	 VxG | D]? \ } }
 |
 d k rJt | ƒ Vq$t | ƒ | t |
 ƒ Vq$W| j | ƒ qžWn× t ƒ  } xË |  j ƒ  D]½ \ } } g  | j ƒ  D]$ \ } }
 | | k rª| |
 f ^ qª} t | ƒ }	 t | ƒ | d |	 VxG | D]? \ } }
 |
 d k r$t | ƒ Vqþt | ƒ | t |
 ƒ VqþW| j | ƒ q‘Wd S(   s”  Generate a single line of the graph G in multiline adjacency list format.

    Parameters
    ----------
    G : NetworkX graph
    
    delimiter : string, optional
       Separator for node labels 

    Returns
    -------
    lines : string
        Lines of data in multiline adjlist format.

    Examples
    --------
    >>> G = nx.lollipop_graph(4, 3)
    >>> for line in nx.generate_multiline_adjlist(G):
    ...     print(line)    
    0 3
    1 {}
    2 {}
    3 {}
    1 2
    2 {}
    3 {}
    2 1
    3 {}
    3 1
    4 {}
    4 1
    5 {}
    5 1
    6 {}
    6 0

    See Also
    --------
    write_multiline_adjlist, read_multiline_adjlist
    s   %iN(	   t   is_directedt   is_multigrapht   adjacency_itert   itemst   lenR   t   Nonet   sett   add(   t   Gt	   delimitert   st   nbrst   ut   datadictt   keyt   datat	   nbr_edgest   degt   dt   seen(    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pyR    .   sV    )+$$	+	7i   t   modet   wbt   #s   utf-8c   
      C   s¶   d d l  } d d l } | d j | j ƒ } d | | d | j | j ƒ  ƒ | d |  j } | j | j | ƒ ƒ x7 t	 |  | ƒ D]& }	 |	 d 7}	 | j |	 j | ƒ ƒ qˆ Wd S(   s)   Write the graph G in multiline adjacency list format to path

    Parameters
    ----------
    G : NetworkX graph
    
    comments : string, optional
       Marker for comment lines

    delimiter : string, optional
       Separator for node labels 

    encoding : string, optional
       Text encoding. 

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> nx.write_multiline_adjlist(G,"test.adjlist")

    The path can be a file handle or a string with the name of the file. If a
    file handle is provided, it has to be opened in 'wb' mode.

    >>> fh=open("test.adjlist",'wb')
    >>> nx.write_multiline_adjlist(G,fh)

    Filenames ending in .gz or .bz2 will be compressed.

    >>> nx.write_multiline_adjlist(G,"test.adjlist.gz")

    See Also
    --------
    read_multiline_adjlist
    iÿÿÿÿNR   s   %s
s    GMT %s
s    %s
s   
(
   t   syst   timet   joint   argvt   asctimet   gmtimet   namet   writet   encodeR    (
   R   t   pathR   t   commentst   encodingR   R   t   pargst   headert	   multiline(    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pyR   Š   s    %4
c         C   sÎ  d d l  m } | d k r+ t j ƒ  } n* y | } | j ƒ  Wn t d ƒ ‚ n Xxr|  D]j} | j | ƒ }	 |	 d k rŠ | |	  } n  | s– q\ n  y+ | j ƒ  j	 | ƒ \ }
 } t
 | ƒ } Wn t d | ƒ ‚ n X| d k	 ry | |
 ƒ }
 Wqt d |
 | f ƒ ‚ qXn  | j |
 ƒ xœt | ƒ D]Ž} xy t rµy t |  ƒ } Wn* t k
 rd |
 f } t | ƒ ‚ n X| j | ƒ }	 |	 d k r¨| |	  } n  | r=Pq=q=W| j ƒ  j	 | ƒ } t | ƒ } | d k  réq4n  | j d ƒ } d	 j | ƒ } | d k	 rFy | | ƒ } WqFt d | | f ƒ ‚ qFXn  | d k	 rŒy i | | ƒ d
 6} Wq¬t d | | f ƒ ‚ q¬Xn  y | | ƒ } Wn i  } n X| j |
 | d | ƒq4Wq\ W| S(   sÌ  Parse lines of a multiline adjacency list representation of a graph.

    Parameters
    ----------
    lines : list or iterator of strings
        Input data in multiline adjlist format

    create_using: NetworkX graph container       
       Use given NetworkX graph for holding nodes or edges.

    nodetype : Python type, optional
       Convert nodes to this type.  
       
    comments : string, optional
       Marker for comment lines

    delimiter : string, optional
       Separator for node labels.  The default is whitespace. 

    create_using: NetworkX graph container       
       Use given NetworkX graph for holding nodes or edges.


    Returns
    -------
    G: NetworkX graph
        The graph corresponding to the lines in multiline adjacency list format.

    Examples
    --------
    >>> lines = ['1 2',
    ...          "2 {'weight':3, 'name': 'Frodo'}",
    ...          "3 {}",
    ...          "2 1",
    ...          "5 {'weight':6, 'name': 'Saruman'}"]
    >>> G = nx.parse_multiline_adjlist(iter(lines), nodetype = int)
    >>> G.nodes()
    [1, 2, 3, 5]
    >>> G.edges(data = True)
    [(1, 2, {'name': 'Frodo', 'weight': 3}), (1, 3, {}), (2, 5, {'name': 'Saruman', 'weight': 6})]
    
    iÿÿÿÿ(   t   literal_evals(   Input graph is not a networkx graph typei    s+   Failed to read node and degree on line (%s)s&   Failed to convert node (%s) to type %ss%   Failed to find neighbor for node (%s)i   t    t   weights+   Failed to convert edge data (%s) to type %st	   attr_dictN(   t   astR-   R   t   nxt   Grapht   cleart	   TypeErrort   findt   stript   splitt   intt   add_nodet   ranget   Truet   nextt   StopIterationR   t   popR    t   add_edge(   t   linesR(   R   t   create_usingt   nodetypet   edgetypeR-   R   t   linet   pR   R   t   it   msgt   vlistt   numbt   vR   t   edgedata(    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pyR   ¼   s€    - 	 
i    t   rbc            s>   ‡  f d †  |  Dƒ } t  | d | d | d | d | d | ƒS(   s,  Read graph in multi-line adjacency list format from path.

    Parameters
    ----------
    path : string or file
       Filename or file handle to read.
       Filenames ending in .gz or .bz2 will be uncompressed.

    create_using: NetworkX graph container       
       Use given NetworkX graph for holding nodes or edges.

    nodetype : Python type, optional
       Convert nodes to this type.  
       
    edgetype : Python type, optional
       Convert edge data to this type.

    comments : string, optional
       Marker for comment lines

    delimiter : string, optional
       Separator for node labels.  The default is whitespace. 

    create_using: NetworkX graph container       
       Use given NetworkX graph for holding nodes or edges.


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

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> nx.write_multiline_adjlist(G,"test.adjlist")
    >>> G=nx.read_multiline_adjlist("test.adjlist")

    The path can be a file or a string with the name of the file. If a
    file s provided, it has to be opened in 'rb' mode.

    >>> fh=open("test.adjlist", 'rb')
    >>> G=nx.read_multiline_adjlist(fh)

    Filenames ending in .gz or .bz2 will be compressed.

    >>> nx.write_multiline_adjlist(G,"test.adjlist.gz")
    >>> G=nx.read_multiline_adjlist("test.adjlist.gz")

    The optional nodetype is a function to convert node strings to nodetype.

    For example

    >>> G=nx.read_multiline_adjlist("test.adjlist", nodetype=int)

    will attempt to convert all nodes to integer type.

    The optional edgetype is a function to convert edge data strings to 
    edgetype.  

    >>> G=nx.read_multiline_adjlist("test.adjlist")

    The optional create_using parameter is a NetworkX graph container.
    The default is Graph(), an undirected graph.  To read the data as 
    a directed graph use

    >>> G=nx.read_multiline_adjlist("test.adjlist", create_using=nx.DiGraph())
    
    Notes
    -----
    This format does not store graph, node, or edge data.

    See Also
    --------
    write_multiline_adjlist
    c         3   s   |  ] } | j  ˆ  ƒ Vq d  S(   N(   t   decode(   t   .0RE   (   R)   (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pys	   <genexpr>|  s    R(   R   RB   RC   RD   (   R   (   R'   R(   R   RB   RC   RD   R)   RA   (    (   R)   s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pyR   ,  s    P	c         C   s*   d d  l  } | j d ƒ | j d ƒ d  S(   Niÿÿÿÿs   test.adjlists   test.adjlist.gz(   t   ost   unlink(   t   moduleRP   (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pyt   teardown_module†  s    (   t   __doc__R    t
   __author__t   __all__t   networkx.utilsR   R   t   networkxR2   R    R   R   R   R   RS   (    (    (    s\   /Users/dxp/prism/prism-games/prism-examples/smgs/car/networkx/readwrite/multiline_adjlist.pyt   <module>   s,   		\0nV