473,396 Members | 1,827 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,396 software developers and data experts.

Question about using from/import from with a class

I have a class that has this code in it:

def __init__(self,Site):
self.Site = Site
import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
exec( import_cmd )
print AGENT # fails, NameError: global name 'AGENT' is not defined
The regexpr_1.py file, for example, has the line:
AGENT="Mozilla"

After the exec() command is ran, how to I access varaibles defined in
the regexpr_%d file?

Thanks,
John
Jul 18 '05 #1
4 1516
On 27 Jul 2004 10:32:41 -0700, John Taylor <jo**************@yahoo.com> wrote:
I have a class that has this code in it:

def __init__(self,Site):
self.Site = Site
import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
exec( import_cmd )
print AGENT # fails, NameError: global name 'AGENT' is not defined

The regexpr_1.py file, for example, has the line:
AGENT="Mozilla"

After the exec() command is ran, how to I access varaibles defined in
the regexpr_%d file?


don't do it this way. Try this instead:

regexpr = __import__('regexpr_%d' % int(self.Site.id))
print regexpr.AGENT

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #2
On 27 Jul 2004 10:32:41 -0700, John Taylor <jo**************@yahoo.com> wrote:
I have a class that has this code in it:

def __init__(self,Site):
self.Site = Site
import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
exec( import_cmd )
print AGENT # fails, NameError: global name 'AGENT' is not defined

The regexpr_1.py file, for example, has the line:
AGENT="Mozilla"

After the exec() command is ran, how to I access varaibles defined in
the regexpr_%d file?


don't do it this way. Try this instead:

regexpr = __import__('regexpr_%d' % int(self.Site.id))
print regexpr.AGENT

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #3
On 27 Jul 2004 10:32:41 -0700, John Taylor <jo**************@yahoo.com> wrote:
I have a class that has this code in it:

def __init__(self,Site):
self.Site = Site
import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
exec( import_cmd )
print AGENT # fails, NameError: global name 'AGENT' is not defined

The regexpr_1.py file, for example, has the line:
AGENT="Mozilla"

After the exec() command is ran, how to I access varaibles defined in
the regexpr_%d file?


don't do it this way. Try this instead:

regexpr = __import__('regexpr_%d' % int(self.Site.id))
print regexpr.AGENT

--
John Lenton (jl*****@gmail.com) -- Random fortune:
bash: fortune: command not found
Jul 18 '05 #4
John Taylor wrote:
I have a class that has this code in it:

def __init__(self,Site):
self.Site = Site
import_cmd = "from regexpr_%d import *" % ( int(self.Site.id) )
exec( import_cmd )
print AGENT # fails, NameError: global name 'AGENT' is not defined
The regexpr_1.py file, for example, has the line:
AGENT="Mozilla"


It may be bad style but it _does_ work here. I would verify that

[faked:]
import regexpr_1
regexpr_1.AGENT

'Mozilla'

in the interpreter doesn't also raise the NameError. If so, check
int(self.Site.id).

Peter
Jul 18 '05 #5

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

Similar topics

16
by: dario | last post by:
Hi, Im new on phyton programming. On my GPRS modem with embedded Phyton 1.5.2+ version, I have to receive a string from serial port and after send this one enclosed in an e-mail. All OK if the...
55
by: Steve Jorgensen | last post by:
In a recent thread, RKC (correctly, I believe), took issue with my use of multiple parameters in a Property Let procedure to pass dimensional arguments on the basis that, although it works, it's...
3
by: David Hirschfield | last post by:
An xmlrpc client/server app I'm writing used to be super-simple, but now threading has gotten into the mix. On the server side, threads are used to process requests from a queue as they come in....
10
by: Gerard Flanagan | last post by:
Hello I have the following code: #### builder.py ######### class HtmlBuilder(object): @staticmethod def page(title=''): return HtmlPage(title)
11
by: Brian Blazer | last post by:
OK, I have a very simple class here: class Student: """Defines the student class""" def __init__(self, lName, fName, mi): self.lName = lName self.fName = fName self.mi = mi
6
by: DarkBlue | last post by:
My application makes several connections to a remote database server via tcp/ip. Usually all is fine,but occasionally the server is down or the internet does not work and then there is the 30 sec...
0
by: Eric_Dexter | last post by:
I know this might be the wrong place to ask but I recently modified the ogl example from wxpython and it works fine in spe but when I call it from other programs it wierds out because of the run.py...
3
by: Steven W. Orr | last post by:
This is all an intro learning experience for me, so please feel free to explain why what I'm trying to do is not a good idea. In the Cookbook, they have a recipe for how to create global...
17
by: Suresh Pillai | last post by:
I am performing simulations on networks (graphs). I have a question on speed of execution (assuming very ample memory for now). I simplify the details of my simulation below, as the question I...
4
by: harijay | last post by:
Hi I am new to writing module and object oriented python code. I am trying to understand namespaces and classes in python. I have the following test case given in three files runner , master and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
0
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,...

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.