[Tutor] Looking for suggestions for improving chessTimer.py code
bhaaluu at gmail.com
Fri Oct 26 14:00:11 CEST 2007
On 10/26/07, Dick Moores <rdm at rcblue.com> wrote:
> At 09:33 AM 10/25/2007, Dick Moores wrote:
> >Please give me constructive criticism of
> Hmm. Nothing. One problem may be that only people using Windows can
> run the script.
The first thing I saw when I looked at the code was that it was
for MS-Windows only, so I didn't waste anymore time with it.
Recently, I ran across some code that tried MS-Windows, except
if the user wasn't running MS-Windows, it assumed they were
running some *nix variant, and loaded those modules instead.
That certainly makes code more platform independent.
This is what it looked like:
# windows or dos
getkey = msvcrt.getch
# assume unix
import tty, termios
Since Python doesn't come bundled with MS-Windows, out-of-the-box,
I don't think we should presume that "everyone" has Python on their
MS-Windows computer? OTOH, Python is usually part of the base or
core system on a GNU/Linux install. I'm not sure about Mac OS X.x?
Of course, if you only want MS-Windows users to run your code, then....
I'm still working my way through Python tutorials, and as yet, haven't
written any code that makes use of any platform specific libraries.
However, when I DO, and IF I forget to try:except: so other platforms
can run my code, Please Slap Me, and show me THIS post! =)
That being said:
Besides msvcrt/curses, what other libraries are there that we should
be aware of? I'd like to see them as pairs, if possible.... this looks
like a perfect thing for a dictionary! =)
I certainly think this is something that Newbies (like myself) should learn.
But without a MS-Windows computer handy, I don't know? Is there a
place that has this information?
b h a a l u u at g m a i l dot c o m
> I've developed chessTimer a bit further:
> I'm still hoping for some constructive criticism. But I also thought
> I'd mention the points I have doubts about:
> 1. Is wrapping the long lines where I have done so OK?
> 2. I've used 1 for White player, -1 for Black player, and (-1)*player
> to alternate players. Is there a better way?
> 3. I've used a lot of variables. Too Many?
> 4. Should more of the code be in functions?
> 5. Is there a way to collapse lines 107-114:
> if player == 1: # player is White
> whiteMoveCounter += 1
> print "Black to make move %d" % (blackMoveCounter)
> remainingWhiteTime -= timeUsedThisMove
> elif player == -1: # player is Black
> blackMoveCounter += 1
> print "White to make move %d" % (whiteMoveCounter)
> remainingBlackTime -= timeUsedThisMove
> Dick Moores
> Tutor maillist - Tutor at python.org
More information about the Tutor