from spam import eggs, spam at runtime, how?

Peter Hansen peter at engcorp.com
Tue Dec 9 16:18:24 EST 2003


Rene Pijlman wrote:
> The modules are Cheetah templates (http://www.cheetahtemplate.org/) in
> different skins of a website. I've made one package per skin, and my
> website generator basically does:
> 
>    from skin import template
> 
> where skin is only known at runtime (it's passed as a parameter or hidden
> field to my mod_python application).
> 
> The alternative would be:
> 
>    if skin == 'basic':
>        from basic import homepage
>    elif skin == 'modern':
>        from modern import homepage
> 
> ... but this is unmaintainable and unpythonic.

Unmaintainable as-is, perhaps, but pythonicism has nothing to do with this.

What is probably cleaner *and* more maintainable is to have an extra level
of indirection, with some kind of configuration file that in effect maps 
between the skin names and the Python package which implements it.  I'd
personally do this with a quick bit of XML, but I've been drinking too
much Koolaid(tm) so ignore me.

The problem I see with the above approach is that you are tying some 
strings that are used in your HTML pages directly to the names of Python
modules.  Unless you are doing this in an entirely automated fashion
(e.g. generating the list of available skins via a directory scan) then
you are increasing the coupling in your system inappropriately, and 
*that* is certain to be the least maintainable approach of all, IMHO.

-Peter




More information about the Python-list mailing list