-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrenchdeck.py
39 lines (27 loc) · 836 Bytes
/
frenchdeck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from dataclasses import dataclass
@dataclass
class Card:
rank: str
suit: str
"""
Source : https://github.com/fluentpython/example-code-2e/blob/master/01-data-model/frenchdeck.py
"""
class FrenchDeck:
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
suits = 'spades diamonds clubs hearts'.split()
def __init__(self):
self._cards = [Card(rank, suit) for suit in self.suits
for rank in self.ranks]
def __len__(self):
return len(self._cards)
def __getitem__(self, position):
return self._cards[position]
d = FrenchDeck()
print(f"Size the this deck is - %d" % len(d))
for c in d:
print(c)
Card
c = Card("7", "Diamonds")
print(f"Does this deck has %s, - %s" % (c, c in d))
from random import choice
print(f"Random card chosen - %s" % choice(d))