exception.py 3.5 KB

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