diff options
author | Mridul Seth <seth.mridul@gmail.com> | 2015-12-30 23:01:11 +0530 |
---|---|---|
committer | Mridul Seth <seth.mridul@gmail.com> | 2015-12-30 23:01:11 +0530 |
commit | 9a43cd4866fb5c0231282065d5959d0e4d06420e (patch) | |
tree | 97845d94f24fc9d9cd12d76fe10f97924e94cbda | |
parent | 6e2db3386bc98285eacd52b6d42bcb547f59bf9d (diff) | |
download | networkx-9a43cd4866fb5c0231282065d5959d0e4d06420e.tar.gz |
Add exception class NodeNotFound
-rw-r--r-- | networkx/algorithms/simple_paths.py | 14 | ||||
-rw-r--r-- | networkx/algorithms/tests/test_simple_paths.py | 17 | ||||
-rw-r--r-- | networkx/exception.py | 6 |
3 files changed, 21 insertions, 16 deletions
diff --git a/networkx/algorithms/simple_paths.py b/networkx/algorithms/simple_paths.py index 21fd99ca..f755f269 100644 --- a/networkx/algorithms/simple_paths.py +++ b/networkx/algorithms/simple_paths.py @@ -77,9 +77,9 @@ def all_simple_paths(G, source, target, cutoff=None): all_shortest_paths, shortest_path """ if source not in G: - raise nx.NetworkXError('source node %s not in graph'%source) + raise nx.NodeNotFound('source node %s not in graph'%source) if target not in G: - raise nx.NetworkXError('target node %s not in graph'%target) + raise nx.NodeNotFound('target node %s not in graph'%target) if cutoff is None: cutoff = len(G)-1 if G.is_multigraph(): @@ -216,10 +216,10 @@ def shortest_simple_paths(G, source, target, weight=None): """ if source not in G: - raise nx.NetworkXError('source node %s not in graph' % source) + raise nx.NodeNotFound('source node %s not in graph' % source) if target not in G: - raise nx.NetworkXError('target node %s not in graph' % target) + raise nx.NodeNotFound('target node %s not in graph' % target) if weight is None: length_func = len @@ -295,7 +295,7 @@ def _bidirectional_shortest_path(G, source, target, """Return the shortest path between source and target ignoring nodes and edges in the containers ignore_nodes and ignore_edges. - This is a custom modification of the standard bidirectional shortest + This is a custom modification of the standard bidirectional shortest path implementation at networkx.algorithms.unweighted Parameters @@ -315,7 +315,7 @@ def _bidirectional_shortest_path(G, source, target, edges to ignore, optional weight : None - This function accepts a weight argument for convinience of + This function accepts a weight argument for convinience of shortest_simple_paths function. It will be ignored. Returns @@ -454,7 +454,7 @@ def _bidirectional_dijkstra(G, source, target, weight='weight', """Dijkstra's algorithm for shortest paths using bidirectional search. This function returns the shortest path between source and target - ignoring nodes and edges in the containers ignore_nodes and + ignoring nodes and edges in the containers ignore_nodes and ignore_edges. This is a custom modification of the standard Dijkstra bidirectional diff --git a/networkx/algorithms/tests/test_simple_paths.py b/networkx/algorithms/tests/test_simple_paths.py index c068c70a..01e42c52 100644 --- a/networkx/algorithms/tests/test_simple_paths.py +++ b/networkx/algorithms/tests/test_simple_paths.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import random -from nose.tools import * +from nose.tools import assert_equal, assert_raises, assert_true, raises import networkx as nx from networkx import convert_node_labels_to_integers as cnlti @@ -67,13 +67,13 @@ def test_cutoff_zero(): paths = nx.all_simple_paths(nx.MultiGraph(G),0,3,cutoff=0) assert_equal(list(list(p) for p in paths),[]) -@raises(nx.NetworkXError) +@raises(nx.NodeNotFound) def test_source_missing(): G = nx.Graph() G.add_path([1,2,3]) paths = list(nx.all_simple_paths(nx.MultiGraph(G),0,3)) -@raises(nx.NetworkXError) +@raises(nx.NodeNotFound) def test_target_missing(): G = nx.Graph() G.add_path([1,2,3]) @@ -139,14 +139,14 @@ def test_weight_name(): paths = list(nx.shortest_simple_paths(G, 0, 3, weight='foo')) solution = [[0, 6, 5, 4, 3], [0, 1, 2, 3]] assert_equal(paths, solution) - -@raises(nx.NetworkXError) + +@raises(nx.NodeNotFound) def test_ssp_source_missing(): G = nx.Graph() G.add_path([1,2,3]) paths = list(nx.shortest_simple_paths(G, 0, 3)) -@raises(nx.NetworkXError) +@raises(nx.NodeNotFound) def test_ssp_target_missing(): G = nx.Graph() G.add_path([1,2,3]) @@ -199,7 +199,7 @@ def test_bidirectional_shortest_path_restricted(): nx.NetworkXNoPath, _bidirectional_shortest_path, directed_cycle, - 0, 3, + 0, 3, ignore_edges=[(1, 2)], ) @@ -225,7 +225,7 @@ def test_bidirectional_dijksta_restricted(): XG3.add_weighted_edges_from([[0, 1, 2], [1, 2, 12], [2, 3, 1], [3, 4, 5], [4, 5, 1], [5, 0, 10]]) - validate_length_path(XG, 's', 'v', 9, + validate_length_path(XG, 's', 'v', 9, *_bidirectional_dijkstra(XG, 's', 'v')) validate_length_path(XG, 's', 'v', 10, *_bidirectional_dijkstra(XG, 's', 'v', ignore_nodes=['u'])) @@ -259,4 +259,3 @@ def test_bidirectional_dijkstra_no_path(): G.add_path([1, 2, 3]) G.add_path([4, 5, 6]) path = _bidirectional_dijkstra(G, 1, 6) - diff --git a/networkx/exception.py b/networkx/exception.py index 72696b24..4c1a1ec0 100644 --- a/networkx/exception.py +++ b/networkx/exception.py @@ -52,3 +52,9 @@ class NetworkXUnbounded(NetworkXAlgorithmError): class NetworkXNotImplemented(NetworkXException): """Exception raised by algorithms not implemented for a type of graph.""" + +class NotFound(NetworkXException): + """Exception""" + +class NodeNotFound(NotFound): + """Exception raised if node is not present in the graph""" |