Skip to content

Validators

_Validator

Bases: abc.ABC

Base class of all validator classes.

Source code in pyflp/_validators.py
23
24
25
26
27
28
29
30
31
class _Validator(abc.ABC):
    """Base class of all validator classes."""

    def __repr__(self) -> str:
        return f"<{type(self).__name__}>"

    @abc.abstractmethod
    def validate(self, value: Any):
        pass

__repr__()

Source code in pyflp/_validators.py
26
27
def __repr__(self) -> str:
    return f"<{type(self).__name__}>"

validate(value) abstractmethod

Source code in pyflp/_validators.py
29
30
31
@abc.abstractmethod
def validate(self, value: Any):
    pass

_BoolValidator

Bases: _OneOfValidator

Validates whether a value equals True or False.

Source code in pyflp/_validators.py
51
52
53
54
55
56
57
58
59
60
61
62
class _BoolValidator(_OneOfValidator):
    """Validates whether a value equals `True` or `False`."""

    def __init__(self) -> None:
        super().__init__((True, False))

    def __repr__(self) -> str:
        return "<BoolValidator>"

    def validate(self, value: Any) -> None:
        if not isinstance(value, bool):
            raise TypeError(f"Expected {value!r} to be a bool")

__init__()

Source code in pyflp/_validators.py
54
55
def __init__(self) -> None:
    super().__init__((True, False))

__repr__()

Source code in pyflp/_validators.py
57
58
def __repr__(self) -> str:
    return "<BoolValidator>"

validate(value)

Source code in pyflp/_validators.py
60
61
62
def validate(self, value: Any) -> None:
    if not isinstance(value, bool):
        raise TypeError(f"Expected {value!r} to be a bool")

_BytesValidator

Bases: _BytesStrValidatorBase

Validates the type and size of a bytes object.

Source code in pyflp/_validators.py
159
160
161
162
163
164
165
class _BytesValidator(_BytesStrValidatorBase):
    """Validates the type and size of a `bytes` object."""

    def validate(self, value: Any) -> None:
        if not isinstance(value, bytes):
            raise TypeError(f"Expected {value!r} to be a bytes object")
        super().validate(value)

validate(value)

Source code in pyflp/_validators.py
162
163
164
165
def validate(self, value: Any) -> None:
    if not isinstance(value, bytes):
        raise TypeError(f"Expected {value!r} to be a bytes object")
    super().validate(value)

_BytesStrValidatorBase

Bases: _Validator, abc.ABC

Base class for _BytesValidator and _StrValidator.

Source code in pyflp/_validators.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
class _BytesStrValidatorBase(_Validator, abc.ABC):
    """Base class for `_BytesValidator` and `_StrValidator`."""

    def __init__(
        self, minsize: Optional[int] = None, maxsize: Optional[int] = None
    ) -> None:
        self.__minsize = minsize
        self.__maxsize = maxsize

    def __repr__(self) -> str:
        return f"<{type(self).__name__} min={self.__minsize}, max={self.__maxsize}>"

    def validate(self, value: Any):
        if self.__minsize is not None and len(value) < self.__minsize:
            raise ValueError(
                f"Expected {value!r} to be no smaller than {self.__minsize!r}"
            )
        if self.__maxsize is not None and len(value) > self.__maxsize:
            raise ValueError(
                f"Expected {value!r} to be no bigger than {self.__maxsize!r}"
            )

__maxsize = maxsize instance-attribute

__minsize = minsize instance-attribute

__init__(minsize=None, maxsize=None)

Source code in pyflp/_validators.py
139
140
141
142
143
def __init__(
    self, minsize: Optional[int] = None, maxsize: Optional[int] = None
) -> None:
    self.__minsize = minsize
    self.__maxsize = maxsize

__repr__()

Source code in pyflp/_validators.py
145
146
def __repr__(self) -> str:
    return f"<{type(self).__name__} min={self.__minsize}, max={self.__maxsize}>"

validate(value)

Source code in pyflp/_validators.py
148
149
150
151
152
153
154
155
156
def validate(self, value: Any):
    if self.__minsize is not None and len(value) < self.__minsize:
        raise ValueError(
            f"Expected {value!r} to be no smaller than {self.__minsize!r}"
        )
    if self.__maxsize is not None and len(value) > self.__maxsize:
        raise ValueError(
            f"Expected {value!r} to be no bigger than {self.__maxsize!r}"
        )

_ColorValidator

Bases: _Validator

Validates whether the type of value is colour.Color.

