On 6/26/07, <b class="gmail_sendername">Dave Kuhlman</b> <<a href="mailto:dkuhlman@rexx.com">dkuhlman@rexx.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;">
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">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">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><div><br> Thanks guys. I will look into that this afternoon<br><br>-Tino<br><br></div></div>