A
Andr? Roberge
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!
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!