[Tutor] How does this work?

Kent Johnson kent37 at tds.net
Wed Feb 7 19:39:47 CET 2007


Tony Cappellini wrote:
>> 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 should be able to make the logging module work with you, it is very 
flexible. You should hook into the logging module. Write a custom 
handler that pushes log methods into your GUI. Add the handler to the 
root logger.

>> 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.

Right, but the stand-alone stuff can be wrapped with
if __name__=='__main__':
leaving the part you care about available for import in the same module. 
The logging configuration should be in the main code as well, then when 
you import the module you can configure logging the way you want.

Kent



More information about the Tutor mailing list