Skip to content

input4mips_validation.validation.regexp#

input4mips_validation.validation.regexp #

Validation of values against regular expressions

DoesNotMatchRegexpError #

Bases: ValueError

Raised when a value does not match a given regular expression, but it should

Source code in src/input4mips_validation/validation/regexp.py
class DoesNotMatchRegexpError(ValueError):
    """
    Raised when a value does not match a given regular expression, but it should
    """

    def __init__(
        self,
        value: str,
        regexp_to_match: str,
    ) -> None:
        """
        Initialise the error

        Parameters
        ----------
        value
            Value that should match `regexp_to_match`

        regexp_to_match
            Regular expression that `value` should match
        """
        error_msg = f"{value=!r} does not match {regexp_to_match=!r}"

        super().__init__(error_msg)

__init__(value, regexp_to_match) #

Initialise the error

Parameters:

Name Type Description Default
value str

Value that should match regexp_to_match

required
regexp_to_match str

Regular expression that value should match

required
Source code in src/input4mips_validation/validation/regexp.py
def __init__(
    self,
    value: str,
    regexp_to_match: str,
) -> None:
    """
    Initialise the error

    Parameters
    ----------
    value
        Value that should match `regexp_to_match`

    regexp_to_match
        Regular expression that `value` should match
    """
    error_msg = f"{value=!r} does not match {regexp_to_match=!r}"

    super().__init__(error_msg)

validate_matches_regexp(value, regexp_to_match) #

Validate that a value matches a given regular expression

Parameters:

Name Type Description Default
value str

Value to validate

required
regexp_to_match str

Regular expression that value should match

required

Raises:

Type Description
DoesNotMatchRegexpError

value does not match regexp_to_match

Source code in src/input4mips_validation/validation/regexp.py
def validate_matches_regexp(value: str, regexp_to_match: str) -> None:
    """
    Validate that a value matches a given regular expression

    Parameters
    ----------
    value
        Value to validate

    regexp_to_match
        Regular expression that `value` should match

    Raises
    ------
    DoesNotMatchRegexpError
        `value` does not match `regexp_to_match`
    """
    if not re.match(regexp_to_match, value):
        raise DoesNotMatchRegexpError(value, regexp_to_match=regexp_to_match)