ó
|£*^c        	   @   sÞ   d  Z  d d d d g Z d d l Z d d l m Z m Z m Z d d l m	 Z	 e d	 d
 d ƒd d e
 d d „ ƒ Z e d ƒ d e
 d „ ƒ Z d d d d e
 d „ Z e d d
 d ƒd d d d e
 d d d „ ƒ Z d S(   sŽ  
**********
Bipartite Edge Lists
**********
Read and write NetworkX graphs as bipartite edge lists.

Format
------
You can read or write three formats of edge lists with these functions.

Node pairs with no data::

 1 2

Python dictionary as data::

 1 2 {'weight':7, 'color':'green'}

Arbitrary data::

 1 2 7 green

For each edge (u, v) the node u is assigned to part 0 and the node v to part 1.
t   generate_edgelistt   write_edgelistt   parse_edgelistt   read_edgelistiÿÿÿÿN(   t	   open_filet   make_strt   not_implemented_for(   t   _prep_create_usingi   t   modet   wbt   #t    s   utf-8c         C   sA   x: t  |  | | ƒ D]& } | d 7} | j | j | ƒ ƒ q Wd S(   sñ  Write a bipartite graph as a list of edges.

    Parameters
    ----------
    G : Graph
       A NetworkX bipartite graph
    path : file or string
       File or filename to write. If a file is provided, it must be
       opened in 'wb' mode. Filenames ending in .gz or .bz2 will be compressed.
    comments : string, optional
       The character used to indicate the start of a comment
    delimiter : string, optional
       The string used to separate values.  The default is whitespace.
    data : bool or list, optional
       If False write no edge data.
       If True write a string representation of the edge data dictionary..
       If a list (or other iterable) is provided, write the  keys specified
       in the list.
    encoding: string, optional
       Specify which encoding to use when writing file.

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> G.add_nodes_from([0,2], bipartite=0)
    >>> G.add_nodes_from([1,3], bipartite=1)
    >>> nx.write_edgelist(G, "test.edgelist")
    >>> fh=open("test.edgelist",'wb')
    >>> nx.write_edgelist(G, fh)
    >>> nx.write_edgelist(G, "test.edgelist.gz")
    >>> nx.write_edgelist(G, "test.edgelist.gz", data=False)

    >>> G=nx.Graph()
    >>> G.add_edge(1,2,weight=7,color='red')
    >>> nx.write_edgelist(G,'test.edgelist',data=False)
    >>> nx.write_edgelist(G,'test.edgelist',data=['color'])
    >>> nx.write_edgelist(G,'test.edgelist',data=['color','weight'])

    See Also
    --------
    write_edgelist()
    generate_edgelist()
    s   
N(   R    t   writet   encode(   t   Gt   patht   commentst	   delimitert   datat   encodingt   line(    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pyR   (   s    .
t   directedc         #   sK  y< g  |  j  j ƒ  D]" \ } ‰  ˆ  d d k r | ^ q } Wn t d ƒ ‚ n X| t k sj | t k rµ xÚ | D]= } x4 |  j | d | ƒD] } | j t t | ƒ ƒ Vq� Wqq Wn’ x� | D]‡ } x~ |  j | d t ƒD]g \ } } ‰  | | g } y! | j	 ‡  f d †  | Dƒ ƒ Wn t
 k
 r'n X| j t t | ƒ ƒ VqØ Wq¼ Wd S(   sÊ  Generate a single line of the bipartite graph G in edge list format.

    Parameters
    ----------
    G : NetworkX graph
       The graph is assumed to have node attribute `part` set to 0,1 representing
       the two graph parts

    delimiter : string, optional
       Separator for node labels

    data : bool or list of keys
       If False generate no edge data.  If True use a dictionary
       representation of edge data.  If a list of keys use a list of data
       values corresponding to the keys.

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

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> G.add_nodes_from([0,2], bipartite=0)
    >>> G.add_nodes_from([1,3], bipartite=1)
    >>> G[1][2]['weight'] = 3
    >>> G[2][3]['capacity'] = 12
    >>> for line in bipartite.generate_edgelist(G, data=False):
    ...     print(line)
    0 1
    2 1
    2 3

    >>> for line in bipartite.generate_edgelist(G):
    ...     print(line)
    0 1 {}
    2 1 {'weight': 3}
    2 3 {'capacity': 12}

    >>> for line in bipartite.generate_edgelist(G,data=['weight']):
    ...     print(line)
    0 1
    2 1 3
    2 3
    t	   bipartitei    s"   Missing node attribute `bipartite`R   c         3   s   |  ] } ˆ  | Vq d  S(   N(    (   t   .0t   k(   t   d(    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pys	   <genexpr>™   s    N(   t   nodet   itemst   AttributeErrort   Truet   Falset   edgest   joint   mapR   t   extendt   KeyError(   R   R   R   t   nt   part0t   et   ut   v(    (   R   sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pyR    [   s     1<"%!c         C   sY  d d l  m } t | ƒ } x6|  D].} | j | ƒ }	 |	 d k rQ | |	  } n  t | ƒ sc q# n  | j ƒ  j | ƒ }
 t |
 ƒ d k  r� q# n  |
 j d ƒ } |
 j d ƒ } |
 } | d k	 ry | | ƒ } | | ƒ } Wqt	 d | | | f ƒ ‚ qXn  t | ƒ d k s | t
 k r)i  } nì | t k rqy t | d j | ƒ ƒ ƒ } Wqt	 d | ƒ ‚ qXn¤ t | ƒ t | ƒ k r¢t d | | f ƒ ‚ n  i  } xj t | | ƒ D]Y \ \ } } } y | | ƒ } Wn  t	 d	 | | | f ƒ ‚ n X| j i | | 6ƒ q¸W| j | d
 d ƒ| j | d
 d ƒ| j | | d | ƒq# W| S(   s  Parse lines of an edge list representation of a bipartite graph.

    Parameters
    ----------
    lines : list or iterator of strings
        Input data in edgelist format
    comments : string, optional
       Marker for comment lines
    delimiter : string, optional
       Separator for node labels
    create_using: NetworkX graph container, optional
       Use given NetworkX graph for holding nodes or edges.
    nodetype : Python type, optional
       Convert nodes to this type.
    data : bool or list of (label,type) tuples
       If False generate no edge data or if True use a dictionary
       representation of edge data or a list tuples specifying dictionary
       key names and types for edge data.

    Returns
    -------
    G: NetworkX Graph
        The bipartite graph corresponding to lines

    Examples
    --------
    Edgelist with no data:

    >>> from networkx.algorithms import bipartite
    >>> lines = ["1 2",
    ...          "2 3",
    ...          "3 4"]
    >>> G = bipartite.parse_edgelist(lines, nodetype = int)
    >>> sorted(G.nodes())
    [1, 2, 3, 4]
    >>> sorted(G.nodes(data=True))
    [(1, {'bipartite': 0}), (2, {'bipartite': 0}), (3, {'bipartite': 0}), (4, {'bipartite': 1})]
    >>> sorted(G.edges())
    [(1, 2), (2, 3), (3, 4)]

    Edgelist with data in Python dictionary representation:

    >>> lines = ["1 2 {'weight':3}",
    ...          "2 3 {'weight':27}",
    ...          "3 4 {'weight':3.0}"]
    >>> G = bipartite.parse_edgelist(lines, nodetype = int)
    >>> sorted(G.nodes())
    [1, 2, 3, 4]
    >>> sorted(G.edges(data = True))
    [(1, 2, {'weight': 3}), (2, 3, {'weight': 27}), (3, 4, {'weight': 3.0})]

    Edgelist with data in a list:

    >>> lines = ["1 2 3",
    ...          "2 3 27",
    ...          "3 4 3.0"]
    >>> G = bipartite.parse_edgelist(lines, nodetype = int, data=(('weight',float),))
    >>> sorted(G.nodes())
    [1, 2, 3, 4]
    >>> sorted(G.edges(data = True))
    [(1, 2, {'weight': 3.0}), (2, 3, {'weight': 27.0}), (3, 4, {'weight': 3.0})]

    See Also
    --------
    iÿÿÿÿ(   t   literal_evali    i   s)   Failed to convert nodes %s,%s to type %s.R   s/   Failed to convert edge data (%s) to dictionary.s5   Edge data %s and data_keys %s are not the same lengths(   Failed to convert %s data %s to type %s.R   i   t	   attr_dictN(   t   astR)   R   t   findt   lent   stript   splitt   popt   Nonet	   TypeErrorR   R   t   dictR    t
   IndexErrort   zipt   updatet   add_nodet   add_edge(   t   linesR   R   t   create_usingt   nodetypeR   R)   R   R   t   pt   sR'   R(   R   t   edgedatat   edge_keyt	   edge_typet
   edge_value(    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pyR   Ÿ   s\    C	"i    t   rbc   	         s>   ‡  f d †  |  Dƒ } t  | d | d | d | d | d | ƒS(   sÇ  Read a bipartite graph from a list of edges.

    Parameters
    ----------
    path : file or string
       File or filename to read. If a file is provided, it must be
       opened in 'rb' mode.
       Filenames ending in .gz or .bz2 will be uncompressed.
    comments : string, optional
       The character used to indicate the start of a comment.
    delimiter : string, optional
       The string used to separate values.  The default is whitespace.
    create_using : Graph container, optional,
       Use specified container to build graph.  The default is networkx.Graph,
       an undirected graph.
    nodetype : int, float, str, Python type, optional
       Convert node data from strings to specified type
    data : bool or list of (label,type) tuples
       Tuples specifying dictionary key names and types for edge data
    edgetype : int, float, str, Python type, optional OBSOLETE
       Convert edge data from strings to specified type and use as 'weight'
    encoding: string, optional
       Specify which encoding to use when reading file.

    Returns
    -------
    G : graph
       A networkx Graph or other type specified with create_using

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> G.add_nodes_from([0,2], bipartite=0)
    >>> G.add_nodes_from([1,3], bipartite=1)
    >>> bipartite.write_edgelist(G, "test.edgelist")
    >>> G = bipartite.read_edgelist("test.edgelist")

    >>> fh = open("test.edgelist", 'rb')
    >>> G = bipartite.read_edgelist(fh)
    >>> fh.close()

    >>> G=bipartite.read_edgelist("test.edgelist", nodetype=int)

    Edgelist with data in a list:

    >>> textline = '1 2 3'
    >>> fh = open('test.edgelist','w')
    >>> d = fh.write(textline)
    >>> fh.close()
    >>> G = bipartite.read_edgelist('test.edgelist', nodetype=int, data=(('weight',float),))
    >>> G.nodes()
    [1, 2]
    >>> G.edges(data = True)
    [(1, 2, {'weight': 3.0})]

    See parse_edgelist() for more examples of formatting.

    See Also
    --------
    parse_edgelist

    Notes
    -----
    Since nodes must be hashable, the function nodetype must return hashable
    types (e.g. int, float, str, frozenset - or tuples of those, etc.)
    c         3   s   |  ] } | j  ˆ  ƒ Vq d  S(   N(   t   decode(   R   R   (   R   (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pys	   <genexpr>`  s    R   R   R:   R;   R   (   R   (	   R   R   R   R:   R;   R   t   edgetypeR   R9   (    (   R   sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pyR     s    H(   t   __doc__t   __all__t   networkxt   nxt   networkx.utilsR   R   R   t   networkx.convertR   R   R   R    R1   R   R   (    (    (    sz   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/edgelist.pyt   <module>   s&   		1	Cx	