Reputation: 43
can sonmeone help me replace deprecated parse in script, tried with ast but still dont get any error but it dont return the result just error11.
I belive this is because parse is deprecated, so can anyone help replace it when evaluating?
When i trie to get a result it always return error11
I belive the error is because parse is depretacted but im not sure about that
from tkinter import *
import parse
from math import factorial
from OCP.formularios import tabmain3
i = 0
def calc():
frmcalc = LabelFrame(tabmain3, text="Calculator", padx=5, pady=5, highlightbackground="black", highlightthickness=2)
frmcalc.grid(row=1, column=0, sticky=W, pady=2)
def get_variables(num):
global i
display.insert(i, num)
i += 1
def calculate():
entire_string = display.get()
print("ddddd "+entire_string)
try:
a = parse.expr(entire_string).compile()
print(a)
result = eval(a)
clear_all()
display.insert(0, result)
except Exception:
clear_all()
display.insert(0, "Error11"). <--- always get this error
def get_operation(operator):
global i
length = len(operator)
display.insert(i, operator)
i += length
def clear_all():
display.delete(0, END)
def undo():
entire_string = display.get()
if len(entire_string):
new_string = entire_string[:-1]
clear_all()
display.insert(0, new_string)
else:
clear_all()
display.insert(0, "Error")
def fact():
entire_string = display.get()
try:
result = factorial(int(entire_string))
clear_all()
display.insert(0, result)
except Exception:
clear_all()
display.insert(0, "Error")
display = Entry(frmcalc)
display.grid(row=1, columnspan=6, sticky=N + E + W + S)
Button(frmcalc, width="10",height = "5", text="1", command=lambda: get_variables(1)).grid(row=2, column=0, sticky=N + S + E + W)
Button(frmcalc, width="10",height = "5",text=" 2", command=lambda: get_variables(2)).grid(row=2, column=1, sticky=N + S + E + W)
Button(frmcalc, width="10",height = "5",text=" 3", command=lambda: get_variables(3)).grid(row=2, column=2, sticky=N + S + E + W)
Button(frmcalc, width="10",height = "5",text="4", command=lambda: get_variables(4)).grid(row=3, column=0, sticky=N + S + E + W)
Button(frmcalc, text=" 5", command=lambda: get_variables(5)).grid(row=3, column=1, sticky=N + S + E + W)
Button(frmcalc, text=" 6", command=lambda: get_variables(6)).grid(row=3, column=2, sticky=N + S + E + W)
Button(frmcalc, width="10",height = "5",text="7", command=lambda: get_variables(7)).grid(row=4, column=0, sticky=N + S + E + W)
Button(frmcalc, text=" 8", command=lambda: get_variables(8)).grid(row=4, column=1, sticky=N + S + E + W)
Button(frmcalc, text=" 9", command=lambda: get_variables(9)).grid(row=4, column=2, sticky=N + S + E + W)
Button(frmcalc, width="10",height = "5",text="AC", command=lambda: clear_all()).grid(row=5, column=0, sticky=N + S + E + W)
Button(frmcalc, text=" 0", command=lambda: get_variables(0)).grid(row=5, column=1, sticky=N + S + E + W)
Button(frmcalc, text=" .", command=lambda: get_variables(".")).grid(row=5, column=2, sticky=N + S + E + W)
Button(frmcalc, width="5",height = "5",text="+", command=lambda: get_operation("+")).grid(row=2, column=3, sticky=N + S + E + W)
Button(frmcalc, text="-", command=lambda: get_operation("-")).grid(row=3, column=3, sticky=N + S + E + W)
Button(frmcalc, text="*", command=lambda: get_operation("*")).grid(row=4, column=3, sticky=N + S + E + W)
Button(frmcalc, text="/", command=lambda: get_operation("/")).grid(row=5, column=3, sticky=N + S + E + W)
Button(frmcalc, width="5",text="pi", command=lambda: get_operation("*3.14")).grid(row=2, column=4, sticky=N + S + E + W)
Button(frmcalc, text="%", command=lambda: get_operation("%")).grid(row=3, column=4, sticky=N + S + E + W)
Button(frmcalc, text="(", command=lambda: get_operation("(")).grid(row=4, column=4, sticky=N + S + E + W)
Button(frmcalc, text="exp", command=lambda: get_operation("**")).grid(row=5, column=4, sticky=N + S + E + W)
Button(frmcalc, width="5",height = "5",text="<-", command=lambda: undo()).grid(row=2, column=5, sticky=N + S + E + W)
Button(frmcalc, text="x!", command=lambda: fact()).grid(row=3, column=5, sticky=N + S + E + W)
Button(frmcalc, text=")", command=lambda: get_operation(")")).grid(row=4, column=5, sticky=N + S + E + W)
Button(frmcalc, text="^2", command=lambda: get_operation("**2")).grid(row=5, column=5, sticky=N + S + E + W)
Button(frmcalc, text="^2", command=lambda: get_operation("**2")).grid(row=5, column=5, sticky=N + S + E + W)
Button(frmcalc, width="5",height = "5",text="=", command=lambda: calculate()).grid(columnspan=6, sticky=N + S + E + W)
Upvotes: 0
Views: 383
Reputation: 2458
(1) Your sample code is not a complete script that shows the problem when run. A complete script would presumably include:
calculate
.display
and clear_all
and parse
.If the problem is with the parse
object, it's especially important to know what that is / how it's defined. (Mind you, there isn't enough info here to know that parse
is indeed the problem.)
(2) As @rici indicated, if you change the except
block to something like:
except Exception as e:
clear_all()
display.insert(0, e)
you'll get output that is more informative to both you and anyone attempting to help you.
[After the above info was filled in...]
Okay, you're importing a module named parse
, and then calling the function parse.expr
, but the error message is telling you that there is no such function in the parse
module, so you get an Exception when you try to call it.
parse
doesn't appear to be a standard Python module (I don't see it listed here), so I can't help you much more. You may need to go back to whoever/whatever told you to use it, or find some documentation for it.
Or maybe you meant parser
. (import parser
and then parser.expr
)
Upvotes: 1