[Tutor] Is it possible for a Python Program to send commands to thePython Interpreter?

Alan Gauld alan.gauld at btinternet.com
Fri Feb 19 20:37:41 CET 2010


"Carnell, James E" <jecarnell at saintfrancis.com> wrote

> I am trying to teach a computer program - to program. It builds grammars
> and procedural memories on dictionary networks. How do I get the program
> to be able to input to the interpreter/command line and read the
> results? 

You probably don;t want to do that. I suspect you just want to 
execute a string that your program has created? If so you can 
use the exec or eval functions in Python.

If you do want to create an interactive prompt for your users you might 
like to look at the cmd module which includes the tools for building 
a command prompt type environment similar to the pdb debugger.

> I have done this a wee bit with making a dictionary of
> functions, but in a sense I am creating a script language on top of a
> script language so it seems really dumb to me especially since I have to
> recreate the grammar.

Not really sure I understand? Are you creating a new grammar or are 
you using Python syntax etc but just adding new features/commands?
If the outputr is valid python syntax then you can use exec/eval but if 
you are creating your own dialect then you will need to buld a parser 
- using any of the excellent python parsing tools available?

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/





More information about the Tutor mailing list