[Tutor] Please review my code - a simple iterator thingy to make round-robin pairings
dyoo at hkn.eecs.berkeley.edu
Mon Dec 12 19:52:53 CET 2005
On Mon, 12 Dec 2005, Pekka Karjalainen wrote:
> I hope my code and comments are clear enough for you to work out what I
> want to do. Please review my code, my commenting and give some advice on
> how to use the testing module. This is a fairly trivial example, but I'd
> like to learn how to make & run tests properly, so as to use them with
> Real Code. I'm looking forward to your replies.
One thing you can add is unit tests to see that your program is doing what
you expect. There's a great example of how unit tests work in Mark
Pilgrim's "Dive into Python":
and the things they talk about there can be applied to your program.
Have you played with 'unittest' before?
Some casual comments so far:
> # the constructor
> def __init__ (self,players,empty="empty"):
> if len(players)%2: players.insert(0,empty)
> self.players = players[:] # copy, so can modify
> self.times = 0
We might want to interchange the list copying with the code that ensures
len(players) is even. Otherwise, the caller can see mutation of their
input list, which goes contrary to doing the list copy in the first place.
Otherwise, the main thing I'd recommend is to back the algorithm with unit
tests; the implementation has some subtle details (like interchanging
colors) that has enough special cases to make me a little hesitant. Unit
testing would help us to have more confidence in those special cases.
Otherwise, looks good!
More information about the Tutor