Source code in pyflp/_validators.py
188
189
190
191
192
193
class _ColorValidator(_Validator):
    """Validates whether the type of value is `colour.Color`."""

    def validate(self, value: Any) -> None:
        if not isinstance(value, colour.Color):
            raise TypeError(f"Expected {value!r} to be a 'colour.Color' object")

validate(value)

Source code in pyflp/_validators.py
191
192
193
def validate(self, value: Any) -> None:
    if not isinstance(value, colour.Color):
        raise TypeError(f"Expected {value!r} to be a 'colour.Color' object")

_EnumValidator

Bases: _OneOfValidator

Validates whether a value exists in a particular enum class.

Source code in pyflp/_validators.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class _EnumValidator(_OneOfValidator):
    """Validates whether a value exists in a particular `enum` class."""

    _Enum = TypeVar("_Enum", bound=enum.Enum)

    def __init__(self, enum: _Enum) -> None:
        self.__enum = enum
        super().__init__(tuple(enum))

    def __repr__(self) -> str:
        return f'<EnumValidator enum="{self.__enum.__name__}>"'

    def validate(self, value: Any) -> None:
        if issubclass(self.__enum, (enum.IntEnum, enum.IntFlag)):
            if not isinstance(value, int):
                raise TypeError(f"Expected {value!r} to be an int")
        return super().validate(value)

__enum = enum instance-attribute

__init__(enum)

Source code in pyflp/_validators.py
70
71
72
def __init__(self, enum: _Enum) -> None:
    self.__enum = enum
    super().__init__(tuple(enum))

__repr__()

Source code in pyflp/_validators.py
74
75
def __repr__(self) -> str:
    return f'<EnumValidator enum="{self.__enum.__name__}>"'

validate(value)

Source code in pyflp/_validators.py
77
78
79
80
81
def validate(self, value: Any) -> None:
    if issubclass(self.__enum, (enum.IntEnum, enum.IntFlag)):
        if not isinstance(value, int):
            raise TypeError(f"Expected {value!r} to be an int")
    return super().validate(value)

_FloatValidator

Bases: _IntFloatValidatorBase

Validates whether value is an float and lies in a range, optionally.

Source code in pyflp/_validators.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class _FloatValidator(_IntFloatValidatorBase):
    """Validates whether value is an `float` and lies in a range, optionally."""

    def __init__(
        self, min_: Optional[float] = None, max_: Optional[float] = None
    ) -> None:
        self.__min = min_
        self.__max = max_

    def validate(self, value: Any):
        if type(value) is not float:
            raise TypeError(f"Expected {value!r} to be a float")
        if self.__min is not None and value < self.__min:
            raise ValueError(f"Expected {value!r} to be at least {self.__min!r}")
        if self.__max is not None and value > self.__max:
            raise ValueError(f"Expected {value!r} to be no more than {self.__max!r}")

__max = max_ instance-attribute

__min = min_ instance-attribute

__init__(min_=None, max_=None)

Source code in pyflp/_validators.py
121
122
123
124
125
def __init__(
    self, min_: Optional[float] = None, max_: Optional[float] = None
) -> None:
    self.__min = min_
    self.__max = max_

validate(value)

Source code in pyflp/_validators.py
127
128
129
130
131
132
133
def validate(self, value: Any):
    if type(value) is not float:
        raise TypeError(f"Expected {value!r} to be a float")
    if self.__min is not None and value < self.__min:
        raise ValueError(f"Expected {value!r} to be at least {self.__min!r}")
    if self.__max is not None and value > self.__max:
        raise ValueError(f"Expected {value!r} to be no more than {self.__max!r}")

_OneOfValidator

Bases: _Validator

Validates whether a value exists in a given set of options.

Source code in pyflp/_validators.py
37
38
39
40
41
42
43
44
45
46
47
48
class _OneOfValidator(_Validator):
    """Validates whether a value exists in a given set of options."""

    def __init__(self, options: Iterable[Any]):
        self.__options = options

    def __repr__(self) -> str:
        return f"<{type(self).__name__} options={self.__options!r}>"

    def validate(self, value: Any):
        if value not in self.__options:
            raise ValueError(f"Expected {value!r} to be one of {self.__options!r}")

__options = options instance-attribute

__init__(options)

Source code in pyflp/_validators.py
40
41
def __init__(self, options: Iterable[Any]):
    self.__options = options

__repr__()

Source code in pyflp/_validators.py
43
44
def __repr__(self) -> str:
    return f"<{type(self).__name__} options={self.__options!r}>"

validate(value)

Source code in pyflp/_validators.py
46
47
48
def validate(self, value: Any):
    if value not in self.__options:
        raise ValueError(f"Expected {value!r} to be one of {self.__options!r}")

_IntFloatValidatorBase

