【发布时间】:2016-12-16 02:04:46
【问题描述】:
我正在开发一个“复制粘贴计算器”,它可以检测复制到系统剪贴板的任何数学表达式,评估它们并将答案复制到准备粘贴的剪贴板。然而,虽然代码使用了 eval() 函数,但考虑到用户通常知道他们在复制什么,我并不十分担心。话虽如此,我想找到一种更好的方法,而不会给计算带来障碍(= 例如,消除计算乘法或指数的能力)。
这是我的代码的重要部分:
#! python3
import pyperclip, time
parsedict = {"×": "*",
"÷": "/",
"^": "**"} # Get rid of anything that cannot be evaluated
def stringparse(string): # Remove whitespace and replace unevaluateable objects
a = string
a = a.replace(" ", "")
for i in a:
if i in parsedict.keys():
a = a.replace(i, parsedict[i])
print(a)
return a
def calculate(string):
parsed = stringparse(string)
ans = eval(parsed) # EVIL!!!
print(ans)
pyperclip.copy(str(ans))
def validcheck(string): # Check if the copied item is a math expression
proof = 0
for i in mathproof:
if i in string:
proof += 1
elif "http" in string: #TODO: Create a better way of passing non-math copies
proof = 0
break
if proof != 0:
calculate(string)
def init(): # Ensure previous copies have no effect
current = pyperclip.paste()
new = current
main(current, new)
def main(current, new):
while True:
new = pyperclip.paste()
if new != current:
validcheck(new)
current = new
pass
else:
time.sleep(1.0)
pass
if __name__ == "__main__":
init()
问:我应该使用什么来代替 eval() 来计算答案?
【问题讨论】:
标签: python-3.x eval mathematical-expressions