[Tutor] parsing?
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 1 Jul 2001 11:07:31 -0700 (PDT)
On Fri, 29 Jun 2001, Roeland Rengelink wrote:
> > I'm tying to make something that can read some values and functions
> from a file > and then use these to create a parametric surface in
> triangles to be rendered > by POV-Ray. The triangle generation isn't
> the problem, but reading the > functions into the program. >
>
> Unfortunately, in general this is not trivial.
>
>
> Presumably, the file you read contains statements in some kind of
> programming language. It is certainly possible to build a parser in
> Python for that language which then allows you to execute those
> statements in Python, as it were. This is not easy, and may not be the
> right solution anyway.
It sounds like something that dealt with rexec would be a good Useless
Python challenge; an example might make it easier to adjust it to do
parameteric surface stuff. Here's an idea for such a challenge:
It'd be great if we had a program that acts like a numerical integrator.
We'd be able to give this program some function, and have it do something
that a TI calculator might be able to do.
If we put on our "wishing makes it so" cap, we can imagine that running
Integrator might look like this:
###
[dyoo@einfall] ./Integrator.py
Hello, I'm a simple numerical integrator. For help, please type 'help'
at the prompt.
> help
Hello, this is the help file for Integrator. Here are a list of commands
you can do with me:
'list'
--- print out a list of functions we can integrate
'integrate([function], [left-point], [right-point])'
--- Return the numerical integration of a particular function,
starting from the left-point to the right-point.
'load([file])'
--- Load a function into the integrator.
'help'
--- You're looking at it.
'quit'
--- you'll need to quit.
> load('FyMathFunction.py') # Let's pretend that we have an
# MyMathFunction.py that
# contains a one-argument function.
Ok, I've loaded MyMathFunction().
> list
[MyMathFunction]
> integrate(MyMathFunction, 0, 10)
42
###
The MyMathFunction.py file itself would contain something like:
###
def MyMathFunction(x):
return 4.2
###
but of course, Integrator.py should be able to handle more interesting
functions.
I don't think I have time to write this at the moment, but perhaps someone
else can work on this one.