[PythonCAD] Anyone have experience with the parser module?

Art Haas ahaas at airmail.net
Thu Feb 10 00:42:40 CET 2005


Hi.

I've been busy with non-computer related things lately, so there has
been very little PythonCAD development activity of late. I removed some
unused code here and there and a few routines that were scheduled to
disappear, but not much new code has appeared lately.

While thinking about what parts of PythonCAD to work on, I began to
re-examine the handling of the commands, and in general how text from
the entry box at the bottom of the screen, is dealt with. Right now,
when text entry can provide data for a tool the usual way to handle this
is via eval(). That's all fine and good, but in view of the goal of
making the program scriptable this approach is too simplistic. A bit of
reading and tinkering with the 'exec' keyword demonstrated how it would
easy to add the ability to store variables and procedures on a per-image
basis. The key is storing in each image a dictionary that 'exec' can
populate with variables, functions, etc., as well as use previous stored
values. The syntax seems a bit odd though:

exec cmd in gdict, ldict

The variable 'cmd' above would be the text taken from the entry box, and
'gdict' is a dictionary containing the global variables used for
variable lookup and storage, and 'ldict' is a dictionary for the same
purpose but with a local variable scope. So, if 'cmd' looked like

a = 7

Then the command above would store the variable 'a' with a value of 7 in
the 'gdict' dictionary. This variable could then be used in an eval()
statement

eval('a', gdict)

This should return '7'. If the 'gdict' argument is not supplied, then
the eval() examines the globals variables defined in the context where
the eval() was called, so if there is no 'a' variable defined an error
is raised.

The difficulty I've encountered is that I want to make processing of the
entry box text very flexible, so sometimes I need to call 'exec' and
other times eval(), and I haven't found a good way to accomplish this.
I've started looking at the parser module in an effort to provide this
flexibility as it appears to return a parse tree than can be examined
prior to executing the tree contents. I'm not familiar with the parser
module, though, so there are certainly some subtleties when using it that
someone having done so would know. Anyone fitting that description out
there and be willing to answer some questions?

Art
-- 
Man once surrendering his reason, has no remaining guard against absurdities
the most monstrous, and like a ship without rudder, is the sport of every wind.

-Thomas Jefferson to James Smith, 1822


More information about the PythonCAD mailing list