diff options
| author | Nick Brassel | 2025-11-27 14:36:49 +0100 |
|---|---|---|
| committer | GitHub | 2025-11-27 14:36:49 +0100 |
| commit | 9c2ca00074784dbee27b459d71cfc8e75f47b976 (patch) | |
| tree | a59576289fd024bf35b0573db70eb8862ed44568 /lib/python/qmk/math.py | |
| parent | 594558ec7b9ac1963870447778426682065e0d20 (diff) | |
QMK CLI Environment bootstrapper (#25038)
Co-authored-by: Joel Challis <git@zvecr.com>
Co-authored-by: Pascal Getreuer <getreuer@google.com>
Diffstat (limited to 'lib/python/qmk/math.py')
| -rw-r--r-- | lib/python/qmk/math.py | 33 |
1 files changed, 0 insertions, 33 deletions
diff --git a/lib/python/qmk/math.py b/lib/python/qmk/math.py deleted file mode 100644 index 88dc4a300c..0000000000 --- a/lib/python/qmk/math.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Parse arbitrary math equations in a safe way. - -Gratefully copied from https://stackoverflow.com/a/9558001 -""" -import ast -import operator as op - -# supported operators -operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor, ast.USub: op.neg} - - -def compute(expr): - """Parse a mathematical expression and return the answer. - - >>> compute('2^6') - 4 - >>> compute('2**6') - 64 - >>> compute('1 + 2*3**(4^5) / (6 + -7)') - -5.0 - """ - return _eval(ast.parse(expr, mode='eval').body) - - -def _eval(node): - if isinstance(node, ast.Num): # <number> - return node.n - elif isinstance(node, ast.BinOp): # <left> <operator> <right> - return operators[type(node.op)](_eval(node.left), _eval(node.right)) - elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1 - return operators[type(node.op)](_eval(node.operand)) - else: - raise TypeError(node) |