f | from functools import wraps | f | from functools import wraps |
| | | |
| class Fix: | | class Fix: |
| | | |
n | def __init__(self, n): | n | def __init__(self, precision): |
| self.n = n | | self.precision = precision |
| | | |
| def __call__(self, func): | | def __call__(self, func): |
| | | |
| @wraps(func) | | @wraps(func) |
n | def wrapper(*args, **kwargs): | n | def wrapped(*args, **kwargs): |
| args = tuple((self._round(value) for value in args)) | | processed_args = tuple((self._apply_rounding(arg) for arg in |
| | | args)) |
| kwargs = {key: self._round(value) if isinstance(value, float | | processed_kwargs = {key: self._apply_rounding(value) if isin |
| ) else value for key, value in kwargs.items()} | | stance(value, float) else value for key, value in kwargs.items()} |
| result = func(*args, **kwargs) | | result = func(*processed_args, **processed_kwargs) |
| return self._round(result) | | return self._apply_rounding(result) |
| return wrapper | | return wrapped |
| | | |
n | def _round(self, value): | n | def _apply_rounding(self, value): |
| if isinstance(value, float): | | if isinstance(value, float): |
t | return round(value, self.n) | t | return round(value, self.precision) |
| return value | | return value |