Generic number clipping function. Works with mypy type annotations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numbers import Number | |
from typing import TypeVar | |
N = TypeVar("N", bound=Number) | |
"""A number; e.g. either float or int. | |
""" | |
def clip(value: N, minimum: N, maximum: N) -> N: | |
"""Bounds the `value` to [`minimum`,`maximum`]. | |
""" | |
max_clip: N = min(value, maximum) | |
min_clip: N = min(max_clip, minimum) | |
return min_clip |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment