bases.encoding.errors API documentation
Description: Encoding error classes.

# Module `bases.encoding.errors`

Encoding error classes.

Expand source code

    """
        Encoding error classes.
    """

    import binascii

    from bases.alphabet import Alphabet

    class Error(binascii.Error):
        """
            Generic encoding or decoding error.
        """
        ...

    class EncodingError(Error):
        """
            Generic encoding error.
        """
        ...

    class InvalidDigitError(EncodingError):
        """
            Encoding error raised when a digit does not index a character
            in the given alphabet (because `not 0 <= digit < len(alphabet)`)
        """

        digit: int
        base: int

        def __init__(self, digit: int, base: int) -> None:
            self.digit = digit
            self.base = base
            if 0 <= digit < base:
                raise ValueError(f"Digit {digit} is valid for base {base}.")
            error_msg = f"Invalid digit {digit} encountered for base {base}."
            super().__init__(error_msg)

    class InvalidByteBlockError(EncodingError):
        """
            Encoding error raised by block encodings when a byte block is invalid.
        """
        ...

    class DecodingError(Error):
        """
            Generic decoding error.
        """
        ...

    class NonAlphabeticCharError(DecodingError):
        """
            Decoding error raised when a character is not in the given alphabet,
            (considering case-insensitivity of the alphabet, if relevant).
        """

        char: str
        alphabet: Alphabet

        def __init__(self, char: str, alphabet: Alphabet) -> None:
            self.char = char
            self.alphabet = alphabet
            if char in alphabet:
                raise ValueError(f"Character {repr(char)} is alphabetic.")
            error_msg = f"Invalid character {repr(char)} encountered for alphabet {str(alphabet)}."
            super().__init__(error_msg)

    class PaddingError(DecodingError):
        """
            Decoding error raised when a string to be decoded has incorrect padding.
        """

        padding: int
        expected_padding: int

        def __init__(self, padding: int, expected_padding: int) -> None:
            self.padding = padding
            self.expected_padding = expected_padding
            if padding < expected_padding:
                error_msg = f"Insufficient padding: found {padding}, expected {expected_padding}"
            elif padding > expected_padding:
                error_msg = f"Excessive padding: found {padding}, expected {expected_padding}"
            else:
                raise ValueError("Padding is exactly what was expected.")
            super().__init__(error_msg)

    class InvalidCharBlockError(DecodingError):
        """
            Decoding error raised by block encodings when a char block is invalid.
        """
        ...

## Classes

`class DecodingError (*args, **kwargs)`

  Generic decoding error.

  Expand source code

      class DecodingError(Error):
          """
              Generic decoding error.
          """
          ...

  ### Ancestors

  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException

  ### Subclasses

  - InvalidCharBlockError
  - NonAlphabeticCharError
  - PaddingError
`class EncodingError (*args, **kwargs)`

  Generic encoding error.

  Expand source code

      class EncodingError(Error):
          """
              Generic encoding error.
          """
          ...

  ### Ancestors

  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException

  ### Subclasses

  - InvalidByteBlockError
  - InvalidDigitError
`class Error (*args, **kwargs)`

  Generic encoding or decoding error.

  Expand source code

      class Error(binascii.Error):
          """
              Generic encoding or decoding error.
          """
          ...

  ### Ancestors

  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException

  ### Subclasses

  - DecodingError
  - EncodingError
`class InvalidByteBlockError (*args, **kwargs)`

  Encoding error raised by block encodings when a byte block is invalid.

  Expand source code

      class InvalidByteBlockError(EncodingError):
          """
              Encoding error raised by block encodings when a byte block is invalid.
          """
          ...

  ### Ancestors

  - EncodingError
  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException
`class InvalidCharBlockError (*args, **kwargs)`

  Decoding error raised by block encodings when a char block is invalid.

  Expand source code

      class InvalidCharBlockError(DecodingError):
          """
              Decoding error raised by block encodings when a char block is invalid.
          """
          ...

  ### Ancestors

  - DecodingError
  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException
`class InvalidDigitError (digit: int, base: int)`

  Encoding error raised when a digit does not index a character in the given alphabet (because `not 0 <= digit < len(alphabet)`)

  Expand source code

      class InvalidDigitError(EncodingError):
          """
              Encoding error raised when a digit does not index a character
              in the given alphabet (because `not 0 <= digit < len(alphabet)`)
          """

          digit: int
          base: int

          def __init__(self, digit: int, base: int) -> None:
              self.digit = digit
              self.base = base
              if 0 <= digit < base:
                  raise ValueError(f"Digit {digit} is valid for base {base}.")
              error_msg = f"Invalid digit {digit} encountered for base {base}."
              super().__init__(error_msg)

  ### Ancestors

  - EncodingError
  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException

  ### Class variables

  `var base : int`
  `var digit : int`
`class NonAlphabeticCharError (char: str, alphabet: Alphabet)`

  Decoding error raised when a character is not in the given alphabet, (considering case-insensitivity of the alphabet, if relevant).

  Expand source code

      class NonAlphabeticCharError(DecodingError):
          """
              Decoding error raised when a character is not in the given alphabet,
              (considering case-insensitivity of the alphabet, if relevant).
          """

          char: str
          alphabet: Alphabet

          def __init__(self, char: str, alphabet: Alphabet) -> None:
              self.char = char
              self.alphabet = alphabet
              if char in alphabet:
                  raise ValueError(f"Character {repr(char)} is alphabetic.")
              error_msg = f"Invalid character {repr(char)} encountered for alphabet {str(alphabet)}."
              super().__init__(error_msg)

  ### Ancestors

  - DecodingError
  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException

  ### Class variables

  `var alphabet : Alphabet`
  `var char : str`
`class PaddingError (padding: int, expected_padding: int)`

  Decoding error raised when a string to be decoded has incorrect padding.

  Expand source code

      class PaddingError(DecodingError):
          """
              Decoding error raised when a string to be decoded has incorrect padding.
          """

          padding: int
          expected_padding: int

          def __init__(self, padding: int, expected_padding: int) -> None:
              self.padding = padding
              self.expected_padding = expected_padding
              if padding < expected_padding:
                  error_msg = f"Insufficient padding: found {padding}, expected {expected_padding}"
              elif padding > expected_padding:
                  error_msg = f"Excessive padding: found {padding}, expected {expected_padding}"
              else:
                  raise ValueError("Padding is exactly what was expected.")
              super().__init__(error_msg)

  ### Ancestors

  - DecodingError
  - Error
  - binascii.Error
  - builtins.ValueError
  - builtins.Exception
  - builtins.BaseException

  ### Class variables

  `var expected_padding : int`
  `var padding : int`