1
0
Fork 0

[CLI] Add a subcommand for getting information about a keyboard (#8666)

You can now use `qmk info` to get information about keyboards and keymaps.

Co-authored-by: Erovia <Erovia@users.noreply.github.com>
This commit is contained in:
Zach White 2020-05-26 13:05:41 -07:00 committed by GitHub
parent 5d3bf8a050
commit 751316c344
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 921 additions and 113 deletions

33
lib/python/qmk/math.py Normal file
View file

@ -0,0 +1,33 @@
"""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)