473,785 Members | 2,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_subclas s(super):
def func(self):
'''windows-specific function defined here'''

And then in main I have:

inst = eval('%s_subcla ss' % sys.platform)(a rgs)

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 1218
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.platfo rm()
, 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.platfo rm()
, 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(fi lename)

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
4285
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. Unfortunately these ad hominem rhetorts are frequently introduced into purely technical discussions on the feasibility of supporting such functionality in C++. That usually serves to divert the discussion from the technical subject to a discussion of the...
253
2785
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
5
1567
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 remembered to have seen it sometime in clc, and that it was for finding the offset of a member in a structure ( if I am not horribly mistaken). Now does this work ? Does this actually use the address 0x0, and when we
6
1380
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 could be build on mac os, linux, and windows. The code would involve lots of multi-threading and network/socket programming. Since I have read that vc++ 2k5 supports a lot of standard c++ stuff, can it help me to acheive what I want or I will...
239
10325
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 users request are a part of the OpenSource community. Many if not most of those applications strongly require the presence of the GNU compiling suite to work properly. My assumption is that this is due to the author/s creating the applications...
4
2006
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 href="../basicstyle.css" rel="stylesheet" type="text/css"> <style type="text/css"> <!-- .style1 {color: #CCCC99} --> </style>
232
13349
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 set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
3
1872
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
1139
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 Python modules for the community, I find it difficult to have a decent level of confidence in the efficacy of my code on platforms and Python versions other than those that I own or use regularly. My documentation states that I support Python 2.3 or...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.