how to use "exec" stmt to get input from user

Rick King rickbking at comcast.net
Mon Aug 10 23:36:27 CEST 2009


Thanks for your comment.

The purpose of the application is to automate the manipulation of large 
numbers of files. For automation I want to be able to have scripts that 
I can use for various purposes.

On input, cmd.py handles calling "do_...." methods for known commands; 
for the rest I want to "exec" the lines with my own dictionary. This 
allows me to use python statements, so I can do things like 
concatenating variables to form directory names, and so on. I'd like to 
be able to use any python statement, including "x=raw_input('>')", but 
this currently brings everything to a halt.

One thing this app does is execute processes using wxPythons wx.Process 
object and wx.Execute function. wxPython makes it easy to redirect stdin 
and stdout. I'm trying to do a similar thing for individual python 
statements. I'm not sure that makes sense.

I hope that's clear.

Rick


David C Ullrich wrote:
> What you're trying to do and what's not working isn't 
> entirely clear to me.
>
> But if I had a wxPython application and I wanted to
> execute user input (note the _if_) I'd just pop up a window
> (I forget how "ShowModal" is spelled in wx right now)
> with a text box and an Execute button and a Cancel
> button - if the user hits the Execute button I'd
> attempt to execute what he'd typed in the box.
>
> There are reasons you want to be very careful about
> this...
>
> On Mon, 10 Aug 2009 13:44:17 -0400, Rick King wrote:
>
>   
>> I have a cmd.py-derived program (with a wxPython GUI) and want to
>> execute python statements for lines that are not my own special
>> commands.
>>
>> So basically it's either:
>>
>>     def do_somecommand(self,arg):
>>         ...
>>
>> or
>>
>>     def default(self,arg):
>>         exec arg in globals(),self.cmdlocals
>>
>> (where cmdlocals is a my local dictionary)
>>
>> in default() I'd like to be able to execute any python statement
>> including something like
>>
>>     "x = raw_input('>')"
>>
>> when I do this though it goes to the command window and so any user of
>> the program would be confused, which also means I have to have a command
>> window.
>>
>> If I add this:
>>
>>     self.stdin = self.edt_console_input   (where self.edt_console_input
>> is a wxPython text control)
>>
>> it just gets an EOF right away.
>>
>> Is there any way to do what I want to do? This might be better posted on
>> the wxpython list.
>>
>> Thanks for any help!
>>
>> Rick King
>> Southfield MI
>>     
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090810/114adefa/attachment.html>


More information about the Python-list mailing list