Dynamic/runtime code introspection/compilation

Discussion in 'Python' started by Thomas W, Nov 28, 2006.

  1. Thomas W

    Thomas W Guest

    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?
     
    Thomas W, Nov 28, 2006
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Nov 28, 2006
    #2
    1. Advertising

  3. Thomas W

    Leo Kislov Guest

    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
     
    Leo Kislov, Nov 28, 2006
    #3
  4. Thomas W

    Thomas W Guest

    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
     
    Thomas W, Nov 28, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    276
    Bruno Desthuilliers
    Aug 27, 2006
  2. Steven T. Hatton

    Synopsis: A Source-code Introspection Tool

    Steven T. Hatton, Jul 6, 2005, in forum: C++
    Replies:
    2
    Views:
    509
    Howard
    Jul 6, 2005
  3. C__chp
    Replies:
    4
    Views:
    510
    Puppet_Sock
    Feb 15, 2008
  4. Alex J
    Replies:
    7
    Views:
    811
    Alex J
    Sep 12, 2011
  5. Daniel Cremer

    code introspection

    Daniel Cremer, Jul 26, 2004, in forum: Ruby
    Replies:
    10
    Views:
    213
    Joel VanderWerf
    Jul 29, 2004
Loading...

Share This Page