Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Turning a string into an programatic mathematical expression

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


dbickett at gmail

Oct 19, 2004, 7:30 PM

Post #1 of 5 (438 views)
Permalink
Turning a string into an programatic mathematical expression

The title really says it all. I'm trying to take input from a user
(intended to be a mathematical expression), from a text box for
example, and evaluate it mathematically within the program. For
clarification: the user inputs the string "4*5(3-3)", I would be
interested in a straight-forward way to find the result of that, based
only on a string. The follow-up question would be how to incorporate
variables into the mix, however I'll leave it at that for now. Thanks
for your time :)

Daniel Bickett
--
http://mail.python.org/mailman/listinfo/python-list


jdhunter at nitace

Oct 19, 2004, 7:17 PM

Post #2 of 5 (402 views)
Permalink
Re: Turning a string into an programatic mathematical expression [In reply to]

>>>>> "Daniel" == Daniel Bickett <dbickett [at] gmail> writes:

Daniel> The title really says it all. I'm trying to take input
Daniel> from a user (intended to be a mathematical expression),
Daniel> from a text box for example, and evaluate it
Daniel> mathematically within the program. For clarification: the
Daniel> user inputs the string "4*5(3-3)", I would be interested
Daniel> in a straight-forward way to find the result of that,
Daniel> based only on a string. The follow-up question would be
Daniel> how to incorporate variables into the mix, however I'll
Daniel> leave it at that for now. Thanks for your time :)

Depending on what sort of grammar you want to require for input
strings, it can be as simple as

>>> s = "4*5*(3-3)"
>>> eval(s)
0

Although it may not appear so at first glance, I think you are
describing a proposed solution to your problem and not the problem you
are really trying to solve. If you restate the problem you are trying
to solve, you may get better answers. From my read of your question,
you're on the road to writing a mathematical expression parser. Note
that python has an expression parser built in, and it gives you access
to that parser in www.python.org/doc/current/lib/module-parser.html;,
by reusing the python parser you may save yourself some labor.

If you are interested in numeric rather than symbolic processing of
the mathematical expressions, the weave module, via blitz expression
templates, does something quite close. You give it strings like
'a*(b+c)' where a, b and c are numeric arrays, and it will use blitz
expression templates to process this in a single loop without the
creation of temporaries and multiple loops that such expressions
generally entail - www.scipy.org/documentation/weave .

The book "Scientific and Engineering C++" by Barton and Nackman
describes how to implement a mathematical expression parser that
supports variables, binary operations, etc.

JDH

--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Oct 19, 2004, 7:46 PM

Post #3 of 5 (399 views)
Permalink
Re: Turning a string into an programatic mathematical expression [In reply to]

Daniel Bickett wrote:

> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
> for your time :)
>
> Daniel Bickett

Well, you can simply use input(), a horrendously dangerous function that
was designed (if that's the right word) in less security-minded times to
allow users to enter expressions which would be made available to the
program:

>>> print input("What: ")
What: 4*5*(3-3)
0
>>> print input("What: ")
What: 24+35/7
29
>>>

Don't know whether this will help. It's also possible to use variables
in your expressions:

>>> a=33
>>> b=15.5
>>> print input("What: ")
What: a/b
2.12903225806
>>>

Note that the inputs must be valid Python expressions, which
unfortunately removes the possiblity of your implied multiplication:

>>> print input("What: ")
What: 4*5(3-3)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<string>", line 0, in ?
TypeError: 'int' object is not callable
>>>

If this isn't going to help you then I'm afraid you'll have to get down
and dirty by parsing the expressions and evaluating them in detail.

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


steve at holdenweb

Oct 19, 2004, 7:50 PM

Post #4 of 5 (419 views)
Permalink
Re: Turning a string into an programatic mathematical expression [In reply to]

Daniel Bickett wrote:

> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
> for your time :)
>
> Daniel Bickett

I should, perhaps, have explained that the input() built-in essentially
applies the eval() function to an input string. So, whatever the source
of your string you can use eval() to evaluate it.

The difficulty is that there's little control over what the user can
enter (though you do get the choice of providing dictionaries of local
and global variables it's hard to limit what users have access to and
still provide sufficient functionality).

>>> eval('"Hello" + " " + "world"')
'Hello world'
>>> eval("3+14/27.4")
3.5109489051094891
>>>

regards
Steve
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
--
http://mail.python.org/mailman/listinfo/python-list


ptmcg at austin

Oct 19, 2004, 9:21 PM

Post #5 of 5 (407 views)
Permalink
Re: Turning a string into an programatic mathematical expression [In reply to]

"Daniel Bickett" <dbickett [at] gmail> wrote in message
news:mailman.5204.1098239457.5135.python-list [at] python
> The title really says it all. I'm trying to take input from a user
> (intended to be a mathematical expression), from a text box for
> example, and evaluate it mathematically within the program. For
> clarification: the user inputs the string "4*5(3-3)", I would be
> interested in a straight-forward way to find the result of that, based
> only on a string. The follow-up question would be how to incorporate
> variables into the mix, however I'll leave it at that for now. Thanks
> for your time :)
>
> Daniel Bickett

This is a pretty standard text processing task, often assigned as homework
in CS classes. Check out this entry from the Python Tutor list
http://mail.python.org/pipermail/tutor/2003-December/027032.html, authored
by Danny Yoo, which includes many helpful points about this problem.

You can also find a working Python expression parser included with the
examples provided with the pyparsing parser module, to be found at
http://pyparsing.sourceforge.net .

-- Paul McGuire


--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.