473,386 Members | 2,078 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,386 software developers and data experts.

Bind compiled code to name?

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...

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jun 27 '08 #1
6 848
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
Jun 27 '08 #2
"Martin v. Löwis" <ma****@v.loewis.dewrote in
news:48*********************@news.freenet.de:
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...

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----. web: http://www.mosor.net || ICQ#: 10667163
| || _ | _ | Parce mihi domine quia Dalmata sum.
|__|_|__||_____|_____|
Jun 27 '08 #3
>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
Jun 27 '08 #4
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)
Jun 27 '08 #5
>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
Jun 27 '08 #6
>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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hermes | last post by:
Hi all, I'm using bind variables in sql in some asp pages, but something strange is happening with a few of the pages. I'm trying to add a bind variable to do an arrange and order of the output....
13
by: Russell Hind | last post by:
I'm trying to use boost::bind and boost::function inside managed code, but there appears to be some code generation problems. The following code compiles fine, but the function object throws an...
2
by: COHENMARVIN | last post by:
I'm leafing through a big book on asp.net, and I don't see any way to the following: 1. bind values and text to a dropdown 2. Also make the first line of the dropdown say something different. For...
0
by: teclioness | last post by:
Hi, I am using gridview for the user to update rows. In a row, there sre two columns, which need to be updated. When the gridview is to be shown, the row should show the values from database....
0
by: Mark Micallef | last post by:
Hi, I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm using: <InsertItemTemplate> <div class="insertArea">...
1
by: Mark Micallef | last post by:
Hi, this question relates to a control in the Ajax toolkit for asp.net 2. I'm having a problem using a databound dropdownlist inside a reorderlist ajax control. Here's a snippet of the code I'm...
1
by: David C | last post by:
I have a DataList that displays photos and is bound to a list of files. Is there a way to combine the value of a variable (strpath) with the # Bind(...) in the ImageURL field. Below is what I...
6
by: crack.the.hack | last post by:
Hi All, If I am changing the database machine, what should I do not to prep bind the sqc files everytime. Because I need to build my application everytime the database is changed? Is there...
3
by: eleland | last post by:
I am trying to bind to an LDAP server using php without success. I have a linux server running php compiled with open ldap. I am using the following code taken right from php.net: echo "<h3>LDAP...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.