Skip to content

Commit

Permalink
Merge pull request #1634 from MridulS/selfloops
Browse files Browse the repository at this point in the history
Make selfloop methods return iterator instead of list in graph class
  • Loading branch information
dschult committed Jul 4, 2015
2 parents 7e10758 + 141fbf5 commit 306f660
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion networkx/algorithms/richclub.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def rich_club_coefficient(G, normalized=True, Q=100):
if G.is_multigraph() or G.is_directed():
raise Exception('rich_club_coefficient is not implemented for ',
'directed or multiedge graphs.')
if len(G.selfloop_edges()) > 0:
if G.number_of_selfloops() > 0:
raise Exception('rich_club_coefficient is not implemented for ',
'graphs with self loops.')
rc=_compute_rc(G)
Expand Down
20 changes: 10 additions & 10 deletions networkx/classes/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ def nodes_with_selfloops(self):
[1]
"""
return [n for n, nbrs in self.adj.items() if n in nbrs]
return (n for n, nbrs in self.adj.items() if n in nbrs)

def selfloop_edges(self, data=False, default=None):
"""Return a list of selfloop edges.
Expand Down Expand Up @@ -1542,20 +1542,20 @@ def selfloop_edges(self, data=False, default=None):
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.selfloop_edges()
>>> list(G.selfloop_edges())
[(1, 1)]
>>> G.selfloop_edges(data=True)
>>> list(G.selfloop_edges(data=True))
[(1, 1, {})]
"""
if data is True:
return [(n, n, nbrs[n])
for n, nbrs in self.adj.items() if n in nbrs]
return ((n, n, nbrs[n])
for n, nbrs in self.adj.items() if n in nbrs)
elif data is not False:
return [(n, n, nbrs[n].get(data, default))
for n, nbrs in self.adj.items() if n in nbrs]
return ((n, n, nbrs[n].get(data, default))
for n, nbrs in self.adj.items() if n in nbrs)
else:
return [(n, n)
for n, nbrs in self.adj.items() if n in nbrs]
return ((n, n)
for n, nbrs in self.adj.items() if n in nbrs)

def number_of_selfloops(self):
"""Return the number of selfloop edges.
Expand All @@ -1579,7 +1579,7 @@ def number_of_selfloops(self):
>>> G.number_of_selfloops()
1
"""
return len(self.selfloop_edges())
return sum(1 for _ in self.selfloop_edges())

def size(self, weight=None):
"""Return the number of edges.
Expand Down
32 changes: 16 additions & 16 deletions networkx/classes/multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,42 +881,42 @@ def selfloop_edges(self, data=False, keys=False, default=None):
>>> G = nx.MultiGraph() # or MultiDiGraph
>>> G.add_edge(1,1)
>>> G.add_edge(1,2)
>>> G.selfloop_edges()
>>> list(G.selfloop_edges())
[(1, 1)]
>>> G.selfloop_edges(data=True)
>>> list(G.selfloop_edges(data=True))
[(1, 1, {})]
>>> G.selfloop_edges(keys=True)
>>> list(G.selfloop_edges(keys=True))
[(1, 1, 0)]
>>> G.selfloop_edges(keys=True, data=True)
>>> list(G.selfloop_edges(keys=True, data=True))
[(1, 1, 0, {})]
"""
if data is True:
if keys:
return [(n, n, k, d)
return ((n, n, k, d)
for n, nbrs in self.adj.items()
if n in nbrs for k, d in nbrs[n].items()]
if n in nbrs for k, d in nbrs[n].items())
else:
return [(n, n, d)
return ((n, n, d)
for n, nbrs in self.adj.items()
if n in nbrs for d in nbrs[n].values()]
if n in nbrs for d in nbrs[n].values())
elif data is not False:
if keys:
return [(n, n, k, d.get(data, default))
return ((n, n, k, d.get(data, default))
for n, nbrs in self.adj.items()
if n in nbrs for k, d in nbrs[n].items()]
if n in nbrs for k, d in nbrs[n].items())
else:
return [(n, n, d.get(data, default))
return ((n, n, d.get(data, default))
for n, nbrs in self.adj.items()
if n in nbrs for d in nbrs[n].values()]
if n in nbrs for d in nbrs[n].values())
else:
if keys:
return [(n, n, k)
return ((n, n, k)
for n, nbrs in self.adj.items()
if n in nbrs for k in nbrs[n].keys()]
if n in nbrs for k in nbrs[n].keys())
else:
return [(n, n)
return ((n, n)
for n, nbrs in self.adj.items()
if n in nbrs for d in nbrs[n].values()]
if n in nbrs for d in nbrs[n].values())

def number_of_edges(self, u=None, v=None):
"""Return the number of edges between two nodes.
Expand Down
10 changes: 5 additions & 5 deletions networkx/classes/tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_selfloops(self):
G=self.K3.copy()
G.add_edge(0,0)
assert_equal(list(G.nodes_with_selfloops()), [0])
assert_equal(G.selfloop_edges(),[(0,0)])
assert_equal(list(G.selfloop_edges()), [(0, 0)])
assert_equal(G.number_of_selfloops(),1)
G.remove_edge(0,0)
G.add_edge(0,0)
Expand Down Expand Up @@ -419,10 +419,10 @@ def test_selfloops_attr(self):
G=self.K3.copy()
G.add_edge(0,0)
G.add_edge(1,1,weight=2)
assert_equal(G.selfloop_edges(data=True),
[(0,0,{}),(1,1,{'weight':2})])
assert_equal(G.selfloop_edges(data='weight'),
[(0,0,None),(1,1,2)])
assert_equal(list(G.selfloop_edges(data=True)),
[(0, 0, {}), (1, 1, {'weight':2})])
assert_equal(list(G.selfloop_edges(data='weight')),
[(0, 0, None), (1, 1, 2)])


class TestGraph(BaseAttrGraphTester):
Expand Down
4 changes: 2 additions & 2 deletions networkx/classes/tests/test_multigraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def test_selfloops(self):
G=self.K3
G.add_edge(0,0)
assert_equal(list(G.nodes_with_selfloops()), [0])
assert_equal(G.selfloop_edges(),[(0,0)])
assert_equal(G.selfloop_edges(data=True),[(0,0,{})])
assert_equal(list(G.selfloop_edges()), [(0, 0)])
assert_equal(list(G.selfloop_edges(data=True)), [(0, 0, {})])
assert_equal(G.number_of_selfloops(),1)

def test_selfloops2(self):
Expand Down
2 changes: 1 addition & 1 deletion networkx/convert_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def to_scipy_sparse_matrix(G, nodelist=None, dtype=None,
c = col + row
# selfloop entries get double counted when symmetrizing
# so we subtract the data on the diagonal
selfloops = G.selfloop_edges(data=True)
selfloops = list(G.selfloop_edges(data=True))
if selfloops:
diag_index,diag_data = zip(*((index[u],-d.get(weight,1))
for u,v,d in selfloops
Expand Down

0 comments on commit 306f660

Please sign in to comment.