473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Line by line execution of python code

Hi, I'm looking for suggestions on how to accomplish something in python. If
this is the wrong list for such things, I appologize and please disregard the
rest.

My application needs to allow users to create scripts which will be executed
in a statement-by-statement fashion. Here's a little pseudo-code:

def someUserProgram():
while 1:
print "I am a user program"

def mainProgram():
someProgram = someUserProgram
while 1:
print "Doing some stuff here"
executeOneStatement(someProgram)

def executeOneStatement(program):
# What goes in here?

I would expect the output to look like this:

Doing some stuff here
Doing some stuff here
I am a sub program
Doing some stuff here
Doing some stuff here
I am a sub program
etc.

It's possible to use generators to accomplish this, but unfortunately a
state-ment by statement executing would require that every other statement is
a yield. That would either force the users to put in a yield after every
statement, or require a mechanism that parses the source and inserts yield
statement before execution. Neither of these are really satisfactory

The other methods I've considered for doing this cleanly (subclassing the
debugger, or the interactive interpreter) seem to revolve around sys.settrace
and a callback trace function. I couldn't figure out a way to use this
mechanism to resume the main loop from the trace function, and then also
resume the user program the next time around, without the call stack just
spiraling out of control.

Anybody have a solution to this? I've done a bit of searching, looking
through python docs and sources and so forth, and I'm pretty stumped at this
point.

Thanks.

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Jul 13 '06 #1
2 1861
Try this... slightly more complex but get's the job done-added some
wait state in the user program/thread so it didn't kill the output
stream... Enjoy

import thread, sys, time

def someUserProgram(mutexRef):
while 1:
mutexRef.acquire()
print "I am a user program"
mutexRef.release()
time.sleep(1)

class main:
def __init__(self):
self.mutex = thread.allocate_lock()
self.someProgram = None

def mainProgram(self,someUserProgram):

self.someProgram = someUserProgram
self.executeOneStatement(self.someProgram)

inval = ''
while not inval == 'q':
inval = sys.stdin.readline().strip()
print "Doing some stuff here"
thread.exit()

def executeOneStatement(self,program):
thread.start_new(program, (self.mutex,))

l = main()

l.mainProgram(someUserProgram)
Justin Powell wrote:
Hi, I'm looking for suggestions on how to accomplish something in python. If
this is the wrong list for such things, I appologize and please disregard the
rest.

My application needs to allow users to create scripts which will be executed
in a statement-by-statement fashion. Here's a little pseudo-code:

def someUserProgram():
while 1:
print "I am a user program"

def mainProgram():
someProgram = someUserProgram
while 1:
print "Doing some stuff here"
executeOneStatement(someProgram)

def executeOneStatement(program):
# What goes in here?

I would expect the output to look like this:

Doing some stuff here
Doing some stuff here
I am a sub program
Doing some stuff here
Doing some stuff here
I am a sub program
etc.

It's possible to use generators to accomplish this, but unfortunately a
state-ment by statement executing would require that every other statement is
a yield. That would either force the users to put in a yield after every
statement, or require a mechanism that parses the source and inserts yield
statement before execution. Neither of these are really satisfactory

The other methods I've considered for doing this cleanly (subclassing the
debugger, or the interactive interpreter) seem to revolve around sys.settrace
and a callback trace function. I couldn't figure out a way to use this
mechanism to resume the main loop from the trace function, and then also
resume the user program the next time around, without the call stack just
spiraling out of control.

Anybody have a solution to this? I've done a bit of searching, looking
through python docs and sources and so forth, and I'm pretty stumped at this
point.

Thanks.

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Jul 13 '06 #2
Justin Powell wrote:
Hi, I'm looking for suggestions on how to accomplish something in python. If
this is the wrong list for such things, I appologize and please disregard the
rest.
No, this is totally the right place.

My application needs to allow users to create scripts which will be executed
in a statement-by-statement fashion. Here's a little pseudo-code:

def someUserProgram():
while 1:
print "I am a user program"

def mainProgram():
someProgram = someUserProgram
while 1:
print "Doing some stuff here"
executeOneStatement(someProgram)

def executeOneStatement(program):
# What goes in here?

I would expect the output to look like this:

Doing some stuff here
Doing some stuff here
I am a sub program
Doing some stuff here
Doing some stuff here
I am a sub program
etc.

It's possible to use generators to accomplish this, but unfortunately a
state-ment by statement executing would require that every other statement is
a yield. That would either force the users to put in a yield after every
statement, or require a mechanism that parses the source and inserts yield
statement before execution. Neither of these are really satisfactory.
Hmm, ok, so you don't want to manually interfere with the partial programs.
What I could think of is some form of code manipulation where you scan the
source code of the function you get passed and add a yield after each statement.

http://docs.python.org/dev/lib/inspect-source.html
http://docs.python.org/dev/lib/module-parser.html

The other methods I've considered for doing this cleanly (subclassing the
debugger, or the interactive interpreter) seem to revolve around sys.settrace
and a callback trace function. I couldn't figure out a way to use this
mechanism to resume the main loop from the trace function, and then also
resume the user program the next time around, without the call stack just
spiraling out of control.
I assume that tracing would be a good solution, though. Maybe someone else can
tell you how to use it better than I could.

You should tell us a bit more about the actual use case, i.e. *why* you need
this. Maybe there's a better solution to the whole problem after all.

Stefan
Jul 14 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: satish | last post by:
Hello all, I have a shared object executable viz. *cable* which I execute as follows : $ ansyscust71 -custom cable -p ANSYSRF **ansyscust71 is a shell script and is a part of a software...
6
by: Paolo Losi | last post by:
Hi all, I'm pretty new to the python language so please excuse me if this is FAQ... I'm very glad to be part of the list! :-) I'm looking into a way to implement a generic workflow framework...
30
by: Sean R. Lynch | last post by:
I've been playing around with Zope's RestrictedPython, and I think I'm on the way to making the modifications necessary to create a capabilities-based restricted execution system. The idea is to...
15
by: Riko Wichmann | last post by:
Dear all, is there a way in Python to comment out blocks of code without putting a # in front of each line? Somethings like C's /* block of code here is commented out */ Thanks,
6
by: cournape | last post by:
Hi there, I have some scientific application written in python. There is a good deal of list processing, but also some "simple" computation such as basic linear algebra involved. I would like to...
14
by: Xah Lee | last post by:
is there a way to condense the following loop into one line? # -*- coding: utf-8 -*- # python import re, os.path imgPaths= # change the image path to the full sized image, if it exists
8
by: Peter A. Schott | last post by:
Per subject - I realize I can copy/paste a line at a time into an interactive session when I'm trying to debug, but was wondering if there is any tool out there that allows me to copy sections of...
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
2
by: Jim Hendricks | last post by:
New to Python, and just had something strange happen. I've been running my new code in IDLE running in windows. My IDLE version shows as 1.2.1, Python version displaying in IDLE is 2.5.1. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.