diff options
author | Mridul Seth <seth.mridul@gmail.com> | 2015-06-07 12:50:28 +0530 |
---|---|---|
committer | Mridul Seth <seth.mridul@gmail.com> | 2015-06-12 01:10:34 +0530 |
commit | b433dcc99c5728fb16e599a7153fbb1b834d9167 (patch) | |
tree | 4201b3ac6d51df461d2267bcdffffd6d62adc7a8 | |
parent | d49733b61fb7dedfbbbdd9c04464adf6beb5db07 (diff) | |
download | networkx-b433dcc99c5728fb16e599a7153fbb1b834d9167.tar.gz |
Remove successors_iter() and predecessors_iter(). G.successors() and G.predecessors() now return iterators instead of lists
-rw-r--r-- | networkx/algorithms/approximation/connectivity.py | 8 | ||||
-rw-r--r-- | networkx/algorithms/assortativity/connectivity.py | 6 | ||||
-rw-r--r-- | networkx/algorithms/bipartite/basic.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/connectivity/connectivity.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/connectivity/cuts.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/core.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/operators/tests/test_all.py | 2 | ||||
-rw-r--r-- | networkx/algorithms/operators/tests/test_binary.py | 2 | ||||
-rw-r--r-- | networkx/algorithms/shortest_paths/unweighted.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/shortest_paths/weighted.py | 2 | ||||
-rw-r--r-- | networkx/algorithms/simple_paths.py | 8 | ||||
-rw-r--r-- | networkx/algorithms/traversal/breadth_first_search.py | 2 | ||||
-rw-r--r-- | networkx/classes/digraph.py | 19 | ||||
-rw-r--r-- | networkx/classes/function.py | 4 | ||||
-rw-r--r-- | networkx/classes/tests/test_digraph.py | 20 | ||||
-rw-r--r-- | networkx/classes/tests/test_digraph_historical.py | 20 | ||||
-rw-r--r-- | networkx/classes/tests/test_multidigraph.py | 20 | ||||
-rw-r--r-- | networkx/generators/directed.py | 2 |
18 files changed, 61 insertions, 74 deletions
diff --git a/networkx/algorithms/approximation/connectivity.py b/networkx/algorithms/approximation/connectivity.py index 54191825..5850aed9 100644 --- a/networkx/algorithms/approximation/connectivity.py +++ b/networkx/algorithms/approximation/connectivity.py @@ -192,8 +192,8 @@ def node_connectivity(G, s=None, t=None): connected_func = nx.is_weakly_connected iter_func = itertools.permutations def neighbors(v): - return itertools.chain.from_iterable([G.predecessors_iter(v), - G.successors_iter(v)]) + return itertools.chain.from_iterable([G.predecessors(v), + G.successors(v)]) else: connected_func = nx.is_connected iter_func = itertools.combinations @@ -356,8 +356,8 @@ def _bidirectional_pred_succ(G, source, target, exclude): # handle either directed or undirected if G.is_directed(): - Gpred = G.predecessors_iter - Gsucc = G.successors_iter + Gpred = G.predecessors + Gsucc = G.successors else: Gpred = G.neighbors_iter Gsucc = G.neighbors_iter diff --git a/networkx/algorithms/assortativity/connectivity.py b/networkx/algorithms/assortativity/connectivity.py index 8e27ea3f..3a279751 100644 --- a/networkx/algorithms/assortativity/connectivity.py +++ b/networkx/algorithms/assortativity/connectivity.py @@ -17,7 +17,7 @@ def _avg_deg_conn(G, neighbors, source_degree, target_degree, dsum = defaultdict(float) dnorm = defaultdict(float) for n,k in source_degree(nodes).items(): - nbrdeg = target_degree(neighbors(n)) + nbrdeg = target_degree(list(neighbors(n))) if weight is None: s = float(sum(nbrdeg.values())) else: # weight nbr degree by weight of (n,nbr) edge @@ -36,10 +36,10 @@ def _avg_deg_conn(G, neighbors, source_degree, target_degree, # normalize dc = {} for k,avg in dsum.items(): - dc[k]=avg + dc[k] = avg norm = dnorm[k] if avg > 0 and norm > 0: - dc[k]/=norm + dc[k] /= norm return dc def average_degree_connectivity(G, source="in+out", target="in+out", diff --git a/networkx/algorithms/bipartite/basic.py b/networkx/algorithms/bipartite/basic.py index 5c630256..d2708d43 100644 --- a/networkx/algorithms/bipartite/basic.py +++ b/networkx/algorithms/bipartite/basic.py @@ -58,8 +58,8 @@ def color(G): if G.is_directed(): import itertools def neighbors(v): - return itertools.chain.from_iterable([G.predecessors_iter(v), - G.successors_iter(v)]) + return itertools.chain.from_iterable([G.predecessors(v), + G.successors(v)]) else: neighbors=G.neighbors_iter diff --git a/networkx/algorithms/connectivity/connectivity.py b/networkx/algorithms/connectivity/connectivity.py index bf791616..9c14eb27 100644 --- a/networkx/algorithms/connectivity/connectivity.py +++ b/networkx/algorithms/connectivity/connectivity.py @@ -309,8 +309,8 @@ def node_connectivity(G, s=None, t=None, flow_func=None): # It is necessary to consider both predecessors # and successors for directed graphs def neighbors(v): - return itertools.chain.from_iterable([G.predecessors_iter(v), - G.successors_iter(v)]) + return itertools.chain.from_iterable([G.predecessors(v), + G.successors(v)]) else: if not nx.is_connected(G): return 0 diff --git a/networkx/algorithms/connectivity/cuts.py b/networkx/algorithms/connectivity/cuts.py index 45814c2a..d7c0f6f2 100644 --- a/networkx/algorithms/connectivity/cuts.py +++ b/networkx/algorithms/connectivity/cuts.py @@ -408,8 +408,8 @@ def minimum_node_cut(G, s=None, t=None, flow_func=None): raise nx.NetworkXError('Input graph is not connected') iter_func = itertools.permutations def neighbors(v): - return itertools.chain.from_iterable([G.predecessors_iter(v), - G.successors_iter(v)]) + return itertools.chain.from_iterable([G.predecessors(v), + G.successors(v)]) else: if not nx.is_connected(G): raise nx.NetworkXError('Input graph is not connected') diff --git a/networkx/algorithms/core.py b/networkx/algorithms/core.py index 6e6b9f56..d15de11a 100644 --- a/networkx/algorithms/core.py +++ b/networkx/algorithms/core.py @@ -72,8 +72,8 @@ def core_number(G): if G.is_directed(): import itertools def neighbors(v): - return itertools.chain.from_iterable([G.predecessors_iter(v), - G.successors_iter(v)]) + return itertools.chain.from_iterable([G.predecessors(v), + G.successors(v)]) else: neighbors=G.neighbors_iter degrees=G.degree() diff --git a/networkx/algorithms/operators/tests/test_all.py b/networkx/algorithms/operators/tests/test_all.py index 5ff1a063..a58de3d2 100644 --- a/networkx/algorithms/operators/tests/test_all.py +++ b/networkx/algorithms/operators/tests/test_all.py @@ -119,7 +119,7 @@ def test_union_all_and_compose_all(): assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) - assert_equal(G2.neighbors('copy4'),[]) + assert_equal(sorted(G2.neighbors('copy4')),[]) assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4']) assert_equal(len(G),8) assert_equal(nx.number_of_edges(G),6) diff --git a/networkx/algorithms/operators/tests/test_binary.py b/networkx/algorithms/operators/tests/test_binary.py index 70149418..5bad192d 100644 --- a/networkx/algorithms/operators/tests/test_binary.py +++ b/networkx/algorithms/operators/tests/test_binary.py @@ -214,7 +214,7 @@ def test_union_and_compose(): assert_equal(sorted(G2.nodes()), ['1', '2', '3', '4', 'copy1', 'copy2', 'copy3', 'copy4']) - assert_equal(G2.neighbors('copy4'),[]) + assert_equal(sorted(G2.neighbors('copy4')),[]) assert_equal(sorted(G2.neighbors('copy1')),['copy2', 'copy3', 'copy4']) assert_equal(len(G),8) assert_equal(number_of_edges(G),6) diff --git a/networkx/algorithms/shortest_paths/unweighted.py b/networkx/algorithms/shortest_paths/unweighted.py index dc558569..713c4a18 100644 --- a/networkx/algorithms/shortest_paths/unweighted.py +++ b/networkx/algorithms/shortest_paths/unweighted.py @@ -164,8 +164,8 @@ def _bidirectional_pred_succ(G, source, target): # handle either directed or undirected if G.is_directed(): - Gpred=G.predecessors_iter - Gsucc=G.successors_iter + Gpred=G.predecessors + Gsucc=G.successors else: Gpred=G.neighbors_iter Gsucc=G.neighbors_iter diff --git a/networkx/algorithms/shortest_paths/weighted.py b/networkx/algorithms/shortest_paths/weighted.py index 135f2525..e43b6c1d 100644 --- a/networkx/algorithms/shortest_paths/weighted.py +++ b/networkx/algorithms/shortest_paths/weighted.py @@ -952,7 +952,7 @@ def bidirectional_dijkstra(G, source, target, weight='weight'): push(fringe[1], (0, next(c), target)) # neighs for extracting correct neighbor information if G.is_directed(): - neighs = [G.successors_iter, G.predecessors_iter] + neighs = [G.successors, G.predecessors] else: neighs = [G.neighbors_iter, G.neighbors_iter] # variables to hold shortest discovered path diff --git a/networkx/algorithms/simple_paths.py b/networkx/algorithms/simple_paths.py index ae7fbbc0..661d924a 100644 --- a/networkx/algorithms/simple_paths.py +++ b/networkx/algorithms/simple_paths.py @@ -364,8 +364,8 @@ def _bidirectional_pred_succ(G, source, target, ignore_nodes=None, ignore_edges= # handle either directed or undirected if G.is_directed(): - Gpred=G.predecessors_iter - Gsucc=G.successors_iter + Gpred=G.predecessors + Gsucc=G.successors else: Gpred=G.neighbors_iter Gsucc=G.neighbors_iter @@ -522,8 +522,8 @@ def _bidirectional_dijkstra(G, source, target, weight='weight', # handle either directed or undirected if G.is_directed(): - Gpred=G.predecessors_iter - Gsucc=G.successors_iter + Gpred=G.predecessors + Gsucc=G.successors else: Gpred=G.neighbors_iter Gsucc=G.neighbors_iter diff --git a/networkx/algorithms/traversal/breadth_first_search.py b/networkx/algorithms/traversal/breadth_first_search.py index efa4817b..034f2aec 100644 --- a/networkx/algorithms/traversal/breadth_first_search.py +++ b/networkx/algorithms/traversal/breadth_first_search.py @@ -42,7 +42,7 @@ def bfs_edges(G, source, reverse=False): by D. Eppstein, July 2004. """ if reverse and isinstance(G, nx.DiGraph): - neighbors = G.predecessors_iter + neighbors = G.predecessors else: neighbors = G.neighbors_iter visited = set([source]) diff --git a/networkx/classes/digraph.py b/networkx/classes/digraph.py index c6863067..591b0809 100644 --- a/networkx/classes/digraph.py +++ b/networkx/classes/digraph.py @@ -739,38 +739,25 @@ class DiGraph(Graph): """ return (u in self.pred and v in self.pred[u]) - def successors_iter(self,n): + def successors(self,n): """Return an iterator over successor nodes of n. - neighbors_iter() and successors_iter() are the same. + neighbors() and successors() are the same. """ try: return iter(self.succ[n]) except KeyError: raise NetworkXError("The node %s is not in the digraph."%(n,)) - def predecessors_iter(self,n): + def predecessors(self,n): """Return an iterator over predecessor nodes of n.""" try: return iter(self.pred[n]) except KeyError: raise NetworkXError("The node %s is not in the digraph."%(n,)) - def successors(self, n): - """Return a list of successor nodes of n. - - neighbors() and successors() are the same function. - """ - return list(self.successors_iter(n)) - - def predecessors(self, n): - """Return a list of predecessor nodes of n.""" - return list(self.predecessors_iter(n)) - - # digraph definitions neighbors = successors - neighbors_iter = successors_iter def edges_iter(self, nbunch=None, data=False, default=None): """Return an iterator over the edges. diff --git a/networkx/classes/function.py b/networkx/classes/function.py index 4ae7d45e..551c4557 100644 --- a/networkx/classes/function.py +++ b/networkx/classes/function.py @@ -453,8 +453,8 @@ def all_neighbors(graph, node): Iterator of neighbors """ if graph.is_directed(): - values = itertools.chain.from_iterable([graph.predecessors_iter(node), - graph.successors_iter(node)]) + values = itertools.chain.from_iterable([graph.predecessors(node), + graph.successors(node)]) else: values = graph.neighbors_iter(node) diff --git a/networkx/classes/tests/test_digraph.py b/networkx/classes/tests/test_digraph.py index 9ac3d3e6..a5d5e0e0 100644 --- a/networkx/classes/tests/test_digraph.py +++ b/networkx/classes/tests/test_digraph.py @@ -9,31 +9,31 @@ class BaseDiGraphTester(BaseGraphTester): assert_equal(G.has_successor(0,1),True) assert_equal(G.has_successor(0,-1),False) + # def test_successors(self): + # G=self.K3 + # assert_equal(sorted(G.successors(0)),[1,2]) + # assert_raises((KeyError,networkx.NetworkXError), G.successors,-1) + def test_successors(self): G=self.K3 assert_equal(sorted(G.successors(0)),[1,2]) assert_raises((KeyError,networkx.NetworkXError), G.successors,-1) - def test_successors_iter(self): - G=self.K3 - assert_equal(sorted(G.successors_iter(0)),[1,2]) - assert_raises((KeyError,networkx.NetworkXError), G.successors_iter,-1) - def test_has_predecessor(self): G=self.K3 assert_equal(G.has_predecessor(0,1),True) assert_equal(G.has_predecessor(0,-1),False) + # def test_predecessors(self): + # G=self.K3 + # assert_equal(sorted(G.predecessors(0)),[1,2]) + # assert_raises((KeyError,networkx.NetworkXError), G.predecessors,-1) + def test_predecessors(self): G=self.K3 assert_equal(sorted(G.predecessors(0)),[1,2]) assert_raises((KeyError,networkx.NetworkXError), G.predecessors,-1) - def test_predecessors_iter(self): - G=self.K3 - assert_equal(sorted(G.predecessors_iter(0)),[1,2]) - assert_raises((KeyError,networkx.NetworkXError), G.predecessors_iter,-1) - def test_edges(self): G=self.K3 assert_equal(sorted(G.edges()),[(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]) diff --git a/networkx/classes/tests/test_digraph_historical.py b/networkx/classes/tests/test_digraph_historical.py index 6bf295c3..63f193b9 100644 --- a/networkx/classes/tests/test_digraph_historical.py +++ b/networkx/classes/tests/test_digraph_historical.py @@ -59,11 +59,11 @@ class TestDiGraphHistorical(HistoricalTests): assert_equal(sorted(G.neighbors('C')),['D']) assert_equal(sorted(G['C']),['D']) assert_equal(sorted(G.neighbors('A')),['B', 'C']) - assert_equal(sorted(G.neighbors_iter('A')),['B', 'C']) - assert_equal(sorted(G.neighbors_iter('C')),['D']) assert_equal(sorted(G.neighbors('A')),['B', 'C']) + assert_equal(sorted(G.neighbors('C')),['D']) + assert_equal(sorted(G.neighbors('A')),['B', 'C']) + assert_raises(nx.NetworkXError,G.neighbors,'j') assert_raises(nx.NetworkXError,G.neighbors,'j') - assert_raises(nx.NetworkXError,G.neighbors_iter,'j') def test_successors(self): G=self.G() @@ -71,12 +71,12 @@ class TestDiGraphHistorical(HistoricalTests): G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'C'), ('C', 'D')]) assert_equal(sorted(G.successors('A')),['B', 'C']) - assert_equal(sorted(G.successors_iter('A')),['B', 'C']) + assert_equal(sorted(G.successors('A')),['B', 'C']) assert_equal(sorted(G.successors('G')),[]) assert_equal(sorted(G.successors('D')),[]) - assert_equal(sorted(G.successors_iter('G')),[]) + assert_equal(sorted(G.successors('G')),[]) + assert_raises(nx.NetworkXError,G.successors,'j') assert_raises(nx.NetworkXError,G.successors,'j') - assert_raises(nx.NetworkXError,G.successors_iter,'j') def test_predecessors(self): @@ -85,12 +85,12 @@ class TestDiGraphHistorical(HistoricalTests): G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), ('B', 'C'), ('C', 'D')]) assert_equal(sorted(G.predecessors('C')),['A', 'B']) - assert_equal(sorted(G.predecessors_iter('C')),['A', 'B']) + assert_equal(sorted(G.predecessors('C')),['A', 'B']) assert_equal(sorted(G.predecessors('G')),[]) assert_equal(sorted(G.predecessors('A')),[]) - assert_equal(sorted(G.predecessors_iter('G')),[]) - assert_equal(sorted(G.predecessors_iter('A')),[]) - assert_equal(sorted(G.successors_iter('D')),[]) + assert_equal(sorted(G.predecessors('G')),[]) + assert_equal(sorted(G.predecessors('A')),[]) + assert_equal(sorted(G.successors('D')),[]) assert_raises(nx.NetworkXError,G.predecessors,'j') assert_raises(nx.NetworkXError,G.predecessors,'j') diff --git a/networkx/classes/tests/test_multidigraph.py b/networkx/classes/tests/test_multidigraph.py index e8c2543f..b2685a84 100644 --- a/networkx/classes/tests/test_multidigraph.py +++ b/networkx/classes/tests/test_multidigraph.py @@ -112,31 +112,31 @@ class BaseMultiDiGraphTester(BaseMultiGraphTester): assert_equal(G.has_successor(0,1),True) assert_equal(G.has_successor(0,-1),False) + # def test_successors(self): + # G=self.K3 + # assert_equal(sorted(G.successors(0)),[1,2]) + # assert_raises((KeyError,networkx.NetworkXError), G.successors,-1) + def test_successors(self): G=self.K3 assert_equal(sorted(G.successors(0)),[1,2]) assert_raises((KeyError,networkx.NetworkXError), G.successors,-1) - def test_successors_iter(self): - G=self.K3 - assert_equal(sorted(G.successors_iter(0)),[1,2]) - assert_raises((KeyError,networkx.NetworkXError), G.successors_iter,-1) - def test_has_predecessor(self): G=self.K3 assert_equal(G.has_predecessor(0,1),True) assert_equal(G.has_predecessor(0,-1),False) + # def test_predecessors(self): + # G=self.K3 + # assert_equal(sorted(G.predecessors(0)),[1,2]) + # assert_raises((KeyError,networkx.NetworkXError), G.predecessors,-1) + def test_predecessors(self): G=self.K3 assert_equal(sorted(G.predecessors(0)),[1,2]) assert_raises((KeyError,networkx.NetworkXError), G.predecessors,-1) - def test_predecessors_iter(self): - G=self.K3 - assert_equal(sorted(G.predecessors_iter(0)),[1,2]) - assert_raises((KeyError,networkx.NetworkXError), G.predecessors_iter,-1) - def test_degree(self): G=self.K3 diff --git a/networkx/generators/directed.py b/networkx/generators/directed.py index aa00a802..ee229ba1 100644 --- a/networkx/generators/directed.py +++ b/networkx/generators/directed.py @@ -144,7 +144,7 @@ def gnr_graph(n, p, create_using=None, seed=None): for source in range(1,n): target=random.randrange(0,source) if random.random() < p and target !=0: - target=G.successors(target)[0] + target = list(G.successors(target))[0] G.add_edge(source,target) return G |