Wanted: Criticism of code for a Python module, plus a Mac tester

Tim Chase python.list at tim.thechases.com
Thu Feb 16 01:43:42 CET 2012


On 02/15/12 17:33, HoneyMonster wrote:
> Firstly, is there anyone here who uses Python on a Mac and
> would be prepared to test it? I have tested it on Linux and
> Windows, but don't have access to a Mac.

It works from my quick test of it on my Mac.  The "class 
Player():" and the .format() calls choke on 2.4 (if perhaps for 
some reason you're running it on earlier versions), but 
otherwise, it should be good if you're running 2.7 everywhere.

> Secondly, as a more general point I would welcome comments on
> code quality, adherence to standards and so forth. The code is
> at:


All the .append()s seem a little unpythonic to me.  I'd just set 
it up with

   CONDITIONS = [
     [...],
     ...
     ]

And since you're not modifying it, I'd even use tuples (or named 
tuples):

   CONDITIONS = (
     ("Dealer North", "Neither vulnerable"),
     ...
     )

I'd also take advantage of iterable-unpacking to do something 
like the following in your Player.deal() method:

   for card in cards:
     suit, rank = DECK[card]
     {
      'S': self.spades,
      'D': self.diamonds,
      'C': self.clubs,
      'H': self.hearts,
     }[suit].append(rank)

(that fixed dictionary might even be hoisted out for reuse 
elsewhere).

Additionally, if you import this as a module rather than running 
it directly, there's no "north", "south", ... in your module 
namespace (they're only defined in the "if __name__ ..." section) 
so it will fail.

There are some other structural decisions that I might reconsider 
too:

- just shuffling the deck, rather than shuffling indexes into 
that deck

- there seems to be a lot of redundancy in the drawing code, but 
I'm not sure how I'd reduce that

- assigning to internal rank-named lists seems a little redundant 
to me.  In my (non-bridge) card-playing history, and tinkering 
with small programs like this to simulate card-games, I usually 
just give a player the cards and then leave the sifting/sorting 
to the display algorithm

- I see Ian's email came in as I was typing this and agree with 
his method of defining CONDITIONS with the caveat that it doesn't 
keep the same order as yours (I seem to recall bridge had some 
odd rules about that order)


-tkc







More information about the Python-list mailing list