Bases: _Validator, abc.ABC

Base class for _IntValidator and _FloatValidator.

Source code in pyflp/_validators.py
84
85
86
87
88
89
90
91
92
class _IntFloatValidatorBase(_Validator, abc.ABC):
    """Base class for `_IntValidator` and `_FloatValidator`."""

    def __init__(self, min_, max_) -> None:  # pragma: no cover
        self.__min = min_
        self.__max = max_

    def __repr__(self) -> str:
        return f"<{type(self).__name__} min={self.__min}, max={self.__max}>"

__max = max_ instance-attribute

__min = min_ instance-attribute

__init__(min_, max_)

Source code in pyflp/_validators.py
87
88
89
def __init__(self, min_, max_) -> None:  # pragma: no cover
    self.__min = min_
    self.__max = max_

__repr__()

Source code in pyflp/_validators.py
91
92
def __repr__(self) -> str:
    return f"<{type(self).__name__} min={self.__min}, max={self.__max}>"

_IntValidator

Bases: _IntFloatValidatorBase

Validates whether value is an int and lies in a range, optionally.

Source code in pyflp/_validators.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
class _IntValidator(_IntFloatValidatorBase):
    """Validates whether value is an `int` and lies in a range, optionally."""

    def __init__(self, min_: Optional[int] = None, max_: Optional[int] = None):
        self.__min = min_
        self.__max = max_

    def validate(self, value: Any):
        if type(value) is not int:  # https://stackoverflow.com/a/37888668
            raise TypeError(f"Expected {value!r} to be an int")
        if self.__min is not None and value < self.__min:
            raise ValueError(f"Expected {value!r} to be at least {self.__min!r}")
        if self.__max is not None and value > self.__max:
            raise ValueError(f"Expected {value!r} to be no more than {self.__max!r}")

__max = max_ instance-attribute

__min = min_ instance-attribute

__init__(min_=None, max_=None)

Source code in pyflp/_validators.py
 98
 99
100
def __init__(self, min_: Optional[int] = None, max_: Optional[int] = None):
    self.__min = min_
    self.__max = max_

validate(value)

Source code in pyflp/_validators.py
102
103
104
105
106
107
108
def validate(self, value: Any):
    if type(value) is not int:  # https://stackoverflow.com/a/37888668
        raise TypeError(f"Expected {value!r} to be an int")
    if self.__min is not None and value < self.__min:
        raise ValueError(f"Expected {value!r} to be at least {self.__min!r}")
    if self.__max is not None and value > self.__max:
        raise ValueError(f"Expected {value!r} to be no more than {self.__max!r}")

_StrValidator

Bases: _BytesStrValidatorBase

Validates the type and size of an str object.

Source code in pyflp/_validators.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
class _StrValidator(_BytesStrValidatorBase):
    """Validates the type and size of an `str` object."""

    def __init__(
        self,
        minsize: Optional[int] = None,
        maxsize: Optional[int] = None,
        mustascii: bool = False,
    ) -> None:
        super().__init__(minsize, maxsize)
        self.__mustascii = mustascii

    def validate(self, value: Any):
        if not isinstance(value, str):
            raise TypeError(f"Expected {value!r} to be an str")
        if self.__mustascii and not isascii(value):
            raise TypeError(f"Expected {value!r} to be an ASCII string")
        super().validate(value)

__mustascii = mustascii instance-attribute

__init__(minsize=None, maxsize=None, mustascii=False)

Source code in pyflp/_validators.py
171
172
173
174
175
176
177
178
def __init__(
    self,
    minsize: Optional[int] = None,
    maxsize: Optional[int] = None,
    mustascii: bool = False,
) -> None:
    super().__init__(minsize, maxsize)
    self.__mustascii = mustascii

validate(value)

Source code in pyflp/_validators.py
180
181
182
183
184
185
def validate(self, value: Any):
    if not isinstance(value, str):
        raise TypeError(f"Expected {value!r} to be an str")
    if self.__mustascii and not isascii(value):
        raise TypeError(f"Expected {value!r} to be an ASCII string")
    super().validate(value)

_UIntValidator

Bases: _IntValidator

A specialization of _IntValidator for validating positive integers.

Source code in pyflp/_validators.py
111
112
113
114
115
class _UIntValidator(_IntValidator):
    """A specialization of `_IntValidator` for validating positive integers."""

    def __init__(self, max_: Optional[int] = None) -> None:
        super().__init__(min_=0, max_=max_)

__init__(max_=None)

Source code in pyflp/_validators.py
114
115
def __init__(self, max_: Optional[int] = None) -> None:
    super().__init__(min_=0, max_=max_)