Running python apps from within python apps

A

aph

Hello. I'm sure this has been asked before, but I can't find an answer
anywhere.

I want to create a truly "dynamic" app which can get new functions
"on-the-fly" and run them without having to re-start the main app.

I've found the code module that looks kind of hopefull. For instance
this works great:

import code

class myApp:

def __init__(self):
self.ii = code.InteractiveInterpreter()

def kalle(self,str):
return str.upper()

def run_script(self,script):
self.ii.runsource(script)

app = myApp()
app.run_script("print 'hello'")

Now I want this new script to interact with the existing program, and
it doesn't work. I cant for instance do:

app.run_script("print self.kalle('hello')")

Any tips of how to make this work? Is it possible?
 
C

Claudio Grondi

aph said:
Hello. I'm sure this has been asked before, but I can't find an answer
anywhere.

I want to create a truly "dynamic" app which can get new functions
"on-the-fly" and run them without having to re-start the main app.

I've found the code module that looks kind of hopefull. For instance
this works great:

import code

class myApp:

def __init__(self):
self.ii = code.InteractiveInterpreter()

def kalle(self,str):
return str.upper()

def run_script(self,script):
self.ii.runsource(script)

app = myApp()
app.run_script("print 'hello'")

Now I want this new script to interact with the existing program, and
it doesn't work. I cant for instance do:

app.run_script("print self.kalle('hello')")

Any tips of how to make this work? Is it possible?
I have no idea what do you want to achieve and have no experience with
PyPy myself, but I mean, that what you want to achieve should be
possible using it :

http://codespeak.net/pypy/dist/pypy/doc/getting-started.html

Claudio
 
A

aph

actually 'exec()' is the function I was looking for. Working code:

class myApp:

def kalle(self,str):
return str.upper()

def run_script(self,script):
exec(script)

app = myApp()
app.run_script("print self.kalle('hello')")

Thanks...
 
C

Claudio Grondi

aph said:
actually 'exec()' is the function I was looking for. Working code:

class myApp:

def kalle(self,str):
return str.upper()

def run_script(self,script):
exec(script)

app = myApp()
app.run_script("print self.kalle('hello')")

Thanks...
Sorry, I see, I should read your posting more carefully.

PyPy allows to run one Python interpreter in another Python interpreter
and it should even be possible to switch from one to another on the fly
- it is a bit more, than I can currently understand myself - it is a way
beyond what exec() does just running any source code passed to it in the
scope of the current script (and not the interpreter/application as such).

Claudio
P.S. my email directly to you bounced, because your mailbox reports to
be full.
 
P

Peter Hansen

aph said:
actually 'exec()' is the function I was looking for. Working code:

class myApp:

def kalle(self,str):
return str.upper()

def run_script(self,script):
exec(script)

app = myApp()
app.run_script("print self.kalle('hello')")

A very minor point, but perhaps in some circumstances it could make a
difference: exec is a statement, not a function. No need to pass the
argument(s) in parentheses, and of course there's no return value.

-Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top