473,287 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Adapting code to multiple platforms

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

Jul 18 '05 #1
5 1193
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/d...ateMethod.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.
Jul 18 '05 #2
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 ;-)

Jul 18 '05 #3
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
Jul 18 '05 #4
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
Jul 18 '05 #5
Paul Watson wrote:
"Peter Hansen" <pe***@engcorp.com> 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!
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
253
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
5
by: grid | last post by:
Hi, I recently came across a piece of code like this, ( & ( ( ( some struct * ) 0 ) -> element ) ) ) I had never seen this kind of code being used in any implementations so far, but somehow...
6
by: Abubakar | last post by:
Hi, we are finding out ways in which we could develop libraries that could be written in c++ in a way that we can use them in windows, linux, and mac os. We want to write portable code, so that it...
239
by: Eigenvector | last post by:
My question is more generic, but it involves what I consider ANSI standard C and portability. I happen to be a system admin for multiple platforms and as such a lot of the applications that my...
4
NoPeasHear
by: NoPeasHear | last post by:
My problem - the first cell of my table is adapting the .nav class rather than .menu class that I am assigning it. How can I fix it? My code starts out as the following... <link...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
3
by: zaheer031 | last post by:
Hi All, How do I maximse the use of cache( both data and instruction ) in the code . I know use of loops and local variables will help but more would be of good help Thanks,
3
by: David Moss | last post by:
Hopefully a service like this already exists and I just haven't found it yet. If not it could be an idea for some kind soul(s) to pick up and run with ;-) As someone who writes and releases...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.