Re: Dynamic/runtime code introspection/compilation
- From: "Leo Kislov" <Leo.Kislov@xxxxxxxxx>
- Date: 28 Nov 2006 05:31:44 -0800
Thomas W wrote:
Maybe a stupid subject, but this is what I want to do :
I got some python code stored in a string:
somecode = """
from somemodule import ISomeInterface
class Foo(ISomeInterface):
param1 = ...
param2 = ....
"""
and I want to compile that code so that I can use the Foo-class and
check what class it extends, in this case ISomeInterface etc. I've
tried eval, codeop etc. but it doesn't work. Something like this would
be nice :
from somemodule import ISomeInteface
d = compile(sourcecode)
myfoo = d.Foo()
print ISomeInterface in myfoo.__bases__
Any hints?
Here is hello world program for plugins:
import sys
somecode = """
class Foo:
param1 = "Hello, world!"
"""
plugin = type(sys)('unknown_plugin') # Create new empty module
exec somecode in plugin.__dict__
print plugin.Foo.param1
-- Leo
.
- Follow-Ups:
- Re: Dynamic/runtime code introspection/compilation
- From: Thomas W
- Re: Dynamic/runtime code introspection/compilation
- References:
- Dynamic/runtime code introspection/compilation
- From: Thomas W
- Dynamic/runtime code introspection/compilation
- Prev by Date: Re: Dynamic/runtime code introspection/compilation
- Next by Date: Re: Libgmail
- Previous by thread: Re: Dynamic/runtime code introspection/compilation
- Next by thread: Re: Dynamic/runtime code introspection/compilation
- Index(es):
Relevant Pages
|