Question

Dag Hansteen d-hanst at online.no
Thu Aug 12 19:03:22 CEST 2004


hi Shane!
What I want is *just* to resolve an expression in a string like:
a = "2+2"

then

b = eval(a)

so b = 4

I dont want it to be possible to type for example commands such as __import__('os') and be able to use any functions in that module in the wxTextCtrl which is only ment to calculate numbers!

If you understand....

regards Dag Hansteen!


----- Original Message ----- 
From: "Shane Holloway (IEEE)" <shane.holloway at ieee.org>
To: "Dag Hansteen" <d-hanst at online.no>
Cc: <python-list at python.org>
Sent: Thursday, August 12, 2004 12:16 AM
Subject: Re: Question


> Dag,
> 
> If you want to execute python-type statements, then you will want to use 
> eval().  However, if your input is limited to smaller expressions, you 
> can parse the input into pieces and then interpret them.
> 
> So, what I'm saying is that you need to provide more information.  What 
> subset of eval's functionality do you need?
> 
> Regards,
> -Shane Holloway
> 
> Dag Hansteen wrote:
>  > I make a GUI calculator in wxPython.
>  > How can I do the following without having to use eval?:
>  >
>  >     def OnCalc(self, event):
>  >         try:
>  >             regnestykke = self.formel.GetValue()
>  >             svar = eval(regnestykke)
>  >             svar = str(svar)
>  >             self.answer.SetValue(svar)
>  >         except:
>  >             self.answer.SetValue("Error! Check Syntax!")
>  >
>  > """
>  > self.formel is defined as wxTextCtrl for the input from user to be
>  > calculated
>  > self.answer is defined as wxTextCtrl for the answer of the users input
>  > """
> 
> 
> 




More information about the Python-list mailing list