📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division1213import random141516class Card:17"""Represents a standard playing card.1819Attributes:20suit: integer 0-321rank: integer 1-1322"""2324suit_names = ["Clubs", "Diamonds", "Hearts", "Spades"]25rank_names = [None, "Ace", "2", "3", "4", "5", "6", "7",26"8", "9", "10", "Jack", "Queen", "King"]2728def __init__(self, suit=0, rank=2):29self.suit = suit30self.rank = rank3132def __str__(self):33"""Returns a human-readable string representation."""34return '%s of %s' % (Card.rank_names[self.rank],35Card.suit_names[self.suit])3637def __eq__(self, other):38"""Checks whether self and other have the same rank and suit.3940returns: boolean41"""42return self.suit == other.suit and self.rank == other.rank4344def __lt__(self, other):45"""Compares this card to other, first by suit, then rank.4647returns: boolean48"""49t1 = self.suit, self.rank50t2 = other.suit, other.rank51return t1 < t2525354class Deck:55"""Represents a deck of cards.5657Attributes:58cards: list of Card objects.59"""6061def __init__(self):62"""Initializes the Deck with 52 cards.63"""64self.cards = []65for suit in range(4):66for rank in range(1, 14):67card = Card(suit, rank)68self.cards.append(card)6970def __str__(self):71"""Returns a string representation of the deck.72"""73res = []74for card in self.cards:75res.append(str(card))76return '\n'.join(res)7778def add_card(self, card):79"""Adds a card to the deck.8081card: Card82"""83self.cards.append(card)8485def remove_card(self, card):86"""Removes a card from the deck or raises exception if it is not there.8788card: Card89"""90self.cards.remove(card)9192def pop_card(self, i=-1):93"""Removes and returns a card from the deck.9495i: index of the card to pop; by default, pops the last card.96"""97return self.cards.pop(i)9899def shuffle(self):100"""Shuffles the cards in this deck."""101random.shuffle(self.cards)102103def sort(self):104"""Sorts the cards in ascending order."""105self.cards.sort()106107def move_cards(self, hand, num):108"""Moves the given number of cards from the deck into the Hand.109110hand: destination Hand object111num: integer number of cards to move112"""113for i in range(num):114hand.add_card(self.pop_card())115116117class Hand(Deck):118"""Represents a hand of playing cards."""119120def __init__(self, label=''):121self.cards = []122self.label = label123124125def find_defining_class(obj, method_name):126"""Finds and returns the class object that will provide127the definition of method_name (as a string) if it is128invoked on obj.129130obj: any python object131method_name: string method name132"""133for ty in type(obj).mro():134if method_name in ty.__dict__:135return ty136return None137138139if __name__ == '__main__':140deck = Deck()141deck.shuffle()142143hand = Hand()144print(find_defining_class(hand, 'shuffle'))145146deck.move_cards(hand, 5)147hand.sort()148print(hand)149150151