0

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?

Konstantin
  • 2,937
  • 10
  • 41
  • 58

1 Answers1

1

Based on post of Python Core Developer Andrey Svetlov.

I can suggest you to add platform/hardware specific Python modules in separate directories and append this modules to sys.path in runtime.

For example: We have a package

  • package
    • __init__.py
    • a.py
    • x86_64
      • b.py
    • i386
      • b.py

__init__.py will contain something like this:

import sys
import platform
from os.path import join, dirname
__path__.append(join(dirname(__file__), platform.machine()))

With this approach common code is located in package while platform specific distributed in subdirectories.

Take attention that x86_64 and i368 do not have __init__.py module.

Import required module b

from package import b
emcpow2
  • 852
  • 6
  • 19
  • Hello Eduard, thanks for the reply. I'm actually building software that tests functionality of various platforms so I execute it on x86 comp. Is it possible to supply what platform I test as a variable or whatever to `__init__.py`. I think it's about changing of `platform.machine()` for some global variable or something like this – Konstantin Mar 11 '14 at 10:11
  • You can pass it through environment variable or read it from configuration files. – emcpow2 Mar 11 '14 at 10:15
  • I work on x86 computer and I run remote tests (using telnet, com) on `mips` and `powerpc` platforms. I want to pass `mips/powerpc` as a parameter to the test software. It has nothing to do with `platform.machine()` as you may see – Konstantin Mar 11 '14 at 10:18
  • In this case my answer do not fits your needs. Just pass platform as a command line argument and make conditional import. For example platform = sys.argv[1] if platform == 'mips': import mips.port as port elif platform == 'powerpc': import powerpc.port as port port.run_tests() WBR – emcpow2 Mar 11 '14 at 10:29
  • As I wrote in my question, I tried the approach with `if/elif`. It works, but it's not good, since if I add a third platform, I'll have to change a LOT of files in my program. – Konstantin Mar 11 '14 at 10:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/49467/discussion-between-eduard-iskandarov-and-konstantin) – emcpow2 Mar 11 '14 at 10:38