import and package confusion

Jeroen Ruigrok van der Werven asmodai at in-nomine.org
Thu Apr 30 02:32:31 EDT 2009


-On [20090430 02:21], Dale Amon (amon at vnl.com) wrote:
>import sys
>sys.path.extend (['../lib', '../bin'])
>
>from VLMLegacy.CardReader import CardReader
>rdr = CardReader ("../example/B767.dat","PRINTABLE")
>
>iotypes     = ["WINGTL","VLMPC","VLM4997"]
>for iotype in iotypes:
>        packagename = "VLMLegacy." + iotype + ".Conditions"
>        classname   =  iotype + "_Conditions"
>        code        = "from %s import Conditions as %s" \
>			% (packagename, classname)
>        x           = compile (code,"foo","exec")
>        exec x
>        cls = globals()[classname]
>        a = cls(rdr,2)
>        a.test()
>

Although I can applaud your creativity, this really is very hackish for
Python and definitely has a bad 'smell'.

Like you I work with many different languages, but I am not going to force
the natural flow of Python into something resembling Java or whatever
other language.

Simply doing:

Right now your code boils down to:

from VLMLegacy.VLM4997.Conditions import Conditions as VLM4997_Conditions
from VLMLegacy.VLMPC.Conditions import Conditions as VLMPC_Conditions
from VLMLegacy.WINGTL.Conditions import Conditions as WINGTL_Conditions

And while you are, of course, allowed to do so, it's not the way you would
want to approach it in Python.

For each subpackage/module you could add an import and __all__ to
__init__.py to expose Conditions and then shorten it all to:

import VLMLegacy.VLM4997 as VLM4997
import VLMLegacy.VLMPC as VLMPC
import VLMLegacy.WINGTL as WINGTL

So that you can do:

a = VLM4997.Conditions(rdr, 2)
a.test()

-- 
Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org> / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
May you get - not what you deserve - but your heart's desire...



More information about the Python-list mailing list