473,413 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Compiling Python code within a module

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
3 1575
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: satish | last post by:
Hello all, I have a shared object executable viz. *cable* which I execute as follows : $ ansyscust71 -custom cable -p ANSYSRF **ansyscust71 is a shell script and is a part of a software...
1
by: A. B., Khalid | last post by:
Hello all. After a search on Google it would seem that the users of Mingw have not had good results in compiling the python sources natively. See at least: ...
6
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
0
by: phoolimin | last post by:
Dear all, I am trying to embed python into another scripting language, to do this I need to solve a number of problems on importing or compiling python script. First let me state what exactly I...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
113
by: John Nagle | last post by:
The major complaint I have about Python is that the packages which connect it to other software components all seem to have serious problems. As long as you don't need to talk to anything outside...
5
by: Adam Atlas | last post by:
Does anyone know if it would be possible to create a CPython extension -- or use the ctypes module -- to access Python's own embedding API (http://docs.python.org/api/initialization.html &c.)?...
3
by: Marcin Kalicinski | last post by:
How do I use multiple Python interpreters within the same process? I know there's a function Py_NewInterpreter. However, how do I use functions like Py_RunString etc. with it? They don't take any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.