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