Skip to content

gcages.exceptions#

Exceptions that are used throughout

Classes:

Name Description
MissingOptionalDependencyError

Raised when an optional dependency is missing

UnrecognisedValueError

Raised when a value is not recognised

MissingOptionalDependencyError #

Bases: ImportError

Raised when an optional dependency is missing

For example, plotting dependencies like matplotlib

Methods:

Name Description
__init__

Initialise the error

Source code in src/gcages/exceptions.py
class MissingOptionalDependencyError(ImportError):
    """
    Raised when an optional dependency is missing

    For example, plotting dependencies like matplotlib
    """

    def __init__(self, callable_name: str, requirement: str) -> None:
        """
        Initialise the error

        Parameters
        ----------
        callable_name
            The name of the callable that requires the dependency

        requirement
            The name of the requirement
        """
        error_msg = f"`{callable_name}` requires {requirement} to be installed"
        super().__init__(error_msg)

__init__ #

__init__(callable_name: str, requirement: str) -> None

Initialise the error

Parameters:

Name Type Description Default
callable_name str

The name of the callable that requires the dependency

required
requirement str

The name of the requirement

required
Source code in src/gcages/exceptions.py
def __init__(self, callable_name: str, requirement: str) -> None:
    """
    Initialise the error

    Parameters
    ----------
    callable_name
        The name of the callable that requires the dependency

    requirement
        The name of the requirement
    """
    error_msg = f"`{callable_name}` requires {requirement} to be installed"
    super().__init__(error_msg)

UnrecognisedValueError #

Bases: ValueError

Raised when a value is not recognised

In this context, recognised means 'known' in the sense of being part of some set of values that are understood and defined. For example, this error could be raised when a value is not be part of a set of a controlled vocabulary/known definitions.

Methods:

Name Description
__init__

Initialise the error

Source code in src/gcages/exceptions.py
class UnrecognisedValueError(ValueError):
    """
    Raised when a value is not recognised

    In this context, recognised means 'known'
    in the sense of being part of some set of values
    that are understood and defined.
    For example, this error could be raised when a value
    is not be part of a set of a controlled vocabulary/known definitions.
    """

    def __init__(
        self, unrecognised_value: Any, name: Any, known_values: Collection[Any]
    ) -> None:
        """
        Initialise the error

        Parameters
        ----------
        unrecognised_value
            The unrecognised value

        name
            The name of the thing that has the unrecognised value

            For example, `unrecognised_value` might be `"Emissions|junk"`
            and `name` could be `"variable"`.

        known_values
            The known values for `metadata_key`
        """
        error_msg_l = [f"{unrecognised_value!r} is not a recognised value for {name}."]
        close = difflib.get_close_matches(
            unrecognised_value, known_values, n=3, cutoff=0.6
        )
        if close:
            close_with_quotes = [f"{v!r}" for v in close]
            error_msg_l.append(f"Did you mean {' or '.join(close_with_quotes)}?")

        error_msg_l.append(f"The full list of known values is: {known_values}")

        super().__init__(" ".join(error_msg_l))

__init__ #

__init__(
    unrecognised_value: Any,
    name: Any,
    known_values: Collection[Any],
) -> None

Initialise the error

Parameters:

Name Type Description Default
unrecognised_value Any

The unrecognised value

required
name Any

The name of the thing that has the unrecognised value

For example, unrecognised_value might be "Emissions|junk" and name could be "variable".

required
known_values Collection[Any]

The known values for metadata_key

required
Source code in src/gcages/exceptions.py
def __init__(
    self, unrecognised_value: Any, name: Any, known_values: Collection[Any]
) -> None:
    """
    Initialise the error

    Parameters
    ----------
    unrecognised_value
        The unrecognised value

    name
        The name of the thing that has the unrecognised value

        For example, `unrecognised_value` might be `"Emissions|junk"`
        and `name` could be `"variable"`.

    known_values
        The known values for `metadata_key`
    """
    error_msg_l = [f"{unrecognised_value!r} is not a recognised value for {name}."]
    close = difflib.get_close_matches(
        unrecognised_value, known_values, n=3, cutoff=0.6
    )
    if close:
        close_with_quotes = [f"{v!r}" for v in close]
        error_msg_l.append(f"Did you mean {' or '.join(close_with_quotes)}?")

    error_msg_l.append(f"The full list of known values is: {known_values}")

    super().__init__(" ".join(error_msg_l))