f | from functools import wraps | f | from functools import wraps |
| | | |
| class Fix: | | class Fix: |
| | | |
| def __init__(self, n): | | def __init__(self, n): |
| if not 1 <= n <= 16: | | if not 1 <= n <= 16: |
| raise ValueError() | | raise ValueError() |
| self.n = n | | self.n = n |
| | | |
| def __call__(self, func): | | def __call__(self, func): |
| | | |
| @wraps(func) | | @wraps(func) |
| def wrapper(*args, **kwargs): | | def wrapper(*args, **kwargs): |
t | rounded_args = tuple((round(a, self.n) if isinstance(a, floa | t | rounded_args = tuple((round(arg, self.n) if isinstance(arg, |
| t) else a for a in args)) | | float) else arg for arg in args)) |
| rounded_kwargs = {k: round(v, self.n) if isinstance(v, float | | rounded_kwargs = {k: round(v, self.n) if isinstance(v, float |
| ) else v for k, v in kwargs.items()} | | ) else v for k, v in kwargs.items()} |
| result = func(*rounded_args, **rounded_kwargs) | | result = func(*rounded_args, **rounded_kwargs) |
| return round(result, self.n) if isinstance(result, float) el | | return round(result, self.n) if isinstance(result, float) el |
| se result | | se result |
| return wrapper | | return wrapper |