# networkx.exception 源代码

# -*- coding: utf-8 -*-
#    Aric Hagberg <hagberg@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Pieter Swart <swart@lanl.gov>
#
# Authors:
#    Aric Hagberg <hagberg@lanl.gov>
#    Pieter Swart <swart@lanl.gov>
#    Dan Schult <dschult@colgate.edu>
#    Loïc Séguin-C. <loicseguin@gmail.com>
"""
**********
Exceptions
**********

Base exceptions and errors for NetworkX.
"""

__all__ = [
'HasACycle',
'NodeNotFound',
'PowerIterationFailedConvergence',
'ExceededMaxIterations',
'AmbiguousSolution',
'NetworkXAlgorithmError',
'NetworkXException',
'NetworkXError',
'NetworkXNoCycle',
'NetworkXNoPath',
'NetworkXNotImplemented',
'NetworkXPointlessConcept',
'NetworkXUnbounded',
'NetworkXUnfeasible',
]

[文档]class NetworkXException(Exception):
"""Base class for exceptions in NetworkX."""

[文档]class NetworkXError(NetworkXException):
"""Exception for a serious error in NetworkX"""

[文档]class NetworkXPointlessConcept(NetworkXException):
"""Raised when a null graph is provided as input to an algorithm
that cannot use it.

The null graph is sometimes considered a pointless concept [1]_,
thus the name of the exception.

References
----------
.. [1] Harary, F. and Read, R. "Is the Null Graph a Pointless
Concept?"  In Graphs and Combinatorics Conference, George
Washington University.  New York: Springer-Verlag, 1973.

"""

[文档]class NetworkXAlgorithmError(NetworkXException):
"""Exception for unexpected termination of algorithms."""

[文档]class NetworkXUnfeasible(NetworkXAlgorithmError):
"""Exception raised by algorithms trying to solve a problem
instance that has no feasible solution."""

[文档]class NetworkXNoPath(NetworkXUnfeasible):
"""Exception for algorithms that should return a path when running
on graphs where such a path does not exist."""

[文档]class NetworkXNoCycle(NetworkXUnfeasible):
"""Exception for algorithms that should return a cycle when running
on graphs where such a cycle does not exist."""

[文档]class HasACycle(NetworkXException):
"""Raised if a graph has a cycle when an algorithm expects that it
will have no cycles.

"""

[文档]class NetworkXUnbounded(NetworkXAlgorithmError):
"""Exception raised by algorithms trying to solve a maximization
or a minimization problem instance that is unbounded."""

[文档]class NetworkXNotImplemented(NetworkXException):
"""Exception raised by algorithms not implemented for a type of graph."""

[文档]class NodeNotFound(NetworkXException):
"""Exception raised if requested node is not present in the graph"""

[文档]class AmbiguousSolution(NetworkXException):
"""Raised if more than one valid solution exists for an intermediary step
of an algorithm.

In the face of ambiguity, refuse the temptation to guess.
This may occur, for example, when trying to determine the
bipartite node sets in a disconnected bipartite graph when
computing bipartite matchings.

"""

[文档]class ExceededMaxIterations(NetworkXException):
"""Raised if a loop iterates too many times without breaking.

This may occur, for example, in an algorithm that computes
progressively better approximations to a value but exceeds an
iteration bound specified by the user.

"""

[文档]class PowerIterationFailedConvergence(ExceededMaxIterations):
"""Raised when the power iteration method fails to converge within a
specified iteration limit.

num_iterations is the number of iterations that have been
completed when this exception was raised.

"""

def __init__(self, num_iterations, *args, **kw):
msg = 'power iteration failed to converge within {} iterations'
exception_message = msg.format(num_iterations)
superinit = super(PowerIterationFailedConvergence, self).__init__
superinit(self, exception_message, *args, **kw)