Running python apps from within python apps

Discussion in 'Python' started by aph, Jan 14, 2006.

  1. aph

    aph Guest

    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?
     
    aph, Jan 14, 2006
    #1
    1. Advertising

  2. aph wrote:
    > 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
     
    Claudio Grondi, Jan 14, 2006
    #2
    1. Advertising

  3. aph

    aph Guest

    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...
     
    aph, Jan 14, 2006
    #3
  4. aph wrote:
    > 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.
     
    Claudio Grondi, Jan 14, 2006
    #4
  5. aph

    Peter Hansen Guest

    aph wrote:
    > 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
     
    Peter Hansen, Jan 14, 2006
    #5
    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. Göktuğ Kayaalp
    Replies:
    0
    Views:
    117
    Göktuğ Kayaalp
    Oct 31, 2013
  2. Tim Chase
    Replies:
    0
    Views:
    90
    Tim Chase
    Nov 1, 2013
  3. Göktuğ Kayaalp
    Replies:
    0
    Views:
    127
    Göktuğ Kayaalp
    Nov 1, 2013
  4. Wolfgang Maier
    Replies:
    0
    Views:
    103
    Wolfgang Maier
    Nov 1, 2013
  5. Göktuğ Kayaalp
    Replies:
    0
    Views:
    110
    Göktuğ Kayaalp
    Nov 2, 2013
Loading...

Share This Page