newbie question: how to get the class instance given a module object?

T

Tian

I have a module called ModuleA.py, in which there is a class called
Dog, what should I put in the "????" part to get the instance of class
Dog???



import ModuleA

classname = "Dog"
module = globals()["ModuleA"]
classobj = ??????? <---using classname
instanct = classobj()
 
S

Steven Bethard

Tian said:
import ModuleA

classname = "Dog"
module = globals()["ModuleA"]
classobj = ??????? <---using classname
instanct = classobj()

classobj = getattr(module, classname)

STeVe
 
S

Steven Bethard

Su Wei said:
if i want to make a global class to save configuration information of
app that is read from a xml file,what can i do?

Just put it in a module. Say, 'progconfig.py'. Your configuration
code can then look like:

import progconfig
# parse configuration information from an XML file
for name, value in configuration_information:
setattr(progconfig, name, value)

Now all you have to do to use this information is to import the same
module in your other modules, e.g.:

import progconfig
# do whatever you normally do in this module, e.g.
if progconfig.sayhello:
print "say hello"

STeVe
 
S

Steven Bethard

Su Wei said:
if i have a xml file like this:
<ActionMappings>
<Action path="cpuInformation" type="CPUAction" next="CPUFrameGUI"/>
<Action path="cdromInformation" type="CDROMAction" next="CDROMFrameGUI"/>
</ActionMappings>

i want to save this information,and used by other moduls later.

how should i do it? ths

First you need an xml library. There's one built into Python, but
ElementTree is a simpler one: http://effbot.org/zone/element-index.htm

I've saved your text into a file named 'temp.xml':

py> print open('temp.xml').read()
<ActionMappings>
<Action path="cpuInformation" type="CPUAction" next="CPUFrameGUI"/>
<Action path="cdromInformation" type="CDROMAction" next="CDROMFrameGUI"/>
</ActionMappings>

Now let's parse that file:

py> from elementtree import ElementTree
py> actionmappings = ElementTree.parse('temp.xml').getroot()
py> for action in actionmappings:
.... print action.attrib
....
{'path': 'cpuInformation', 'type': 'CPUAction', 'next': 'CPUFrameGUI'}
{'path': 'cdromInformation', 'type': 'CDROMAction', 'next': 'CDROMFrameGUI'}

Note that I now have an object that I've named 'actionmappings' which
contains all the data I need. How do you want the information from
the XML file to be available? If you're happy navigating the XML
structure, you can just pass this object around. If you included the
code above in a module called, say, 'progconfig', then you could
access this info in another file like:

improt progconfig
# the following should give you 'cpuInformation'
progconfig.actionmappings[0].attrib['path']

If you don't like the format the XML file gives you, you'll need to
give us more information on how you'd like to reformat it.

STeVe

P.S. Please make sure you reply to the list. I probably won't be
able to answer again tonight, but someone else on the list may...
 
K

Kent Johnson

Steven said:
Tian said:
import ModuleA

classname = "Dog"
module = globals()["ModuleA"]
classobj = ??????? <---using classname
instanct = classobj()


classobj = getattr(module, classname)

There may not be any need for the indirect lookup of ModuleA, you can say
classobj = getattr(ModuleA, classname)

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top