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

Discussion in 'Python' started by Andr? Roberge, Oct 15, 2004.

  1. 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!
    Andr? Roberge, Oct 15, 2004
    #1
    1. Advertising

  2. 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
    Alex Martelli, Oct 17, 2004
    #2
    1. Advertising

  3. (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
    Alexander Schliep, Oct 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. priya
    Replies:
    0
    Views:
    1,216
    priya
    Oct 3, 2005
  2. Pat Deegan
    Replies:
    3
    Views:
    465
    Joe Smith
    Apr 22, 2004
  3. varala_kanth

    execution of perl program

    varala_kanth, Jun 26, 2004, in forum: Perl
    Replies:
    1
    Views:
    4,165
    J├╝rgen Exner
    Jun 26, 2004
  4. Justin Powell

    Line by line execution of python code

    Justin Powell, Jul 13, 2006, in forum: Python
    Replies:
    2
    Views:
    440
    Stefan Behnel
    Jul 14, 2006
  5. Erwin Moller
    Replies:
    7
    Views:
    118
    Phillip Gawlowski
    May 8, 2008
Loading...

Share This Page