Adapting code to multiple platforms

Discussion in 'Python' started by Jeffrey Barish, Mar 12, 2005.

  1. I have a small program that I would like to run on multiple platforms
    (at least linux and windows). My program calls helper programs that
    are different depending on the platform. I think I figured out a way
    to structure my program, but I'm wondering whether my solution is good
    Python programming practice.

    Most of my program lives in a class. My plan is to have a superclass
    that performs the generic functions and subclasses to define methods
    specific to each platform. I envision something like this:

    class super:
    '''All the generic stuff goes here'''

    class linux_subclass(super):
    def func(self):
    '''linux-specific function defined here'''

    class windows_subclass(super):
    def func(self):
    '''windows-specific function defined here'''

    And then in main I have:

    inst = eval('%s_subclass' % sys.platform)(args)

    to create an instance of the appropriate subclass.

    I realize that I will have to name the subclasses appropriately
    depending on what values get assigned to sys.platform. (On my platform,
    sys.platform is 'linux2', so I would actually need class
    linux2_subclass(super). I don't know what value gets assigned on a
    windows platform.)

    The other way I thought of -- surrounding all the platform-specific code
    with if statements to test sys.platform -- seems clearly worse. I'm
    just getting up to speed on Python and OOP, so I'm wondering whether I
    have missed something obvious. I'm hoping that the strongest rebuke
    would be that I found something obvious.
    --
    Jeffrey Barish
     
    Jeffrey Barish, Mar 12, 2005
    #1
    1. Advertising

  2. On Fri, 11 Mar 2005 17:27:06 -0700, Jeffrey Barish wrote:
    > Most of my program lives in a class. My plan is to have a superclass
    > that performs the generic functions and subclasses to define methods
    > specific to each platform....
    > I'm just getting up to speed on Python and OOP, so I'm wondering whether I
    > have missed something obvious. I'm hoping that the strongest rebuke
    > would be that I found something obvious.


    You may be interested to know this is called the Template Pattern.

    http://home.earthlink.net/~huston2/dp/templateMethod.html

    Generally, while most of the patterns are obvious in hindsight they are
    not necessarily obvious in advance, and I consider independently
    re-discovering a pattern is a good sign; it's much easier to correct not
    knowing about them than gaining the skills to independently re-derive them.
     
    Jeremy Bowers, Mar 12, 2005
    #2
    1. Advertising

  3. Jeffrey Barish

    Simon John Guest

    If you're using a GUI, then that may help you decode the platform too -
    for example wxPython has wx.Platform, there's also platform.platform()
    , sys.platform and os.name

    You could try import win32api and checking for an exception ;-)
     
    Simon John, Mar 12, 2005
    #3
  4. Jeffrey Barish

    Peter Hansen Guest

    Simon John wrote:
    > If you're using a GUI, then that may help you decode the platform too -
    > for example wxPython has wx.Platform, there's also platform.platform()
    > , sys.platform and os.name
    >
    > You could try import win32api and checking for an exception ;-)


    (Winky noted) Just in case anyone thinks that last is a
    useful idea, keep in mind that win32api is not installed
    (with the standard distribution) but must be installed
    with a separate download of the pywin32 package by Mark
    Hammond.

    -Peter
     
    Peter Hansen, Mar 12, 2005
    #4
  5. Jeffrey Barish wrote:

    > I have a small program that I would like to run on multiple platforms
    > (at least linux and windows). My program calls helper programs that
    > are different depending on the platform. I think I figured out a way
    > to structure my program, but I'm wondering whether my solution is good
    > Python programming practice.
    >


    I use something like this in the setup code:

    if os.name == 'posix':
    statfunction = os.lstat
    else:
    statfunction = os.stat

    and then further in the code:

    x = statfunction(filename)

    So the idea is to have your "own" function names and assign the
    os-specific functions one and for all in the beginning. Afterwards, your
    code only uses your own function names and, as long as they behave in
    the same way, there's no more if - else stuff.

    -pu
     
    Patrick Useldinger, Mar 12, 2005
    #5
  6. Jeffrey Barish

    Paul Watson Guest

    "Peter Hansen" <> wrote in message
    news:...
    > Simon John wrote:
    >> If you're using a GUI, then that may help you decode the platform too -
    >> for example wxPython has wx.Platform, there's also platform.platform()
    >> , sys.platform and os.name
    >>
    >> You could try import win32api and checking for an exception ;-)

    >
    > (Winky noted) Just in case anyone thinks that last is a
    > useful idea, keep in mind that win32api is not installed
    > (with the standard distribution) but must be installed
    > with a separate download of the pywin32 package by Mark
    > Hammond.
    >
    > -Peter


    How about try: import msvcrt
     
    Paul Watson, Mar 13, 2005
    #6
  7. Jeffrey Barish

    Peter Hansen Guest

    Paul Watson wrote:
    > "Peter Hansen" <> wrote:
    >>Simon John wrote:
    >>>You could try import win32api and checking for an exception ;-)

    >>
    >>(Winky noted) Just in case anyone thinks that last is a
    >>useful idea, keep in mind that win32api is not installed
    >>(with the standard distribution) but must be installed
    >>with a separate download of the pywin32 package by Mark
    >>Hammond.

    >
    > How about try: import msvcrt


    Sure, that works, but it would make for pretty
    inscrutable code if you were doing this just to
    check whether you were on Windows, and not because
    you really intended to use the msvcrt module.

    sys.platform exists for a good reason. Use it!
     
    Peter Hansen, Mar 14, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffrey
    Replies:
    2
    Views:
    6,759
    Jeffrey
    Oct 16, 2003
  2. David Moss
    Replies:
    3
    Views:
    313
  3. jacob navia

    Adapting software to multiple usage patterns

    jacob navia, Oct 13, 2009, in forum: C Programming
    Replies:
    90
    Views:
    1,673
    Richard Bos
    Oct 25, 2009
  4. Stuart Hungerford
    Replies:
    0
    Views:
    118
    Stuart Hungerford
    Mar 14, 2006
  5. ross

    adapting code

    ross, Jan 24, 2006, in forum: Javascript
    Replies:
    4
    Views:
    92
    Thomas 'PointedEars' Lahn
    Jan 25, 2006
Loading...

Share This Page