f | from functools import wraps | f | from functools import wraps |
| | | |
| class Fix: | | class Fix: |
| | | |
n | def __init__(self, value): | n | def __init__(self, n): |
| self.value = value | | self.n = n |
| | | |
| def __call__(self, func): | | def __call__(self, func): |
| | | |
| @wraps(func) | | @wraps(func) |
t | def wrep(*args, **kwargs): | t | def wrapper(*args, **kwargs): |
| args = [round(x, self.value) if isinstance(x, float) else x | | rounded_args = [round(arg, self.n) if isinstance(arg, float) |
| for x in args] | | else arg for arg in args] |
| kwargs = {key: round(x, self.value) if isinstance(x, float) | | rounded_kwargs = {key: round(value, self.n) if isinstance(va |
| else x for key, x in kwargs.items()} | | lue, float) else value for key, value in kwargs.items()} |
| ans = func(*args, **kwargs) | | result = func(*rounded_args, **rounded_kwargs) |
| if isinstance(ans, float): | | if isinstance(result, float): |
| ans = round(ans, self.value) | | result = round(result, self.n) |
| return ans | | return result |
| return wrep | | return wrapper |