473,695 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to create/ref globals in an alternate namespace?

I have two seperate modules doing factory stuff which each have the
similar function2:

In the ds101 module,
def DS101CLASS(mnam e,data):
cname = mname+'DS101'
msg_class = globals()[cname]
msg = msg_class(data)
return msg

and in the fdu module,

def FDUCLASS(mname, data):
cname = mname+'FDU'
msg_class = globals()[cname]
msg = msg_class(data)
return msg

I was thinking I'd be clever and create a common function:
def procCLASS(mname , objname, data):
cname = mname+objname
msg_class = globals()[cname]
msg = msg_class(data)
return msg

but the call to globals fouls it all up. Is there a way to write it so
that the call to globals can be parameterized to be in the context of a
specific module?

Also, I need to go the other way, a la,
globals()[name] = nclass

Is this doable?

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Apr 27 '07 #1
5 1289
Steven W. Orr wrote:
I have two seperate modules doing factory stuff which each have the
similar function2:

In the ds101 module, def DS101CLASS(mnam e,data):
cname = mname+'DS101'
msg_class = globals()[cname]
msg = msg_class(data)
return msg

and in the fdu module,

def FDUCLASS(mname, data):
cname = mname+'FDU'
msg_class = globals()[cname]
msg = msg_class(data)
return msg

I was thinking I'd be clever and create a common function:
def procCLASS(mname , objname, data):
cname = mname+objname
msg_class = globals()[cname]
msg = msg_class(data)
return msg

but the call to globals fouls it all up. Is there a way to write it so
that the call to globals can be parameterized to be in the context of a
specific module?

Also, I need to go the other way, a la,
globals()[name] = nclass

Is this doable?

TIA
Why do you need all of the msg_class(es) global? Why not put them into a
module and import the module where you need them? This would be the
conventional way to avoid such problems.
Apr 27 '07 #2
On Friday, Apr 27th 2007 at 14:07 -0700, quoth James Stroud:

=>Steven W. Orr wrote:
=>I have two seperate modules doing factory stuff which each have the
=>similar function2:
=>>
=>In the ds101 module, def DS101CLASS(mnam e,data):
=> cname = mname+'DS101'
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>and in the fdu module,
=>>
=>def FDUCLASS(mname, data):
=> cname = mname+'FDU'
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>I was thinking I'd be clever and create a common function:
=>def procCLASS(mname , objname, data):
=> cname = mname+objname
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>but the call to globals fouls it all up. Is there a way to write it so
=>that the call to globals can be parameterized to be in the context of a
=>specific module?
=>>
=>Also, I need to go the other way, a la,
=> globals()[name] = nclass
=>>
=>Is this doable?
=>>
=>TIA
=>>
=>
=>Why do you need all of the msg_class(es) global? Why not put them into a
=>module and import the module where you need them? This would be the
=>conventional way to avoid such problems.

The idea is that DS101 is being called in a loop in the ds101 module to
create a lot of msg_classes. The same is true for the FDUCLASS function;
it creates a lot of classes in a loop.

In addition, I have two other functions, almost alike, in two seperate
modules (mdefs is a structure with all of the stuff needed to drive the
loops)

def __InitDS101Clas ses():
for m in mdefs:
mdef = mdefs[m]
name = mdefs[m]['name']+'DS101'
nclass = new.classobj(na me,(DS101,),{})
nclass.mdef = mdef
nclass.mid = m
globals()[name] = nclass
def __InitFDUClasse s():
for m in mdefs:
mdef = mdefs[m]
name = mdefs[m]['name']+'FDU'
nclass = new.classobj(na me,(FDU,),{})
nclass.mdef = mdef
nclass.mid = m
globals()[name] = nclass

I'm trying to see if by being clever, I can factor out the common code of
the four different functions and still end up with what they create ending
up in the namespaces where they are intended to reside in. Does this make
sense or am I way off base?
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
Apr 27 '07 #3
On Apr 27, 2:33 pm, "Steven W. Orr" <ste...@syslang .netwrote:
On Friday, Apr 27th 2007 at 14:07 -0700, quoth James Stroud:
<snip>
>
I'm trying to see if by being clever, I can factor out the common code of
the four different functions and still end up with what they create ending
up in the namespaces where they are intended to reside in. Does this make
sense or am I way off base?
<snip>

You may be trying to get too clever.

If what you're trying to do is what I think you're trying to do, I
would suggest looking at how the logging module handles globals and
functions for an example.

Apr 27 '07 #4
Steven W. Orr wrote:
On Friday, Apr 27th 2007 at 14:07 -0700, quoth James Stroud:

=>Steven W. Orr wrote:
=>I have two seperate modules doing factory stuff which each have the
=>similar function2:
=>>
=>In the ds101 module, def DS101CLASS(mnam e,data):
=> cname = mname+'DS101'
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>and in the fdu module,
=>>
=>def FDUCLASS(mname, data):
=> cname = mname+'FDU'
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>I was thinking I'd be clever and create a common function:
=>def procCLASS(mname , objname, data):
=> cname = mname+objname
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>but the call to globals fouls it all up. Is there a way to write it so
=>that the call to globals can be parameterized to be in the context of a
=>specific module?
=>>
=>Also, I need to go the other way, a la,
=> globals()[name] = nclass
=>>
=>Is this doable?
=>>
=>TIA
=>>
=>
=>Why do you need all of the msg_class(es) global? Why not put them into a
=>module and import the module where you need them? This would be the
=>conventional way to avoid such problems.

