gillesB / azulejo

Window resizing and tiling utility
MIT License
6 stars 1 forks source link

Avoid eval #15

Open gillesB opened 3 years ago

gillesB commented 3 years ago

eval() should be avoided. At the moment it is needed to calculate the new position of the window.

Something similar to this could be used: https://levelup.gitconnected.com/3-ways-to-write-a-calculator-in-python-61642f2e4a9a

from operator import pow, truediv, mul, add, sub  

operators = {
  '+': add,
  '-': sub,
  '*': mul,
  '/': truediv
}

def calculate(s):
    if s.isdigit():
        return float(s)
    for c in operators.keys():
        left, operator, right = s.partition(c)
        if operator in operators:
            return operators[operator](calculate(left), calculate(right))

calc = input("Type calculation:\n")
print("Answer: " + str(calculate(calc)))