By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Compiling Python code within a module

P: n/a
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 mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
The "cutting edge" is getting rather dull.
-- Andy Purshottam
May 18 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ici
On May 19, 12:52 am, Mitko Haralanov <m...@qlogic.comwrote:
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 m...@qlogic.com
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

May 18 '07 #2

P: n/a
On 18 May 2007 15:51:40 -0700
ici <il******@gmail.comwrote:
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 mi***@qlogic.com
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
((lambda (foo) (bar foo)) (baz))
May 18 '07 #3

P: n/a
En Fri, 18 May 2007 20:49:33 -0300, Mitko Haralanov <mi***@qlogic.com>
escribió:
On 18 May 2007 15:51:40 -0700
ici <il******@gmail.comwrote:
>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

May 19 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.