The idea is that DS101 is being called in a loop in the ds101 module to
create a lot of msg_classes. The same is true for the FDUCLASS function;
it creates a lot of classes in a loop.

In addition, I have two other functions, almost alike, in two seperate
modules (mdefs is a structure with all of the stuff needed to drive the
loops)

def __InitDS101Clas ses():
for m in mdefs:
mdef = mdefs[m]
name = mdefs[m]['name']+'DS101'
nclass = new.classobj(na me,(DS101,),{})
nclass.mdef = mdef
nclass.mid = m
globals()[name] = nclass
def __InitFDUClasse s():
for m in mdefs:
mdef = mdefs[m]
name = mdefs[m]['name']+'FDU'
nclass = new.classobj(na me,(FDU,),{})
nclass.mdef = mdef
nclass.mid = m
globals()[name] = nclass

I'm trying to see if by being clever, I can factor out the common code of
the four different functions and still end up with what they create ending
up in the namespaces where they are intended to reside in. Does this make
sense or am I way off base?

If I get your intention, the imported module idea still works:
# imported.py

pass
# module1.py

import imported

imported.abc = 2
# module2.py

import module1
import imported

print imported.abc
James
Apr 27 '07 #5
Steven W. Orr wrote:
On Friday, Apr 27th 2007 at 14:07 -0700, quoth James Stroud:

=>Steven W. Orr wrote:
=>I have two seperate modules doing factory stuff which each have the
=>similar function2:
=>>
=>In the ds101 module, def DS101CLASS(mnam e,data):
=> cname = mname+'DS101'
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>and in the fdu module,
=>>
=>def FDUCLASS(mname, data):
=> cname = mname+'FDU'
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>I was thinking I'd be clever and create a common function:
=>def procCLASS(mname , objname, data):
=> cname = mname+objname
=> msg_class = globals()[cname]
=> msg = msg_class(data)
=> return msg
=>>
=>but the call to globals fouls it all up. Is there a way to write it so
=>that the call to globals can be parameterized to be in the context of a
=>specific module?
=>>
=>Also, I need to go the other way, a la,
=> globals()[name] = nclass
=>>
=>Is this doable?
=>>
=>TIA
=>>
=>
=>Why do you need all of the msg_class(es) global? Why not put them into a
=>module and import the module where you need them? This would be the
=>conventional way to avoid such problems.

The idea is that DS101 is being called in a loop in the ds101 module to
create a lot of msg_classes. The same is true for the FDUCLASS function;
it creates a lot of classes in a loop.

In addition, I have two other functions, almost alike, in two seperate
modules (mdefs is a structure with all of the stuff needed to drive the
loops)

def __InitDS101Clas ses():
for m in mdefs:
mdef = mdefs[m]
name = mdefs[m]['name']+'DS101'
nclass = new.classobj(na me,(DS101,),{})
nclass.mdef = mdef
nclass.mid = m
globals()[name] = nclass
def __InitFDUClasse s():
for m in mdefs:
mdef = mdefs[m]
name = mdefs[m]['name']+'FDU'
nclass = new.classobj(na me,(FDU,),{})
nclass.mdef = mdef
nclass.mid = m
globals()[name] = nclass

I'm trying to see if by being clever, I can factor out the common code of
the four different functions and still end up with what they create ending
up in the namespaces where they are intended to reside in. Does this make
sense or am I way off base?

Even if you *can* factor out the common code there's no reason why that
shouldn't go in a fifth module that the other four import ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
------------------ Asciimercial ---------------------
Get Python in your .sig and on the web. Blog and lens
holdenweb.blogs pot.com squidoo.com/pythonology
tag items: del.icio.us/steve.holden/python
All these services currently offer free registration!
-------------- Thank You for Reading ----------------

Apr 28 '07 #6

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

Similar topics

1
4303
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would be better to promote better programming than rely on PHP to compensate for lazy programming?
3
2076
by: Robert Dodier | last post by:
Hello, I'm interested in introducing new variables into the environment of a Python interpreter or program. In reading through old posts to this newsgroup, I see there is an often-repeating warning against modifying the contents of locals(). Fair enough. However, is there anything wrong with modifying globals() ?
2
1640
by: r holland | last post by:
The only way I know of to create a series of object/variable names in a loop is to form strings and then use exec (or eval in some cases). Is there a better way?
45
2667
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
6
1309
by: marek | last post by:
Hello All, we are doing a quite a big project that contains at the lowest level an unmenaged c++ classes. Above it there are managed wrappers and at the top there are ASP.NET pages. Can anyone tell me how to force ASP.NET not to "clear" or reinitialize unmenaged global or static variables that are set on unmanaged level between successive requests?
0
1129
by: Geert Jansen | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm experiencing a weird problem with eval(), providing it a copy of the globals() dictionary. The following code works:
18
2941
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
1
1851
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals() printed\n"
0
1041
by: Gabriel Genellina | last post by:
En Sat, 12 Jul 2008 16:15:36 -0300, Akathorn Greyhat <akathorn@gmail.com> escribi�: Welcome! You have to pass in the namespace of the desired module - instead of globals. I'd use an explicit argument:
0
8623
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8977
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8839
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6488
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5837
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4339
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2997
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1971
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.