Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Compiling Python code within a module

Reply
Thread Tools

Compiling Python code within a module

 
 
Mitko Haralanov
Guest
Posts: n/a
 
      05-18-2007
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
The "cutting edge" is getting rather dull.
-- Andy Purshottam
 
Reply With Quote
 
 
 
 
ici
Guest
Posts: n/a
 
      05-18-2007
On May 19, 12:52 am, Mitko Haralanov <(E-Mail Removed)> 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 (E-Mail Removed)
> 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

 
Reply With Quote
 
 
 
 
Mitko Haralanov
Guest
Posts: n/a
 
      05-18-2007
On 18 May 2007 15:51:40 -0700
ici <(E-Mail Removed)> 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 (E-Mail Removed)
Senior Software Engineer 650.934.8064
System Interconnect Group http://www.qlogic.com

==========================================
((lambda (foo) (bar foo)) (baz))
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-19-2007
En Fri, 18 May 2007 20:49:33 -0300, Mitko Haralanov <(E-Mail Removed)>
escribió:

> On 18 May 2007 15:51:40 -0700
> ici <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 09:47 PM
Cross-compiling error when compiling 2.6.1... Garrett Cooper Python 0 02-24-2009 08:55 PM
Compiling when libedit is in path Is there a trick to compiling Ruby when libedit must exist in the search path? Can you statically link to readline 5.0 in some manner? -- Lon Baker Lon Baker Ruby 1 03-21-2005 08:57 AM
Compiling java code from within Java John Morey Java 6 11-24-2004 10:33 AM
Compiling Java code using Ant within Eclipse junk@andy-coleman.co.uk Java 1 11-05-2004 10:25 AM



Advertisments