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

Dynamic/runtime code introspection/compilation

P: n/a
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?

Nov 28 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Thomas W wrote:
from somemodule import ISomeInteface

d = compile(sourcecode)

myfoo = d.Foo()

print ISomeInterface in myfoo.__bases__

Any hints?
Python is a dynamic language, so compiling something won't tell you much
about what the code actually does. the only reliable way to do that is
to execute the code:

code = compile(sourcecode)
namespace = {}
exec code in namespace
print issubclass(namespace["Foo"])

</F>

Nov 28 '06 #2

P: n/a

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

Nov 28 '06 #3

P: n/a
Great !!! That works like charm.

Thanks alot.

Thomas
Leo Kislov wrote:
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
Nov 28 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.