How Would You Do: Parsing and Expression Evaluation

A

Al Christians

I've got an idea for an application, and I wonder how much of what it
takes to create it is available in open source python components.

My plan is this -- I want to do a simple spreadsheet-like application:
I've used a wxGrid, so I expect that wxPython will do fine for the user
interface. The spreadsheet can be organized vertically, one item per
line. It might need no more than 100 rows or so. On each line, the
user will enter in one column an item (variable) name and, in another
column, a formula or expression for computing the item's value. A
third column will show the computed value. There will usually be some
circular references between the items, but their values will converge
quickly with repeated evaluation.

The final feature is a button that the user can click when they have the
sheet set up correctly -- it will translate the formulas into simple C
and python code to perform the same calculations as the spreadsheet.

The formulas should be pretty easy -- add, subtract, multiply, divide,
log, exponential, and perhaps some user-defined functions.

Are there any off-the-shelf python libraries that would make this kind
of app much easier? I don't have any special experience related to such
things, so I surely don't want to re-invent the wheel out of pure
ignorance -- should I try the parsing tools in the standard python
library, or will something else work better for me?

TIA for any advice,


Al
 
P

Paul McGuire

Al Christians said:
I've got an idea for an application, and I wonder how much of what it
takes to create it is available in open source python components.

My plan is this -- I want to do a simple spreadsheet-like application:
I've used a wxGrid, so I expect that wxPython will do fine for the user
interface. The spreadsheet can be organized vertically, one item per
line. It might need no more than 100 rows or so. On each line, the
user will enter in one column an item (variable) name and, in another
column, a formula or expression for computing the item's value. A
third column will show the computed value. There will usually be some
circular references between the items, but their values will converge
quickly with repeated evaluation.

The final feature is a button that the user can click when they have the
sheet set up correctly -- it will translate the formulas into simple C
and python code to perform the same calculations as the spreadsheet.

The formulas should be pretty easy -- add, subtract, multiply, divide,
log, exponential, and perhaps some user-defined functions.

Are there any off-the-shelf python libraries that would make this kind
of app much easier? I don't have any special experience related to such
things, so I surely don't want to re-invent the wheel out of pure
ignorance -- should I try the parsing tools in the standard python
library, or will something else work better for me?

TIA for any advice,


Al

One of the examples that comes with pyparsing is a simple 4-function
expression parser and evaluator, that is easily extendable to add
exponentiation and user-defined functions.

Download pyparsing at http://pyparsing.sourceforge.net .

-- Paul
 
L

Larry Bates

I may not be understanding what you want to do but
here goes:

You might just want to use eval() function and have
the expression evaluated by Python. That way your
expression could be any Python expression and you
don't have to parse the expression at all (e.g eval
parses and executes it and returns the value). You
also don't have to "translate" the formulas. They
are already Python expressions. Python expression
syntax is pretty much like what you would want.

Larry Bates
Syscon, Inc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top