Compiling Python code within a module

Discussion in 'Python' started by Mitko Haralanov, May 18, 2007.

  1. For various reason, what I need to do is be able to send some Python
    code (mostly entire functions in the form of a string) to a remote
    server (written in Python), have that server compile the code and
    insert it in the local namespace so it is available to be called at a
    later time.

    I have gotten the sending and receiving part already written and that
    works. However, I can't get the compiling part! I have looked at the
    compile module and while it is able to compile the code I am not
    entirely sure what to do with the returned code object so it get's
    inserted as a local function.

    I would appreciate any help that you guys might be able to offer?

    Thanks

    --
    Mitko Haralanov
    Senior Software Engineer 650.934.8064
    System Interconnect Group http://www.qlogic.com

    ==========================================
    The "cutting edge" is getting rather dull.
    -- Andy Purshottam
     
    Mitko Haralanov, May 18, 2007
    #1
    1. Advertising

  2. Mitko Haralanov

    ici Guest

    On May 19, 12:52 am, Mitko Haralanov <> wrote:
    > For various reason, what I need to do is be able to send some Python
    > code (mostly entire functions in the form of a string) to a remote
    > server (written in Python), have that server compile the code and
    > insert it in the local namespace so it is available to be called at a
    > later time.
    >
    > I have gotten the sending and receiving part already written and that
    > works. However, I can't get the compiling part! I have looked at the
    > compile module and while it is able to compile the code I am not
    > entirely sure what to do with the returned code object so it get's
    > inserted as a local function.
    >
    > I would appreciate any help that you guys might be able to offer?
    >
    > Thanks
    >
    > --
    > Mitko Haralanov
    > Senior Software Engineer 650.934.8064
    > System Interconnect Group http://www.qlogic.com
    >
    > ==========================================
    > The "cutting edge" is getting rather dull.
    > -- Andy Purshottam


    exec it :)

    --- Example---
    exec(compile("""
    def test():
    import os
    for i in os.listdir('.'):
    print i
    """,'<string>', 'exec'))

    test()
    --- End example---
    Now you have test() function available in namespace where executed
    example

    Po-zdravi
     
    ici, May 18, 2007
    #2
    1. Advertising

  3. On 18 May 2007 15:51:40 -0700
    ici <> wrote:

    > exec it :)


    Thank you! That works when I compile/exec it in the main body of the
    program. However, when I try to do that in a separate module it
    doesn't. For example:

    Module Foo:
    import compiler

    class Foo:
    def __init__ (self):
    return
    def register (self, text):
    exec (compiler.compile (text, "<string>",
    "exec"))

    File Bar:
    import Foo

    f = Foo.Foo ()
    f.register ("def blah():\n\tprint 'blah'\n")

    In this case, the function 'blah' is nowhere to be found. I've tried
    looking in 'f', in the class 'Foo', in the module 'Foo' and it's
    nowhere.

    --
    Mitko Haralanov
    Senior Software Engineer 650.934.8064
    System Interconnect Group http://www.qlogic.com

    ==========================================
    ((lambda (foo) (bar foo)) (baz))
     
    Mitko Haralanov, May 19, 2007
    #3
  4. En Fri, 18 May 2007 20:49:33 -0300, Mitko Haralanov <>
    escribió:

    > On 18 May 2007 15:51:40 -0700
    > ici <> wrote:
    >
    >> exec it :)

    >
    > Thank you! That works when I compile/exec it in the main body of the
    > program. However, when I try to do that in a separate module it
    > doesn't. For example:


    exec has a long form - see http://docs.python.org/ref/exec.html
    And forget the compile pass - let Python handle it automatically. Also
    note that exec is a statement, not a function, so you don't need ()

    To "inject" inside a module a function defined in source_code, do:

    exec source_code in module.__dict__

    To "inject" a function inside a class, it's easier if you use an
    intermediate dictionary:

    d = globals().copy()
    exec source_code in d
    my_class.method = d['method']

    > def register (self, text):
    > exec (compiler.compile (text, "<string>",
    > "exec"))
    >
    > f.register ("def blah():\n\tprint 'blah'\n")
    >
    > In this case, the function 'blah' is nowhere to be found. I've tried
    > looking in 'f', in the class 'Foo', in the module 'Foo' and it's
    > nowhere.


    It existed briefly in the local namespace of the register method - after
    register is exited, it's gone.

    --
    Gabriel Genellina
     
    Gabriel Genellina, May 19, 2007
    #4
    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. Replies:
    1
    Views:
    3,890
    Thomas Åhlén
    Nov 5, 2004
  2. John Morey
    Replies:
    6
    Views:
    447
    Chris Uppal
    Nov 24, 2004
  3. Garrett Cooper
    Replies:
    0
    Views:
    558
    Garrett Cooper
    Feb 24, 2009
  4. Garrett Cooper
    Replies:
    0
    Views:
    500
    Garrett Cooper
    Feb 24, 2009
  5. Lon Baker
    Replies:
    1
    Views:
    224
    Csaba Henk
    Mar 21, 2005
Loading...

Share This Page