473,657 Members | 2,351 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 1288
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
4288
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
2074
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
2662
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
1305
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
1128
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
2939
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
1849
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
1038
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
8399
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
8312
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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...
0
7337
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
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
5632
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
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
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

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.