fagus.utils module
This module contains classes and functions used across the Fagus-library that didn’t fit in another module
- class fagus.utils.FagusOption(name: str, default: ~typing.Any, type_: type = <class 'typing._SpecialForm'>, verify_function: ~typing.Callable[[~typing.Any], bool] = <function FagusOption.<lambda>>, verify_error_msg: ~typing.Optional[str] = None)
Bases:
object
Helper class to facilitate Fagus options.
- __init__(name: str, default: ~typing.Any, type_: type = <class 'typing._SpecialForm'>, verify_function: ~typing.Callable[[~typing.Any], bool] = <function FagusOption.<lambda>>, verify_error_msg: ~typing.Optional[str] = None) None
Initializes FagusOption with the given parameters
- Parameters:
name (str) – The name of the option.
default (Any) – The default value for the option if it hasn’t been set explicitly at class- or instance level or in the function.
type_ (type) – The expected type for the input to the option. Defaults to Any. In case the provided input to the option doesn’t have the type indicated here, an error-message is thrown.
verify_function (Callable[[Any], bool]) – A function to verify the input value to the option. Returns a bool whether the input was valid or not. An error is thrown if the input isn’t valid with the error message defined in verify_error_message. Defaults to lambda x: True, meaning that any input is valid
verify_error_msg (Optional[str]) – An error message to display when the verify_function returns False. Defaults to f”{value} is not a valid value for {self.name}”
- Returns:
None
- verify(value: Any) Any
Verifies if the input value to the option has the correct type and passes the validation function.
- Parameters:
value (Any) – The option input value to be verified.
- Raises:
TypeError – If the input value is not of the expected type.
ValueError – If the input value does not pass the custom validation function.
- Returns:
The input value if it meets the requirements.
- Return type:
Any
- __dict__ = mappingproxy({'__module__': 'fagus.utils', '__doc__': 'Helper class to facilitate Fagus options.', '__init__': <function FagusOption.__init__>, 'verify': <function FagusOption.verify>, '__dict__': <attribute '__dict__' of 'FagusOption' objects>, '__weakref__': <attribute '__weakref__' of 'FagusOption' objects>, '__annotations__': {}})
- __module__ = 'fagus.utils'
- __weakref__
list of weak references to the object (if defined)
- fagus.utils.EllipsisType
TypeAlias to represent type(…), which cannot be done in a nicer way prior to Python 3.10
- fagus.utils.OptStr
TypeAlias for FagusOption requiring a str. Specify custom value as str, or keep … to use FagusOption default.
alias of
Union
[str
,ellipsis
]
- fagus.utils.OptBool
TypeAlias for FagusOption requiring a bool. Specify custom value as bool, or keep … to use FagusOption default.
alias of
Union
[bool
,ellipsis
]
- fagus.utils.OptInt
TypeAlias for FagusOption requiring an int. Specify custom value as int, or keep … to use FagusOption default.
alias of
Union
[int
,ellipsis
]
- fagus.utils.OptAny: TypeAlias = typing.Any
TypeAlias for FagusOption taking any object. Specify custom value, or keep … to use FagusOption default.