[Tutor] Importing and creation on the fly

Tino Dai tinoloc at gmail.com
Thu Jun 28 15:24:20 CEST 2007


On 6/26/07, Tino Dai <tinoloc at gmail.com> wrote:
>
> On 6/26/07, Dave Kuhlman <dkuhlman at rexx.com> wrote:
>
> > On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:
> > > Hi there,
> > >
> > >     I've been banging my head on this for about two weeks, and I can't
> > > figure out a solution to this. I'm wondering if you could assist me on
> > this
> > > pesky problem.
> > >
> > >     I'm reading in an xml file that has the name of class, location,
> > and
> > > the filename into a dictionary. I want to import these classes and
> > create
> > > instances of them.  The code in question is as follows:
> > >
> > > 36       for xmlKey in self.dictXML.keys():
> > > 37             if not self.dictXML[xmlKey]['location'] in sys.path and
> > \
> > > 38             not self.dictXML[xmlKey]['location'] == os.getcwd():
> > > 39                 sys.path.append(self.dictXML[xmlKey]['location'])
> > > 40             try:
> > > 41                 if os.stat(self.dictXML[xmlKey]['location'] + \
> > > 42                 self.dictXML[xmlKey]['filename']):
> > > 43                     eval('import ' + self.dictXML[xmlKey]["class"])
> > > <-- syntax error here
> > > 44                     actionStmt=self.dictXML [xmlKey]["class"] + '.'
> > +
> > > self.dictXML[xmlKey]["class"] + '()' 45
> > > 45                          self.objList.append(eval(actionStmt))
> > > 46             except:
> > > 47                 pass
> > >
> > >
> > > I have also tried: __import__(self.dictXML[xmlKey]["class"]), which
> > gave me
> > > an error when I did the eval(actionStmt). Could anybody shed some
> > light on
> > > this? Thanks in advance.
> >
> > For the task of importing, look at the "imp" module:
> >
> >     http://docs.python.org/lib/module-imp.html
> >
> > Also, the "inspect" module may be of help:
> >
> >     http://docs.python.org/lib/module-inspect.html
> >
> > In particular, look at the the inspect.getmembers() and
> > inspect.isclass() methods.
> >
> > Dave
>
>
>  Thanks guys. I will look into that this afternoon
>
> -Tino
>
> Hi Everybody,

      This is the solution that I came up with. Using Dave's advice:

      fp, pathname, description = imp.find_module(self.dictXML
[xmlKey]["class"])
      tarMod = imp.load_module('apacheLimiter',fp,pathname,description)
      tarClass = getattr(tarMod,self.dictXML[xmlKey]["class"])
      tarObj=tarClass()
      if hasattr(tarObj,'do'):
            self.objList.append(tarObj)

       So, I used the find_module to find the particular module that I
wanted, returning the filename, path to the module, and the description (see
the docs.python.org for more info). Next, I loaded the module from the info
provided to me. After loading the module, I "extracted" the class that I
wanted using the getattr function. After I had the class loaded, I created
an instance from it.

     I also checked out the exec function Kent. And one of my colleagues
recommended execfile. I felt like that was "hack" to get around a problem
(IHMO). Thanks for everything.

-Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070628/86cdb57e/attachment.html 


More information about the Tutor mailing list