[Tutor] Help with python parsing please..

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sat, 20 Jul 2002 20:49:40 -0700 (PDT)


On Sat, 20 Jul 2002, SA wrote:

> The following portion of my script is redirecting to the shell instead of
> Python:
>
> def expyth(self):
>         self.t2.delete(0.0, END)
>         self.output = commands.getoutput(self.t1.get(0.0, END))
>         self.t2.insert(END, self.output)

Hmmm... expyth() looks ok to me.  So 't2' is the "script output" window,
while t2 is the "script input" window?  You may want to double check your
commands.getoutput() function.


> Basically this part of my script takes the text I type in one window and
> redirects the python output to another window. So I can write some
> script in window one, this takes that text and parses the commands
> through Python and displays the output in window 2.  Those of you that
> helped me a long time ago with my IDLE program may remember this code.
> Needless to say, I've made some sytem changes since the last time this
> script worked properly.

Hmmm... is it possible to have the source code available somewhere?  This
sounds like a subtle bug that will need to be hunted down.  Post a link,
and one of us here will be happy to look at the code and see what's going
on.

It might also be a great opportunity to play with the 'unittest' module!

    http://www.python.org/doc/current/lib/module-unittest.html

I've been reading Martin Fowler's "Refactoring", and his evangalizing of
unit tests is pretty persuasive to me.  *grin* It might make a great
tutorial demonstration to show how to write unit tests for your program.


> If there is some import command in the first window I get a zsh error if
> the there is a from command (ie. From Tkinter import *) I get an error
> about /var/mail/Tkinter.(No such thing on my system)
>
> However if I write something simple like print "hello world", I get the
> correct output in window two. I can't figure what the deal is. Any ideas?

Ah, but is that output coming from zsh, or from Python?  The statement:

    print "hello world"

is legitimate Python, but it is also perfectly legitimate Perl!  zsh, too
might consider it a good command, though I have never played with zsh.

We should look at commands.getoutput() to see how it works.


Best of wishes to you!