Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > newbie question: how to get the class instance given a module object?

Reply
Thread Tools

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

 
 
Tian
Guest
Posts: n/a
 
      03-28-2005
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()

 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      03-28-2005
Tian wrote:
> import ModuleA
>
> classname = "Dog"
> module = globals()["ModuleA"]
> classobj = ??????? <---using classname
> instanct = classobj()


classobj = getattr(module, classname)

STeVe
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      03-28-2005
Su Wei <(E-Mail Removed)> wrote:
> 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
--
You can wordify anything if you just verb it.
--- Bucky Katt, Get Fuzzy
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      03-28-2005
Su Wei <(E-Mail Removed)> wrote:
> 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...
--
You can wordify anything if you just verb it.
--- Bucky Katt, Get Fuzzy
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      03-28-2005
Steven Bethard wrote:
> Tian wrote:
>
>> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
How to find at Runtime, if Created class object is instance of given class declaration Ami C++ 3 02-27-2006 04:59 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments