Main.Cards History

Hide minor edits - Show changes to markup

April 09, 2010, at 08:38 AM MST by 10.84.44.67 -
Deleted line 2:
Added lines 76-102:
  1. Comments on list comprehension
  2. A common task in python is creating lists.
  3. For example if we wanted to created a list of all the squares of the elements
  4. in a given list we would do:

a = range(10) squared = [] for element in a :

    squared.append(a**2)
  1. Python has a concise way of doing this using a construct called list comprehension.
  2. List comprehension provides a compact way of mapping a list into another list by
  3. applying a function to each element of the list.
  4. Examples:

fruit_list = [' banana', ' strawberry ', 'passion fruit '] print [fruit.strip() for fruit in fruit_list]

vec = [2, 4, 6] print [3*x for x in vec] print [3*x for x in vec if x > 3] print [(x, x**2) for x in vec]

li = ["a", "asa", "foo", "b", "c", "b", "d", "d"] print [elem for elem in li if len(elem) > 1]

April 07, 2010, at 08:46 AM MST by 10.84.44.73 -
Changed line 16 from:
        if rank not in self.ranks :
to:
        if rank not in Card.ranks :
Changed line 18 from:
        if suit not in self.suits :
to:
        if suit not in Card.suits :
April 06, 2010, at 03:03 PM MST by 10.84.44.80 -
Added lines 1-78:

(:source lang=python:)

""" More objected oriented programming http://openbookproject.net/thinkcs/python/english2e/ch16.html """

import random

class Card :

    ranks = ["2", "3", "4" , "5", "6", "7", "8", "9", "10", 
             "Jack", "Queen", "King", "Ace"]
    suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
    def __init__(self, rank, suit) :
        if rank not in self.ranks :
            raise ValueError, "bad rank"
        if suit not in self.suits :
            raise ValueError, "bad suit"
        self.suit = Card.suits.index(suit)
        self.rank = Card.ranks.index(rank)
    def __repr__(self):
        return (Card.ranks[self.rank] + " of " + Card.suits[self.suit])
    def __cmp__(self, other):
        """Compare cards on the basis of their rank"""
        if self.rank > other.rank: return 1
        if self.rank < other.rank: return -1
        return 0

c1 = Card("Jack", "Spades") c2 = Card("King", "Hearts") c1 < c2

  1. Observations about the class:
  2. The arrays "ranks" and "suits" are not instance attributes - they are class attributes,
  3. i.e. variables that are associated with the class itself. You can access them as
  4. self.ranks and self.suits, but also as Card.ranks and Card.suits which more explicitly
  5. shows that they are class attributes. All instances of the class share the same
  6. instances of those variables. Therefore, if you modify class variables, that affects
  7. all instances of the class.
  8. The __cmp__ method defines the behavior of the comparison operators == != < > <= >=

class Deck :

    def __init__(self) :
        self.cards = []
        for suit in Card.suits :
            for rank in Card.ranks :
                self.cards.append(Card(rank, suit))
    def shuffle(self) :
        random.shuffle(self.cards)
    def __len__(self) :
        return len(self.cards)
    def __repr__(self):
        s = [str(card) for card in self.cards]
        return "\n".join(s)
    def __getitem__(self, key) :
        return self.cards.__getitem__(key)
    def remove(self, card):
        if card in self.cards:
            self.cards.remove(card)
            return True
        else:
            return False
    def deal_card(self) :
        return self.cards.pop()
    def is_empty(self) :
        return len(self) == 0
  1. Notes on the Deck class:
  2. __len__ is the function that gets called when the len function is called.
  3. Note the use of remove and pop in the remove and deal_card methods.
  4. Documentation of the set of all special methods (___xxx___) that are used to customize
  5. the behavior of a class are found at the python language reference manual at:
  6. http://docs.python.org/reference/datamodel.html#basic-customization

(:sourceend:)