From: <umg...@us...> - 2007-03-13 12:31:13
|
Revision: 362 http://svn.sourceforge.net/pybridge/?rev=362&view=rev Author: umgangee Date: 2007-03-13 05:31:04 -0700 (Tue, 13 Mar 2007) Log Message: ----------- Make Card a new-style class, subclassing object. Make rank and suit read-only properties. Enhanced type checking. Modified Paths: -------------- trunk/pybridge/pybridge/bridge/card.py Modified: trunk/pybridge/pybridge/bridge/card.py =================================================================== --- trunk/pybridge/pybridge/bridge/card.py 2007-03-13 12:27:29 UTC (rev 361) +++ trunk/pybridge/pybridge/bridge/card.py 2007-03-13 12:31:04 UTC (rev 362) @@ -21,16 +21,29 @@ from symbols import Rank, Suit -class Card(pb.Copyable, pb.RemoteCopy): - """A card has a rank and a suit.""" +class Card(object, pb.Copyable, pb.RemoteCopy): + """A card has a rank and a suit. + + @param rank: the rank of the card. + @type rank: L{Rank} + @param suit: the suit of the card. + @type suit: L{Suit} + """ + rank = property(lambda self: self.__rank) + suit = property(lambda self: self.__suit) + def __init__(self, rank, suit): - assert rank in Rank and suit in Suit - self.rank = rank - self.suit = suit + if rank not in Rank: + raise TypeError, "Expected Rank, got %s" % type(rank) + if suit not in Suit: + raise TypeError, "Expected Suit, got %s" % type(suit) + self.__rank = rank + self.__suit = suit + def __eq__(self, other): """Two cards are equivalent if their ranks and suits match.""" if isinstance(other, Card): @@ -56,13 +69,10 @@ def getStateToCopy(self): - state = {} - state['rank'] = self.rank.key - state['suit'] = self.suit.key - return state + return {'rank' : self.rank.key, 'suit' : self.suit.key} def setCopyableState(self, state): - self.rank = getattr(Rank, state['rank']) - self.suit = getattr(Suit, state['suit']) + self.__rank = getattr(Rank, state['rank']) + self.__suit = getattr(Suit, state['suit']) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |