Running python apps from within python apps

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aph

    #1

    Running python apps from within python apps

    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.Interactiv eInterpreter()

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

    def run_script(self ,script):
    self.ii.runsour ce(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('hel lo')")

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

  • Claudio Grondi

    #2
    Re: Running python apps from within python apps

    aph wrote:[color=blue]
    > 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.Interactiv eInterpreter()
    >
    > def kalle(self,str) :
    > return str.upper()
    >
    > def run_script(self ,script):
    > self.ii.runsour ce(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('hel lo')")
    >
    > Any tips of how to make this work? Is it possible?
    >[/color]
    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 :



    Claudio

    Comment

    • aph

      #3
      Re: Running python apps from within python apps

      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('hel lo')")

      Thanks...

      Comment

      • Claudio Grondi

        #4
        Re: Running python apps from within python apps

        aph wrote:[color=blue]
        > 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('hel lo')")
        >
        > Thanks...
        >[/color]
        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.

        Comment

        • Peter Hansen

          #5
          Re: Running python apps from within python apps

          aph wrote:[color=blue]
          > 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('hel lo')")[/color]

          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

          Comment

          Working...