The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
[Originally posted by hbm]

I have a need to use Python to evaluate user-defined expressions and formulae
as exactly described at the top of page 245. The paragraph relating to
Expression evaluation says I can "pass Python the data and function to
evaluate directly...(and) can offer a wider range of functions. This can be
extended to user-coded business rules..." - again, this is exactly what I need
to do. I'm trying to avoid creating my own language interpreter. While I'm
thoroughly enjoying this book, and am working my way through it, I was
wondering if there are any sources of information or texts that might describe
how to do this in more detail?

import-bot (20211) [Avatar] Offline
Re: Expression Evaluation
[Originally posted by hbm]

I've had some time to think about some of your suggestions, and as it turns
out, kwParser is exactly what I'm looking for! Many, many thanks!!

import-bot (20211) [Avatar] Offline
Re: Expression Evaluation
[Originally posted by daryl harms]

Hi Heather,

I'm not aware of specific book sections are articles that cover this
specifically. Andy Robinson (who wrote that chapter) has his own book out now
with Mark Hammond which gives a lot more detail and specifics of using Python
with COM than you find in this chapter. But this doesn't appear to be what you
are asking for.

I'm also not exactly sure of what type of application you need to develop.
Hopefully, something in the following though will be of help.

First, you might find the built-in function "eval" and/or the "exec" statement
very useful. If I was to do an update on the book, I'd want to add detailed
examples of their use as I continually find they are very useful in one
application after the other. There is one example in the book on page 282
where Guido uses "eval" in his enter function along with some description of
it in the last paragraph of section 24.3. But this is within the context of
JPython (now Jython).

The most often used of these two is "eval" which will evaluate a string as if
it were a Python expression in the context of the current namespace or a
namespace you define for it by passing in a local and global dictionaries. A
Python expression can be loosely thought of as anything that would be valid on
the right side of a an assignment statement (i.e. a = eval(aString) ). The
"exec" statement will execute a string as if it were a Python statement. So it
can be used to for example dynamically define new functions are classes.

One caveat with the use of "eval" and "exec" is that since they will execute
any code given, they may pose a security loophole if you use them directly on
uncontrolled input from untrusted but knowledgable users.

If you want to create something that takes input interactively from the users
you might want to look at the built-in module "cmd" (see the documentation on
it in the Library Reference that is bundled with Python). Related to it there
is also Steinar Knutsen LineInterpreter program (
and Doug Hellman's pybox library

It sounds like this would be beyond what you want, but If you are looking to
go to the level of looking at generic language parsing you might want to look
at the kwParser that come with a Python based SQL called Gadfly:

Hope this helps. Feel free to follow up with further questions.

import-bot (20211) [Avatar] Offline
Re: Expression Evaluation
[Originally posted by hbm]

Hi Daryl,
Thanks so much! This gives me some clues to check out...much appreciated!