[Tutor] "Visually following" Python program execution

André Roberge andre.roberge at ns.sympatico.ca
Sun Oct 17 03:59:52 CEST 2004


Kent:
Thanks for the suggestion; I tried this approach ... but it proved too 
"powerful" for what I had in mind, as it goes into program parts that I 
wanted to hide from the user.  However, I did find a solution to my 
problem.  I'll outline it here, in case someone has a better idea.  But 
first, I probably need to give the context.

What I want to do (and I am very much at the preliminary stage) is to 
write my own Python version of "Karel the Robot"  (or "Guido van Robot" 
as a Python adaptation is now known) so that it can have additional 
features.  [For those that don't know this GREAT project have a look at 
http://gvr.sourceforge.net/].

One of the "new" features I want, is for the user to be able to 
highlight the instruction (move, or putbeeper, etc.) in the program at 
the same time it is performed on the "world" GUI.

Rather than build my own parser (or stepper as it is known in GvR), I 
will put a "time.sleep()" instruction in the basic robot moves, and use 
the full power of Python interpretor.  This will allow to go beyond the 
five basic instructions, using variables and, possibly, sub-classes.

So, suppose the user defines a simple program (basic.py) as follows:
=== basic.py===
def turnright():
     turnleft()
     turnleft()
     turnleft()

move()
turnright()
=====
Then, my "new and improved solution" is to generate a second program 
(processed_basic.py) from the first which would read as follows:
==== processed_basic.py===
from Constants import *
from Instructions import *
NUM = 1
def turnright():
     NUM = 2
     turnleft()
     NUM = 3
     turnleft()
     NUM = 4
     turnleft()
     NUM = 5
NUM = 7
move()
NUM = 8
turnright()
==============
and use execfile("processed_basic.py") in my main program as I display
"basic.py" in a separate window and use the information from the 
variable NUM to know which line to highlight.

Right now, all I have are little bits and pieces but it looks promising. 
  However, I am more than open to suggestions for potential improvement!

Sorry for the amount of bandwidth!

André

Kent Johnson wrote:
> This is pretty much what a debugger does. In fact the IDLE debugger is 
> not far from this at all, can you just use it or modify it to do what 
> you want?
> 
> I suggest you look at how pdb and the IDLE debugger work. A few starting 
> points would be the pdb module docs at 
> http://docs.python.org/lib/module-pdb.html especially the "How it works" 
> section. Both pdb and IDLE use the bdb module as a base, you should 
> probably do the same.
> 
> This is a challenging project, good luck!
> Kent
> 
> At 02:37 PM 10/16/2004 -0300, André Roberge wrote:
> 
>> Note: I posted this on comp.lang.python last night from a friend's house.
>> Let's see if the tutor folks can beat the newsgroup readers who, so far,
>> have shown little interest in my problem.
>> André
>>
>> ===
>> I want to "import and execute" a python program (input_test.py below)
>> within another program (execute_test.py below) and "watch it" being
>> executed.
>> By watching it, I mean to display the file input_test.py in a window
>> (GUI) and
>> highlighting the line being executed.
>> I am *simulating* this here by printing the line being executed with
>> the corresponding line number and it works as expected for "simple"
>> programs.
>>
>> The problem arises when I want to do a loop (or other similar blocks).
>> If I write a loop as
>>
>> for i in range(2):
>>    print i
>>
>> exec() gives an EOF error, as it processes the "for" line.
>> I have tried to put the loop on a single physical line, something like
>> for i in range(2):\n    print i
>>
>> but this didn't work either.   I really would like to be able to
>> follow within the loops too...
>> Any pointer would be greatly appreciated.
>>
>> Andre Roberge
>>
>> Btw, input_test.py is already processed to tag on the ";NUM = ...". It 
>> is not the file as I would want it to appear on a window being
>> traced.
>>
>> ===== File Constants.py ====
>> NUM = 1
>>
>> ===== File input_test.py ===
>> from Constants import * ; NUM = 2
>> print "Starting within input_test"; NUM = 3
>> print "Within input_test, NUM =", NUM; NUM = 4
>> print "Done!"
>>
>> ===== File execute_test.py ===
>> import time
>> from Constants import *
>> inp = open("input_test.py", "r")
>>
>> for line in inp.readlines():
>>    print "NUM =", NUM, ":",
>>    exec(line)
>>    time.sleep(1)
>>
>> inp.close()
>>
>> ======= Output from the program =====
>> NUM = 1 : NUM = 2 : Starting within input_test
>> NUM = 3 : Within input_test, NUM = 3
>> NUM = 4 : Done!
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
> 
> 
> 


More information about the Tutor mailing list