I have APIs [port.py] with the same interface for two different hardware platforms: mips and powerpc. By the same interface I mean that classes are the same, thier methods are the same, but implementation differs.
mips/port.py
powerpc/port.py
A program that uses these APIs should be decoupled from the specific platform and the code looks exactly the same except importing packages. How is it possible to dynamically select the platform, something like the code below, where platform
is a global setting (mips
or powerpc
) or whatever like this:
from [platform].port import PortCli
I tried importing with if
, but it looks nasty and doesn't scale well since I may need to add a different platform.
Thank you
Update after discussion with Eduard:
The code below is actually an analogue of import mips.port
:
import importlib
platform = 'mips'
myModule = importlib.import_module(platform + '.port')
myModule.Port().PrintMe()
It's fine, but I need to add myModule everywhere. Is it possible to have a similar substitute for from mips.port import Port
?