1""" 2********** 3Exceptions 4********** 5 6Base exceptions and errors for NetworkX. 7""" 8 9__all__ = [ 10 "HasACycle", 11 "NodeNotFound", 12 "PowerIterationFailedConvergence", 13 "ExceededMaxIterations", 14 "AmbiguousSolution", 15 "NetworkXAlgorithmError", 16 "NetworkXException", 17 "NetworkXError", 18 "NetworkXNoCycle", 19 "NetworkXNoPath", 20 "NetworkXNotImplemented", 21 "NetworkXPointlessConcept", 22 "NetworkXUnbounded", 23 "NetworkXUnfeasible", 24] 25 26 27class NetworkXException(Exception): 28 """Base class for exceptions in NetworkX.""" 29 30 31class NetworkXError(NetworkXException): 32 """Exception for a serious error in NetworkX""" 33 34 35class NetworkXPointlessConcept(NetworkXException): 36 """Raised when a null graph is provided as input to an algorithm 37 that cannot use it. 38 39 The null graph is sometimes considered a pointless concept [1]_, 40 thus the name of the exception. 41 42 References 43 ---------- 44 .. [1] Harary, F. and Read, R. "Is the Null Graph a Pointless 45 Concept?" In Graphs and Combinatorics Conference, George 46 Washington University. New York: Springer-Verlag, 1973. 47 48 """ 49 50 51class NetworkXAlgorithmError(NetworkXException): 52 """Exception for unexpected termination of algorithms.""" 53 54 55class NetworkXUnfeasible(NetworkXAlgorithmError): 56 """Exception raised by algorithms trying to solve a problem 57 instance that has no feasible solution.""" 58 59 60class NetworkXNoPath(NetworkXUnfeasible): 61 """Exception for algorithms that should return a path when running 62 on graphs where such a path does not exist.""" 63 64 65class NetworkXNoCycle(NetworkXUnfeasible): 66 """Exception for algorithms that should return a cycle when running 67 on graphs where such a cycle does not exist.""" 68 69 70class HasACycle(NetworkXException): 71 """Raised if a graph has a cycle when an algorithm expects that it 72 will have no cycles. 73 74 """ 75 76 77class NetworkXUnbounded(NetworkXAlgorithmError): 78 """Exception raised by algorithms trying to solve a maximization 79 or a minimization problem instance that is unbounded.""" 80 81 82class NetworkXNotImplemented(NetworkXException): 83 """Exception raised by algorithms not implemented for a type of graph.""" 84 85 86class NodeNotFound(NetworkXException): 87 """Exception raised if requested node is not present in the graph""" 88 89 90class AmbiguousSolution(NetworkXException): 91 """Raised if more than one valid solution exists for an intermediary step 92 of an algorithm. 93 94 In the face of ambiguity, refuse the temptation to guess. 95 This may occur, for example, when trying to determine the 96 bipartite node sets in a disconnected bipartite graph when 97 computing bipartite matchings. 98 99 """ 100 101 102class ExceededMaxIterations(NetworkXException): 103 """Raised if a loop iterates too many times without breaking. 104 105 This may occur, for example, in an algorithm that computes 106 progressively better approximations to a value but exceeds an 107 iteration bound specified by the user. 108 109 """ 110 111 112class PowerIterationFailedConvergence(ExceededMaxIterations): 113 """Raised when the power iteration method fails to converge within a 114 specified iteration limit. 115 116 `num_iterations` is the number of iterations that have been 117 completed when this exception was raised. 118 119 """ 120 121 def __init__(self, num_iterations, *args, **kw): 122 msg = f"power iteration failed to converge within {num_iterations} iterations" 123 exception_message = msg 124 superinit = super().__init__ 125 superinit(self, exception_message, *args, **kw) 126