ó žÃÒYc @sÞdZddddgZddlZddlmZmZmZddlm Z ed d d ƒd d e dd„ƒZ edƒd e d„ƒZ d ddde d„Zedd dƒd ddde ddd„ƒZdS(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. tgenerate_edgelisttwrite_edgelisttparse_edgelistt read_edgelistiÿÿÿÿN(t open_filetmake_strtnot_implemented_for(t_prep_create_usingitmodetwbt#t sutf-8cCsAx:t|||ƒD]&}|d7}|j|j|ƒƒqWdS(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(Rtwritetencode(tGtpathtcommentst delimitertdatatencodingtline((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pyR(s. tdirectedc#sKy<g|jjƒD]"\}‰ˆddkr|^q}Wntdƒ‚nX|tksj|tkrµxÚ|D]=}x4|j|d|ƒD]}|jtt|ƒƒVqWqqWn’x|D]‡}x~|j|dtƒD]g\}}‰||g}y!|j ‡fd†|DƒƒWnt k r'nX|jtt|ƒƒVqØWq¼WdS(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 bipartiteis"Missing node attribute `bipartite`Rc3s|]}ˆ|VqdS(N((t.0tk(td(s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pys ™sN( tnodestitemstAttributeErrortTruetFalsetedgestjointmapRtextendtKeyError(RRRtntpart0tetutv((Rs|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pyR[s 1< " % ! cCsVddlm}t|ƒ}x3|D]+}|j|ƒ} | dkrQ|| }nt|ƒscq#n|jƒj|ƒ} t| ƒdkrq#n| jdƒ} | jdƒ} | } |d k ry|| ƒ} || ƒ} Wqt d| | |fƒ‚qXnt| ƒdks |t kr)i}nì|t krqyt |dj | ƒƒƒ}Wqt d| ƒ‚qXn¤t| ƒt|ƒkr¢td| |fƒ‚ni}xjt|| ƒD]Y\\}}}y||ƒ}Wn t d |||fƒ‚nX|ji||6ƒq¸W|j| d dƒ|j| d d ƒ|j| | |q#W|S( sParse 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_evaliis)Failed to convert nodes %s,%s to type %s.R s/Failed to convert edge data (%s) to dictionary.s5Edge data %s and data_keys %s are not the same lengths(Failed to convert %s data %s to type %s.RiN(tastR)RtfindtlentstriptsplittpoptNonet TypeErrorRRtdictR t IndexErrortziptupdatetadd_nodetadd_edge(tlinesRRt create_usingtnodetypeRR)RRtptsR'R(Rtedgedatatedge_keyt edge_typet edge_value((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pyRŸs\C         "itrbc s>‡fd†|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),)) >>> list(G) [1, 2] >>> list(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.) c3s|]}|jˆƒVqdS(N(tdecode(RR(R(s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pys `sRRR9R:R(R( RRRR9R:RtedgetypeRR8((Rs|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pyRs H(t__doc__t__all__tnetworkxtnxtnetworkx.utilsRRRtnetworkx.convertRRRRR0RR(((s|/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/bipartite/edgelist.pyts&   1 Cx