Bind compiled code to name?

K

Karlo Lozovina

If string `source_code` contains Python source code, how can I execute
that code, and bind it to some name? I tried compiling with:

code_object = compile(source_code, 'errorfile', 'exec')

but then what to do with `code_object`?

P.S.
If my intentions aren't that clear, this is what I'm trying to do. I want
to emulate this:

import some_module as some_name

but with my code in `some_module` string, and not in file.


Thanks...
 
M

Martin v. Löwis

If string `source_code` contains Python source code, how can I execute
that code, and bind it to some name? I tried compiling with:

code_object = compile(source_code, 'errorfile', 'exec')

but then what to do with `code_object`?

P.S.
If my intentions aren't that clear, this is what I'm trying to do. I want
to emulate this:

import some_module as some_name

but with my code in `some_module` string, and not in file.

d = {}
exec source_code in d
some_name = d['some_name']

HTH,
Martin
 
K

Karlo Lozovina

d = {}
exec source_code in d
some_name = d['some_name']

This works quite well! I can't believe after googling for half on hour I
didn't notice this "exec ... in ..." syntax.
One more thing though, is there a way to access "some_name" as a
attribute, instead as a dictionary:

some_name = d.some_name

?

Thanks...
 
M

Martin v. Löwis

d = {}
exec source_code in d
some_name = d['some_name']

This works quite well! I can't believe after googling for half on hour I
didn't notice this "exec ... in ..." syntax.
One more thing though, is there a way to access "some_name" as a
attribute, instead as a dictionary:

some_name = d.some_name

Sure:

class D:pass
d = D()
exec source_code in d.__dict__
print d.some_name

Notice that this will also give you d.__builtins__, which you might
want to del afterwards.

Regards,
Martin
 
G

Giuseppe Ottaviano

class D:pass
d = D()
exec source_code in d.__dict__
print d.some_name

Notice that this will also give you d.__builtins__, which you might
want to del afterwards.


If you want to mimic an import you can also do this:

import types
D = types.ModuleType('D')
exec source_code in D.__dict__
print D.some_name

This way D is a module (don't know if there are real differences with
the class approach, though)
 
M

Martin v. Löwis

d = {}
exec source_code in d
some_name = d['some_name']

This works quite well! I can't believe after googling for half on hour I
didn't notice this "exec ... in ..." syntax.
One more thing though, is there a way to access "some_name" as a
attribute, instead as a dictionary:

some_name = d.some_name

Sure:

class D:pass
d = D()
exec source_code in d.__dict__
print d.some_name

Notice that this will also give you d.__builtins__, which you might
want to del afterwards.

Regards,
Martin
 
M

Martin v. Löwis

d = {}
exec source_code in d
some_name = d['some_name']

This works quite well! I can't believe after googling for half on hour I
didn't notice this "exec ... in ..." syntax.
One more thing though, is there a way to access "some_name" as a
attribute, instead as a dictionary:

some_name = d.some_name

Sure:

class D:pass
d = D()
exec source_code in d.__dict__
print d.some_name

Notice that this will also give you d.__builtins__, which you might
want to del afterwards.

Regards,
Martin
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top