Interface

Provides Card, Pile, and Deck classes for modelling cards, with Suits and Faces enums as backend data structures

Suits and Faces are enums containing the names and values of their respective items. Card models a single card, Pile models an ordered list of cards of arbitrary length, and Deck models a Pile of cards containing every permutation of suit and face, initially ordered by the enum values.

Faces enum

class model.Faces(value)

Bases: enum.Enum

An Enum class for the faces of a card

__lt__(other)

Return the comparison of two faces based on their enumerated value

__gt__(other, NotImplemented=NotImplemented)

Return a > b. Computed by @total_ordering from (not a < b) and (a != b).

__le__(other, NotImplemented=NotImplemented)

Return a <= b. Computed by @total_ordering from (a < b) or (a == b).

__ge__(other, NotImplemented=NotImplemented)

Return a >= b. Computed by @total_ordering from (not a < b).

Suits enum

class model.Suits(value)

Bases: enum.Enum

An Enum class for the suits in of a card

__lt__(other)

Return the comparison of two faces based on their enumerated value

__gt__(other, NotImplemented=NotImplemented)

Return a > b. Computed by @total_ordering from (not a < b) and (a != b).

__le__(other, NotImplemented=NotImplemented)

Return a <= b. Computed by @total_ordering from (a < b) or (a == b).

__ge__(other, NotImplemented=NotImplemented)

Return a >= b. Computed by @total_ordering from (not a < b).

Card object

class model.Card(face, suit)

Bases: object

A model of a card as having a Face and a Suit, a printeable and a typeable name, and value with respect other cards

__init__(face, suit)

Initialise the card as having a Face and a Suit

property face

Get the Face of the card

property suit

Get the Suit of the card

static get_from_typeable_name(typeable_name)

Return the Card object specified by the typeable name. If the name does not reference a valid card, return None

The first character of this string represents the face, as either the first letter of the face name if it is a picture card, or the number of the face if it is not. The second character of this string reprsents the suit, as the first letter of the suit name. For example, the ace of hearts would be ‘AH’, whereas the five of clubs would be ‘5C’

static get_face_from_typeable_name(typeable_name)

Return the Face enum specified by the typeable name

static get_suit_from_typeable_name(typeable_name)

Return the Suit enum specified by the typeable name

get_typeable_name()

Get the typeable name of the card, i.e. a unique string to describe a card’s value

__eq__(other)

Return the equality between two cards

__lt__(other)

Return whether the card has a lower value than another card, first by comparing faces, then if they are equal by comparing suits

__hash__()

Generate a unique integer representation of the card

__str__()

Use the string representation of the card as the informal representation of the card

__repr__()

Get a string representing the card, using UTF-8 characters to prettily denote the suit

__ge__(other, NotImplemented=NotImplemented)

Return a >= b. Computed by @total_ordering from (not a < b).

__gt__(other, NotImplemented=NotImplemented)

Return a > b. Computed by @total_ordering from (not a < b) and (a != b).

__le__(other, NotImplemented=NotImplemented)

Return a <= b. Computed by @total_ordering from (a < b) or (a == b).

__weakref__

list of weak references to the object (if defined)

Pile object

class model.Pile(cards=None)

Bases: object

A Pile object, which represents an ordered list of cards of arbitrary length

__init__(cards=None)

Initialise the pile by default as empty, or with a specified list of initial cards

property cards

Get the list of cards in the pile

pop()

Pop a card off the top of the pile

remove(card)

Remove a card from the Pile

peek(position=- 1)

Peek at the value of a card in the pile, by default the top card, or at a specified position in the pile

insert(card, position=None)

Place a card into the pile, by default to the top, or at a specified position in the pile

append(card)

Append the card to the Pile

shuffle()

Randomly shuffle the order of the cards in the pile

sort()

Sort the pile of cards in the total ordering of the cards

deal(num_sets, num_cards)

Deal cards from the pile into a specified number of new piles, each containing a specified number of cards. If there are more cards required to fill the piles than there are in the current pile, stop when the current pile is empty, and return the piles, irrespective of the fact they are not totally filled

clear()

Empty the contents of the Pile

count(card)

Count the number of instances of a card in the Pile

size()

Return the number of cards in the pile

empty()

Return whether the pile is empty

copy()

Return a copy of the Pile

extend(other)

Extend the pile contents by another pile

reverse()

Reverse the order of the Pile

__add__(other)

Concatenate a pile to the end of the current pile

__sub__(other)

Remove all cards from the current pile that are in the other pile (take the relative complement of the current and other piles)

__iter__()

Create an iterator for the pile of cards

__contains__(item)

Return whether the pile contains a specified card

__len__()

Get the size of the pile

__getitem__(position)

Get the item at an index in the Pile

__reversed__()

Return a new Pile of the current cards, but in reverse order

__and__(other)

Return the intersection of the current and another pile of cards

__or__(other)

Return the union of the current and another pile of cards

__eq__(other)

Return whether two piles are equal (i.e. contain the same cards in the same order)

__lt__(other)

Return whether this pile is smaller than the other pile

__hash__()

Generate a unique integer representation of the pile

__ge__(other, NotImplemented=NotImplemented)

Return a >= b. Computed by @total_ordering from (not a < b).

__gt__(other, NotImplemented=NotImplemented)

Return a > b. Computed by @total_ordering from (not a < b) and (a != b).

__le__(other, NotImplemented=NotImplemented)

Return a <= b. Computed by @total_ordering from (a < b) or (a == b).

__str__()

Use the string representation of the pile as the informal representation of the pile

__weakref__

list of weak references to the object (if defined)

__repr__()

Return a string representation of the pile, formatted as a list of the string representations of the cards it holds

Deck object

class model.Deck

Bases: model.Pile

A Deck object, which is a Pile object that is initialised with every permutation of suit and face, initially ordered by the enum values

__init__()

Initialise the pile by default as empty, or with a specified list of initial cards

Custom exceptions

exception model.CardError

Bases: Exception

A custom exception to indicate an error occuring in the Card class

exception model.PileError

Bases: Exception

A custom exception to indicate an error occuring in the Pile class