Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > "Visually following", line by line, the execution of a program

Reply
Thread Tools

"Visually following", line by line, the execution of a program

 
 
Andr? Roberge
Guest
Posts: n/a
 
      10-15-2004
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!
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      10-17-2004
Andr? Roberge <> wrote:

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


The functionality you require is known as 'tracing'.

See section 9.2, "How it works", in the Python Library Reference, for
all details. With sys.settrace, you establish your chosen 'tracing
function', and it gets called with three arguments, frame, event, arg.
event is a string which can be 'call', 'line','return', 'exception'.
The 'line' event is the main one you care about: from the frame object
you can find out what file and line is about to be executed. Be sure to
have the trace function return itself if it wants to keep tracing,
because for most events the trace function's return value is taken as
the new trace function to use (locally, i.e. within the function
currently being traced) for future tracing, None meaning stop tracing
(at this level of function call).


Alex
 
Reply With Quote
 
 
 
 
Alexander Schliep
Guest
Posts: n/a
 
      10-18-2004
(Alex Martelli) writes:
> Andr? Roberge <> wrote:
> > 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.

>
> The functionality you require is known as 'tracing'.
>
> See section 9.2, "How it works", in the Python Library Reference, for
> ...


You can find an implementation in our graph algorithm visualization
software Gato (http://gato.sf.net). It should be trivial to strip out
the necessary code.

Best,
Alexander


--
Alexander Schliep

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby has to be interpreted line by line in runtime, does this affectRuby's execution efficiency badly? Erwin Moller Ruby 7 05-08-2008 03:09 PM
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
How to read a text file line by line and remove some line kaushikshome C++ 4 09-10-2006 10:12 PM
Line by line execution of python code Justin Powell Python 2 07-14-2006 05:07 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57