/usr/lib/python3/dist-packages/networkx/algorithms/bipartite/tests/test_edgelist.py is in python3-networkx 1.11-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | """
Unit tests for bipartite edgelists.
"""
from nose.tools import assert_equal, assert_raises, assert_not_equal, raises
import io
import tempfile
import os
import networkx as nx
from networkx.testing import (assert_edges_equal, assert_nodes_equal,
assert_graphs_equal)
from networkx.algorithms import bipartite
class TestEdgelist:
def setUp(self):
self.G=nx.Graph(name="test")
e=[('a','b'),('b','c'),('c','d'),('d','e'),('e','f'),('a','f')]
self.G.add_edges_from(e)
self.G.add_nodes_from(['a','c','e'],bipartite=0)
self.G.add_nodes_from(['b','d','f'],bipartite=1)
self.G.add_node('g',bipartite=0)
self.DG=nx.DiGraph(self.G)
self.MG=nx.MultiGraph()
self.MG.add_edges_from([(1,2),(1,2),(1,2)])
self.MG.add_node(1,bipartite=0)
self.MG.add_node(2,bipartite=1)
def test_read_edgelist_1(self):
s = b"""\
# comment line
1 2
# comment line
2 3
"""
bytesIO = io.BytesIO(s)
G = bipartite.read_edgelist(bytesIO,nodetype=int)
assert_edges_equal(G.edges(),[(1,2),(2,3)])
def test_read_edgelist_3(self):
s = b"""\
# comment line
1 2 {'weight':2.0}
# comment line
2 3 {'weight':3.0}
"""
bytesIO = io.BytesIO(s)
G = bipartite.read_edgelist(bytesIO,nodetype=int,data=False)
assert_edges_equal(G.edges(),[(1,2),(2,3)])
bytesIO = io.BytesIO(s)
G = bipartite.read_edgelist(bytesIO,nodetype=int,data=True)
assert_edges_equal(G.edges(data=True),
[(1,2,{'weight':2.0}),(2,3,{'weight':3.0})])
def test_write_edgelist_1(self):
fh=io.BytesIO()
G=nx.Graph()
G.add_edges_from([(1,2),(2,3)])
G.add_node(1,bipartite=0)
G.add_node(2,bipartite=1)
G.add_node(3,bipartite=0)
bipartite.write_edgelist(G,fh,data=False)
fh.seek(0)
assert_equal(fh.read(),b"1 2\n3 2\n")
def test_write_edgelist_2(self):
fh=io.BytesIO()
G=nx.Graph()
G.add_edges_from([(1,2),(2,3)])
G.add_node(1,bipartite=0)
G.add_node(2,bipartite=1)
G.add_node(3,bipartite=0)
bipartite.write_edgelist(G,fh,data=True)
fh.seek(0)
assert_equal(fh.read(),b"1 2 {}\n3 2 {}\n")
def test_write_edgelist_3(self):
fh=io.BytesIO()
G=nx.Graph()
G.add_edge(1,2,weight=2.0)
G.add_edge(2,3,weight=3.0)
G.add_node(1,bipartite=0)
G.add_node(2,bipartite=1)
G.add_node(3,bipartite=0)
bipartite.write_edgelist(G,fh,data=True)
fh.seek(0)
assert_equal(fh.read(),b"1 2 {'weight': 2.0}\n3 2 {'weight': 3.0}\n")
def test_write_edgelist_4(self):
fh=io.BytesIO()
G=nx.Graph()
G.add_edge(1,2,weight=2.0)
G.add_edge(2,3,weight=3.0)
G.add_node(1,bipartite=0)
G.add_node(2,bipartite=1)
G.add_node(3,bipartite=0)
bipartite.write_edgelist(G,fh,data=[('weight')])
fh.seek(0)
assert_equal(fh.read(),b"1 2 2.0\n3 2 3.0\n")
def test_unicode(self):
G = nx.Graph()
try: # Python 3.x
name1 = chr(2344) + chr(123) + chr(6543)
name2 = chr(5543) + chr(1543) + chr(324)
except ValueError: # Python 2.6+
name1 = unichr(2344) + unichr(123) + unichr(6543)
name2 = unichr(5543) + unichr(1543) + unichr(324)
G.add_edge(name1, 'Radiohead', attr_dict={name2: 3})
G.add_node(name1,bipartite=0)
G.add_node('Radiohead',bipartite=1)
fd, fname = tempfile.mkstemp()
bipartite.write_edgelist(G, fname)
H = bipartite.read_edgelist(fname)
assert_graphs_equal(G, H)
os.close(fd)
os.unlink(fname)
def test_latin1_error(self):
G = nx.Graph()
try: # Python 3.x
name1 = chr(2344) + chr(123) + chr(6543)
name2 = chr(5543) + chr(1543) + chr(324)
except ValueError: # Python 2.6+
name1 = unichr(2344) + unichr(123) + unichr(6543)
name2 = unichr(5543) + unichr(1543) + unichr(324)
G.add_edge(name1, 'Radiohead', attr_dict={name2: 3})
G.add_node(name1,bipartite=0)
G.add_node('Radiohead',bipartite=1)
fd, fname = tempfile.mkstemp()
assert_raises(UnicodeEncodeError,
bipartite.write_edgelist,
G, fname, encoding = 'latin-1')
os.close(fd)
os.unlink(fname)
def test_latin1(self):
G = nx.Graph()
try: # Python 3.x
blurb = chr(1245) # just to trigger the exception
name1 = 'Bj' + chr(246) + 'rk'
name2 = chr(220) + 'ber'
except ValueError: # Python 2.6+
name1 = 'Bj' + unichr(246) + 'rk'
name2 = unichr(220) + 'ber'
G.add_edge(name1, 'Radiohead', attr_dict={name2: 3})
G.add_node(name1,bipartite=0)
G.add_node('Radiohead',bipartite=1)
fd, fname = tempfile.mkstemp()
bipartite.write_edgelist(G, fname, encoding = 'latin-1')
H = bipartite.read_edgelist(fname, encoding = 'latin-1')
assert_graphs_equal(G, H)
os.close(fd)
os.unlink(fname)
def test_edgelist_graph(self):
G=self.G
(fd,fname)=tempfile.mkstemp()
bipartite.write_edgelist(G,fname)
H=bipartite.read_edgelist(fname)
H2=bipartite.read_edgelist(fname)
assert_not_equal(H,H2) # they should be different graphs
G.remove_node('g') # isolated nodes are not written in edgelist
assert_nodes_equal(H.nodes(),G.nodes())
assert_edges_equal(H.edges(),G.edges())
os.close(fd)
os.unlink(fname)
def test_edgelist_integers(self):
G=nx.convert_node_labels_to_integers(self.G)
(fd,fname)=tempfile.mkstemp()
bipartite.write_edgelist(G,fname)
H=bipartite.read_edgelist(fname,nodetype=int)
# isolated nodes are not written in edgelist
G.remove_nodes_from(nx.isolates(G))
assert_nodes_equal(H.nodes(),G.nodes())
assert_edges_equal(H.edges(),G.edges())
os.close(fd)
os.unlink(fname)
def test_edgelist_multigraph(self):
G=self.MG
(fd,fname)=tempfile.mkstemp()
bipartite.write_edgelist(G,fname)
H=bipartite.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph())
H2=bipartite.read_edgelist(fname,nodetype=int,create_using=nx.MultiGraph())
assert_not_equal(H,H2) # they should be different graphs
assert_nodes_equal(H.nodes(),G.nodes())
assert_edges_equal(H.edges(),G.edges())
os.close(fd)
os.unlink(fname)
@raises(nx.NetworkXNotImplemented)
def test_digraph_fail(self):
bytesIO = io.BytesIO()
bipartite.write_edgelist(nx.DiGraph(),bytesIO)
@raises(AttributeError)
def test_attribute_fail(self):
G = nx.path_graph(4)
bytesIO = io.BytesIO()
bipartite.write_edgelist(G,bytesIO)
|