how to distinguish a python expression from a statement?
Fredrik Lundh
fredrik at pythonware.com
Sat Sep 1 05:17:00 EDT 2001
James_Althoff at i2.com wrote:
> I have all of this working except that I don't know how to distinguish
> between expressions and statements (without making the end user push one of
> two different buttons in order to tell me -- yuk). I am using Jython and
> so the parser module is not available (so I can't use parser.isexpr).
>
> Is there any means to accomplish this what-I-had-thought-would-be-routine
> task short of writing a Python parser?
sure:
dict = {}
code = compile(mysource, "<stdin>", "single")
exec code in dict
result = dict.get("_")
if result is not None:
print result
(use dict.has_key("_") to check if it really was an expression)
> I am trying to mimick the Python interpreter in a GUI.
the "code" module contains a framework for this; just instantiate
an InteractiveConsole, and use the "push" method to add lines as
they arrive from the GUI.
</F>
<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list