Re: Transforming a str to an operator

2009-08-29 Thread MRAB
r wrote: On Aug 28, 8:43 pm, Anny Mous wrote: It isn't irrational to have a healthy caution towards eval. Ignorance is never an excuse for stupidity. No caution is needed if you know how to properly use eval. You can't shoot yourself in the foot without first pulling the trigger. Apart from

Re: Transforming a str to an operator

2009-08-29 Thread r
On Aug 28, 8:43 pm, Anny Mous wrote: > It isn't irrational to have a healthy caution towards eval. Ignorance is never an excuse for stupidity. No caution is needed if you know how to properly use eval. You can't shoot yourself in the foot without first pulling the trigger. > Apart from the secur

Re: Transforming a str to an operator

2009-08-28 Thread Anny Mous
r wrote: > Abviously the OP is a python baby noob and casting your irrational > fear (and many others irrational fears) of eval It isn't irrational to have a healthy caution towards eval. Apart from the security issues, running code in eval takes a massive performance hit. Its about ten times s

Re: Transforming a str to an operator

2009-08-28 Thread Gabriel Genellina
En Fri, 28 Aug 2009 01:50:37 -0300, Xavier Ho escribió: On Fri, Aug 28, 2009 at 2:35 PM, Ben Finney wrote: op_funcs = { '+': operator.add, '-': operator.sub, '*': operator.mul, '/': operator.div, } op_prompt = "Select an operator ({}):".format(','.j

Re: Transforming a str to an operator

2009-08-28 Thread Duke Normandin
On Fri, 28 Aug 2009, Ben Finney wrote: > Duke Normandin writes: > > > Hey > > > > I'm a Python noob > > > > So far so good! > > > > I've written the following: > > > > num1 = raw_input('Enter the first number: ') > > num2 = raw_input('Enter the second number: ') > > op = raw_input('Select

Re: Transforming a str to an operator

2009-08-28 Thread Duke Normandin
On Thu, 27 Aug 2009, r wrote: > On Aug 27, 10:52 pm, Duke Normandin wrote: > > How do I convert the contents of "op" from a string to an actual > > arithmetic operator? eval() does not seem to be the answer. TIA! > > > Try this.. > > >>> op = '+' > >>> one = '1' > >>> two = '2' > >>> one+op+two >

Re: Transforming a str to an operator

2009-08-28 Thread Duke Normandin
On Thu, 27 Aug 2009, Stephen Hansen wrote: > > > > num1 = raw_input('Enter the first number: ') > > num2 = raw_input('Enter the second number: ') > > op = raw_input('Select one of the following [+-*/]: ') > > print 'The answer is: ', int(num1), eval(op), int(num2) > >

Re: Transforming a str to an operator

2009-08-27 Thread Stephen Hansen
> > > I would use the following approach: > > Abviously the OP is a python baby noob and casting your irrational > fear (and many others irrational fears) of eval at him is akin to > tales of Chupacabras running a muck in the jungle sucking the blood > from live goats in the twilight hours. I use e

Re: Transforming a str to an operator

2009-08-27 Thread r
On Aug 27, 11:35 pm, Ben Finney wrote: > In general, ‘eval’ on unsanitised input is not the answer. Yes i agree. > I would use the following approach: Abviously the OP is a python baby noob and casting your irrational fear (and many others irrational fears) of eval at him is akin to tales of Ch

Re: Transforming a str to an operator

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 2:35 PM, Ben Finney > wrote: >import operator > >op_funcs = { >'+': operator.add, >'-': operator.sub, >'*': operator.mul, >'/': operator.div, >} > >num_1 = int(raw_input('Enter the first number: ')) >num_2 = int(raw_i

Re: Transforming a str to an operator

2009-08-27 Thread Ben Finney
Duke Normandin writes: > Hey > > I'm a Python noob > > So far so good! > > I've written the following: > > num1 = raw_input('Enter the first number: ') > num2 = raw_input('Enter the second number: ') > op = raw_input('Select one of the following [+-*/]: ') > print 'The answer is: ', int(n

Re: Transforming a str to an operator

2009-08-27 Thread Stephen Hansen
> > num1 = raw_input('Enter the first number: ') > num2 = raw_input('Enter the second number: ') > op = raw_input('Select one of the following [+-*/]: ') > print 'The answer is: ', int(num1), eval(op), int(num2) > > > How do I convert the contents of "op"

Re: Transforming a str to an operator

2009-08-27 Thread r
On Aug 27, 10:52 pm, Duke Normandin wrote: > How do I convert the contents of "op" from a string to an actual > arithmetic operator? eval() does not seem to be the answer. TIA! Try this.. >>> op = '+' >>> one = '1' >>> two = '2' >>> one+op+two '1+2' >>> eval(one+op+two) 3 you could also use s

Re: Transforming a str to an operator

2009-08-27 Thread Xavier Ho
On Fri, Aug 28, 2009 at 1:52 PM, Duke Normandin wrote: > How do I convert the contents of "op" from a string to an actual > arithmetic operator? eval() does not seem to be the answer. TIA! Maybe you were looking for print eval(num1 + op + num2) # it's a little ugly string concatenation. Hm? (