Download model/calculator.py from MSkilll/Etra: direct link, hf CLI and curl.
- Browser
- Download file 511 Bytes
-
https://huggingface.co/MSkilll/Etra/resolve/main/model/calculator.py
- Command line
-
hf download hf://MSkilll/Etra/model/calculator.py
-
curl -L -o calculator.py https://huggingface.co/MSkilll/Etra/resolve/main/model/calculator.py
511 Bytes
| import operator | |
| import re | |
| CALC_RE = re.compile(r"\s*(-?\d+(?:\.\d+)?)\s*([+\-*/])\s*(-?\d+(?:\.\d+)?)\s*") | |
| OPS = {"+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv} | |
| def calc(expr): | |
| m = CALC_RE.fullmatch(expr) | |
| if not m: | |
| return None | |
| a, op, b = float(m.group(1)), m.group(2), float(m.group(3)) | |
| if op == "/" and b == 0: | |
| return "Can't divide by zero." | |
| r = OPS[op](a, b) | |
| return str(int(r)) if r == int(r) else f"{r:.4f}".rstrip("0") |