473,549 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 855
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.loewi s.dewrote in
news:48******** *************@n ews.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.ModuleTyp e('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
5102
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. When I run the query it does not do the arrange and order, but if I type it in how the bind sends it, it works. If I do a...
13
3569
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 exception when you call it with f() saying the function is empty. If you un-comment the #pragma unamanged, it runs fine. It appears to be a code...
2
2139
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 instance, if I'm working with a table of hotels, I might want a 'select' control as follows <select id="mylistbox" runat="server"> <option...
0
1702
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. When user clicks on edit link, should display the windows loginId in one column and in second column, show the current datetime. When user clicks on...
0
1662
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"> <asp:DropDownList ID="lstUser" runat="server" DataSourceID="UserDataSource" DataTextField="UserName" DataValueField="UserId" SelectedValue='<%# Bind("UserId") %>'...
1
2599
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 using: <InsertItemTemplate> <div class="insertArea"> <asp:DropDownList ID="lstUser" runat="server" DataSourceID="UserDataSource"...
1
2395
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 have now. <asp:ImageButton ID="ImgBtn1" runat="server" CausesValidation="false" CommandName="Select" AlternateText='<%# Bind("Name") %>'
6
5671
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 any way, to do prep bind with "defined timestamp" or something which does not require any prep - binding even when the
3
18699
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 query test</h3>"; echo "Connecting ..."; $ds=ldap_connect("ldap.berkeley.edu"); // must be a valid LDAP server! echo "connect result is...
0
7548
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7743
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6074
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5391
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5114
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3518
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.