Skip to content

Cards

patiencepilot.cards

Playing card primitives.

Color

Bases: Enum

A playing card color.

Source code in src/patiencepilot/cards.py
 9
10
11
12
13
class Color(Enum):
    """A playing card color."""

    RED = "red"
    BLACK = "black"

Suit

Bases: Enum

A French-suited playing card suit.

Source code in src/patiencepilot/cards.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Suit(Enum):
    """A French-suited playing card suit."""

    HEARTS = "H"
    DIAMONDS = "D"
    CLUBS = "C"
    SPADES = "S"

    @property
    def color(self) -> Color:
        """Return the suit color."""
        if self in {Suit.HEARTS, Suit.DIAMONDS}:
            return Color.RED
        return Color.BLACK

    @property
    def code(self) -> str:
        """Return the compact one-letter suit code."""
        return self.value

    @classmethod
    def from_code(cls, code: str) -> Suit:
        """Return the suit represented by a compact one-letter code.

        Args:
            code: One of ``H``, ``D``, ``C``, or ``S``.

        Raises:
            ValueError: If ``code`` is not a known suit code.
        """
        normalized = code.strip().upper()
        for suit in cls:
            if suit.code == normalized:
                return suit
        msg = f"unknown suit code: {code!r}"
        raise ValueError(msg)

color property

color: Color

Return the suit color.

code property

code: str

Return the compact one-letter suit code.

from_code classmethod

from_code(code: str) -> Suit

Return the suit represented by a compact one-letter code.

Parameters:

Name Type Description Default
code str

One of H, D, C, or S.

required

Raises:

Type Description
ValueError

If code is not a known suit code.

Source code in src/patiencepilot/cards.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
@classmethod
def from_code(cls, code: str) -> Suit:
    """Return the suit represented by a compact one-letter code.

    Args:
        code: One of ``H``, ``D``, ``C``, or ``S``.

    Raises:
        ValueError: If ``code`` is not a known suit code.
    """
    normalized = code.strip().upper()
    for suit in cls:
        if suit.code == normalized:
            return suit
    msg = f"unknown suit code: {code!r}"
    raise ValueError(msg)

Rank

Bases: IntEnum

A playing card rank from ace through king.

Source code in src/patiencepilot/cards.py
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class Rank(IntEnum):
    """A playing card rank from ace through king."""

    ACE = 1
    TWO = 2
    THREE = 3
    FOUR = 4
    FIVE = 5
    SIX = 6
    SEVEN = 7
    EIGHT = 8
    NINE = 9
    TEN = 10
    JACK = 11
    QUEEN = 12
    KING = 13

    @property
    def code(self) -> str:
        """Return the compact rank code."""
        return {
            Rank.ACE: "A",
            Rank.TEN: "T",
            Rank.JACK: "J",
            Rank.QUEEN: "Q",
            Rank.KING: "K",
        }.get(self, str(self.value))

    @classmethod
    def from_code(cls, code: str) -> Rank:
        """Return the rank represented by a compact code.

        Args:
            code: One of ``A``, ``2`` through ``10``, ``T``, ``J``, ``Q``, or
                ``K``.

        Raises:
            ValueError: If ``code`` is not a known rank code.
        """
        normalized = code.strip().upper()
        if normalized == "10":
            normalized = "T"
        for rank in cls:
            if rank.code == normalized:
                return rank
        msg = f"unknown rank code: {code!r}"
        raise ValueError(msg)

code property

code: str

Return the compact rank code.

from_code classmethod

from_code(code: str) -> Rank

Return the rank represented by a compact code.

Parameters:

Name Type Description Default
code str

One of A, 2 through 10, T, J, Q, or K.

required

Raises:

Type Description
ValueError

If code is not a known rank code.

Source code in src/patiencepilot/cards.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
@classmethod
def from_code(cls, code: str) -> Rank:
    """Return the rank represented by a compact code.

    Args:
        code: One of ``A``, ``2`` through ``10``, ``T``, ``J``, ``Q``, or
            ``K``.

    Raises:
        ValueError: If ``code`` is not a known rank code.
    """
    normalized = code.strip().upper()
    if normalized == "10":
        normalized = "T"
    for rank in cls:
        if rank.code == normalized:
            return rank
    msg = f"unknown rank code: {code!r}"
    raise ValueError(msg)

Card dataclass

A standard playing card.

Source code in src/patiencepilot/cards.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
@dataclass(frozen=True, slots=True)
class Card:
    """A standard playing card."""

    rank: Rank
    suit: Suit

    @property
    def color(self) -> Color:
        """Return the card color."""
        return self.suit.color

    @property
    def code(self) -> str:
        """Return the compact card code."""
        return f"{self.rank.code}{self.suit.code}"

    @classmethod
    def from_code(cls, code: str) -> Card:
        """Return the card represented by a compact code.

        Args:
            code: A rank code followed by a suit code, such as ``AS`` or
                ``10H``.

        Raises:
            ValueError: If ``code`` is not a valid card code.
        """
        normalized = code.strip().upper()
        if len(normalized) < 2:
            msg = f"card code is too short: {code!r}"
            raise ValueError(msg)
        return cls(rank=Rank.from_code(normalized[:-1]), suit=Suit.from_code(normalized[-1]))

    def __str__(self) -> str:
        """Return the compact card code."""
        return self.code

color property

color: Color

Return the card color.

code property

code: str

Return the compact card code.

from_code classmethod

from_code(code: str) -> Card

Return the card represented by a compact code.

Parameters:

Name Type Description Default
code str

A rank code followed by a suit code, such as AS or 10H.

required

Raises:

Type Description
ValueError

If code is not a valid card code.

Source code in src/patiencepilot/cards.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@classmethod
def from_code(cls, code: str) -> Card:
    """Return the card represented by a compact code.

    Args:
        code: A rank code followed by a suit code, such as ``AS`` or
            ``10H``.

    Raises:
        ValueError: If ``code`` is not a valid card code.
    """
    normalized = code.strip().upper()
    if len(normalized) < 2:
        msg = f"card code is too short: {code!r}"
        raise ValueError(msg)
    return cls(rank=Rank.from_code(normalized[:-1]), suit=Suit.from_code(normalized[-1]))

__str__

__str__() -> str

Return the compact card code.

Source code in src/patiencepilot/cards.py
137
138
139
def __str__(self) -> str:
    """Return the compact card code."""
    return self.code

standard_deck

standard_deck() -> tuple[Card, ...]

Return a standard 52-card deck in deterministic order.

Source code in src/patiencepilot/cards.py
142
143
144
def standard_deck() -> tuple[Card, ...]:
    """Return a standard 52-card deck in deterministic order."""
    return tuple(Card(rank=rank, suit=suit) for suit in Suit for rank in Rank)