<div class="gmail_quote">On Mon, Nov 30, 2009 at 12:25 PM, Carsten Haese <span dir="ltr"><<a href="mailto:carsten.haese@gmail.com">carsten.haese@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">Victor Subervi wrote:<br>
> Taking out the parenthesis did it! Thanks. Now, you state this is an<br>
> option of last resort. Although this does indeed achieve my desired aim,<br>
> here is a complete example of what I am trying to achieve. The following<br>
> is from 'createTables2.py':<br>
><br>
>   for table in tables:<br>
>     try:<br>
>       exec 'from options import %s' % table<br>
>     except:<br>
>       pass<br>
>     try:<br>
>       exec '%s()' % table<br>
>     except:<br>
>       pass<br>
<br>
</div>Here's a rough sketch of rewriting that snippet in a way that uses<br>
attribute lookup instead of dynamic code execution:<br>
<br>
#---------------------------------------#<br>
import options<br>
for table in tables:<br>
    tablefunc = getattr(options, table)<br>
    tablefunc()<br>
#---------------------------------------#<br>
<br>
The main ideas behind this approach are:<br>
<br>
1) Rather than importing the functions from the options module piecemeal<br>
into the local namespace, I just import the entire options module as its<br>
own separate namespace.<br>
<br>
2) I use getattr on that separate namespace to look up the desired<br>
function object from the options module. I assign the local name<br>
<<tablefunc>> to the resulting function object.<br>
<br>
3) I call that function using the local name <<tablefunc>>.<br>
<br></blockquote><div><br>Thanks. This is good. Thanks Jean-Michelle for the suggestion on better naming. Thanks Marco for the lxml builder, but I'm happy with just plain old html.<br>V<br></div></div>