Execute external code and get return value

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

    #1

    Execute external code and get return value

    Hi,
    I want to execute an external code, that become from a text file (pe),
    call a function inside it and get its return value:

    # ext_code.txt

    def funct2Call():
    return True

    # test.py

    sourcecode = open("ex_code.t xt").read()
    comp_code = compile(sourcec ode, "My_test", "exec")

    #do something like this
    return_value = comp_code.funct 2Call()

    But I can't...

    I find this:
    http://tinyurl.com/nwbpk

    but here, it call from the external code a function inside my code. I
    want the opposite!
    Is there a solution for do this?

    Thanks,
    Michele
  • Peter Otten

    #2
    Re: Execute external code and get return value

    Michele Petrazzo wrote:
    I want to execute an external code, that become from a text file (pe),
    call a function inside it and get its return value:
    namespace = {}
    execfile("ext_c ode.txt", namespace)
    print namespace["funct2Call "]()

    Peter

    Comment

    • Michele Petrazzo

      #3
      Re: Execute external code and get return value

      Michele Petrazzo wrote:

      Auto reply:
      I find this: http://tinyurl.com/nwbpk
      >
      Following this link, here is my solution:


      code = """
      def funct2Call():
      return "It work!"

      object.method(f unct2Call)
      """

      class Object(object):
      def method(self, value):
      self._callable = value
      def __call__(self):
      return self._callable( )

      # populate the namespace
      namespace = {
      "object": Object()
      }

      # execute the code
      exec code in namespace

      namespace["object"]()

      Michele

      Comment

      • Michele Petrazzo

        #4
        Re: Execute external code and get return value

        Peter Otten wrote:
        Michele Petrazzo wrote:
        >
        >I want to execute an external code, that become from a text file
        >(pe), call a function inside it and get its return value:
        >
        namespace = {} execfile("ext_c ode.txt", namespace) print
        namespace["funct2Call "]()
        >
        Sorry, I forgot to say that "ext_code.t xt" isn't a real file, but a text
        become from a db, so or I create a temp file where I save the code and
        pass its path to execfile, or use my post.
        Peter
        Thanks,
        Michele

        Comment

        • Peter Otten

          #5
          Re: Execute external code and get return value

          Michele Petrazzo wrote:
          Following this link, here is my solution:
          >
          >
          code = """
          def funct2Call():
          return "It work!"
          >
          object.method(f unct2Call)
          """
          >
          class Object(object):
          def method(self, value):
          self._callable = value
          def __call__(self):
          return self._callable( )
          >
          # populate the namespace
          namespace = {
          "object": Object()
          }
          >
          # execute the code
          exec code in namespace
          >
          namespace["object"]()
          Just in case you didn't see it -- the following will work as well:

          code = """
          def funct2Call():
          return "It work!"
          """

          namespace = {}
          exec code in namespace
          namespace["funct2Call "]()

          Peter

          Comment

          Working...