[Tutor] How does this work?

Luke Paireepinart rabidpoobear at gmail.com
Wed Feb 7 19:06:11 CET 2007

Tony Cappellini wrote:
>> If I understand you, you have a python cmdline app that does something
>> useful. You have users who aren't comfortable with the command line, so
>> you are writing a GUI wrapper that calls the cmdline app with popen().
> That is correct
>> A better approach is to turn the functional part of the cmdline app -
>> the code that does the real work - into an importable module.
> it already is importable
>>> Then your GUI app can import and use this module directly, instead
> of doing hacks
>> with popen() and stdout.
> This all worked fine, until the author of the cmd line app stopped
> using stdout and started using the logging module. Now I cannot
> capture any output from his module.
>> You don't even need a separate module for the cmdline app if it is
>> written correctly - the command-line-specific part can be in an main()
>> function that is only called if the module is run as main.
> not totally true. It must be a stand-alone app, because there are
> people using it now and many don't want to use the gui.
> It is maintained completely independent of the gui by people other
> than myself.. Any changes in the cmdline app should not affect the
> gui. That was the intent by the author anyway. The use of the logging
> module happens to be an exception to this.
The application needs to be rewritten if this is true.  The author's 
implementation is not logical,
if I'm inferring correctly that he knows there are other apps depending 
on this.
consider this:

class Foo(object):
    def __init__(self):
        print "we're creating a new Foo and doing something useful here."

if __name__ == "__main__":
    f = Foo()
    #we can do stuff with our Foo instance here.

Notice that if this program is run as a regular script -- not imported 
-- we create a Foo and we can use it for whatever.
All the functionality of the cmdline app is contained in the Foo class.  
All that we have to do is propagate our data into it.

Now if we want to write a GUI for this, we just import this script, and 
use the Foo object to do whatever we were doing in the
cmdline version -- no separate processes, threads, or any of that nastiness.
Because it's imported, the contents of the 'if __name__...' conditional 
statement are never executed, so the cmdline functionality is disabled.

Hope that helps,
> Using popen() in a thread was a nice clean way to capture it's output.
> Perhaps the exec/eval isn't so clean it's just what first came to mind.
> I've already switched the exec/eval code to the import style.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list