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

Discussion in 'Python' started by Tian, Mar 28, 2005.

  1. Tian

    Tian Guest

    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()
    Tian, Mar 28, 2005
    #1
    1. Advertising

  2. Re: newbie question: how to get the class instance given a moduleobject?

    Tian wrote:
    > import ModuleA
    >
    > classname = "Dog"
    > module = globals()["ModuleA"]
    > classobj = ??????? <---using classname
    > instanct = classobj()


    classobj = getattr(module, classname)

    STeVe
    Steven Bethard, Mar 28, 2005
    #2
    1. Advertising

  3. Re: save configuration information in a global class

    Su Wei <> 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
    Steven Bethard, Mar 28, 2005
    #3
  4. Re: save configuration information in a global class

    Su Wei <> 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
    Steven Bethard, Mar 28, 2005
    #4
  5. Tian

    Kent Johnson Guest

    Re: newbie question: how to get the class instance given a moduleobject?

    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
    Kent Johnson, Mar 28, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lord0
    Replies:
    1
    Views:
    563
    Thomas Weidenfeller
    Apr 19, 2006
  2. chiara
    Replies:
    6
    Views:
    467
    Barry Schwarz
    Oct 6, 2005
  3. Ami
    Replies:
    3
    Views:
    419
    Tomás
    Feb 27, 2006
  4. 2Barter.net
    Replies:
    0
    Views:
    363
    2Barter.net
    Dec 13, 2006
  5. Daniel Lipovetsky
    Replies:
    2
    Views:
    345
    Jordan Greenberg
    Mar 12, 2007
Loading...

Share This Page