On 6/26/07, <b class="gmail_sendername">Tino Dai</b> <<a href="mailto:tinoloc@gmail.com">tinoloc@gmail.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><span class="e" id="q_11369419ff16dad5_0">On 6/26/07, <b class="gmail_sendername">Dave Kuhlman</b> <<a href="mailto:dkuhlman@rexx.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">dkuhlman@rexx.com
</a>> wrote:</span></div><div><div><span class="e" id="q_11369419ff16dad5_2"><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On Tue, Jun 26, 2007 at 12:20:18PM -0400, Tino Dai wrote:<br>> Hi there,<br>><br>> I've been banging my head on this for about two weeks, and I can't<br>> figure out a solution to this. I'm wondering if you could assist me on this
<br>> pesky problem.<br>><br>> I'm reading in an xml file that has the name of class, location, and<br>> the filename into a dictionary. I want to import these classes and create<br>> instances of them. The code in question is as follows:
<br>><br>> 36 for xmlKey in self.dictXML.keys():<br>> 37 if not self.dictXML[xmlKey]['location'] in sys.path and \<br>> 38 not self.dictXML[xmlKey]['location'] ==
os.getcwd():<br>> 39 sys.path.append(self.dictXML[xmlKey]['location'])<br>> 40 try:<br>> 41 if os.stat(self.dictXML[xmlKey]['location'] + \<br>> 42
self.dictXML[xmlKey]['filename']):<br>> 43 eval('import ' + self.dictXML[xmlKey]["class"])<br>> <-- syntax error here<br>> 44 actionStmt=self.dictXML
[xmlKey]["class"] + '.' +<br>> self.dictXML[xmlKey]["class"] + '()' 45<br>> 45 self.objList.append(eval(actionStmt))<br>> 46 except:<br>> 47 pass
<br>><br>><br>> I have also tried: __import__(self.dictXML[xmlKey]["class"]), which gave me<br>> an error when I did the eval(actionStmt). Could anybody shed some light on<br>> this? Thanks in advance.
<br><br>For the task of importing, look at the "imp" module:<br><br> <a href="http://docs.python.org/lib/module-imp.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://docs.python.org/lib/module-imp.html
</a><br><br>Also, the "inspect" module may be of help:
<br><br> <a href="http://docs.python.org/lib/module-inspect.html" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://docs.python.org/lib/module-inspect.html</a><br><br>In particular, look at the the
inspect.getmembers() and<br>inspect.isclass() methods.<br><br>Dave
</blockquote></span></div><div><br> Thanks guys. I will look into that this afternoon<br><br>-Tino<br><br></div></div>
</blockquote></div>Hi Everybody,<br><br> This is the solution that I came up with. Using Dave's advice:<br><br> fp, pathname, description = imp.find_module(self.dictXML[xmlKey]["class"])<br> tarMod =
imp.load_module('apacheLimiter',fp,pathname,description)<br> tarClass = getattr(tarMod,self.dictXML[xmlKey]["class"])<br> tarObj=tarClass()<br> if hasattr(tarObj,'do'):<br>
self.objList.append(tarObj)<br><br> 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 <a href="http://docs.python.org">docs.python.org
</a> 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.
<br><br> 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.<br><br>-Tino<br><br><br>