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. Advertisements

  2. 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. Advertisements

  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. 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

    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. Advertisements

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 (here). After that, you can post your question and our members will help you out.