This repository was archived by the owner on Dec 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathcards.py
More file actions
54 lines (43 loc) · 1.39 KB
/
cards.py
File metadata and controls
54 lines (43 loc) · 1.39 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Spadille is the nickname for the Ace of Spades in some games
(see `Webster 1913`_)
>>> beer_card = Card('7', Suite.diamonds)
>>> beer_card
Card('7', Suite.diamonds)
>>> spadille = Card('A', Suite.spades, long_rank='Ace')
>>> spadille
Card('A', Suite.spades)
>>> print(spadille)
Ace of spades
>>> bytes(spadille)
b'A\\x01'
>>> charles = Card('K', Suite.hearts)
>>> bytes(charles)
b'K\\x04'
>>> big_cassino = Card('10', Suite.diamonds)
>>> bytes(big_cassino)
b'T\\x02'
__ http://machaut.uchicago.edu/cgi-bin/WEBSTER.sh?WORD=spadille
"""
from enum import Enum
Suite = Enum('Suite', 'spades diamonds clubs hearts')
class Card:
def __init__(self, rank, suite, *, long_rank=None):
self.rank = rank
if long_rank is None:
self.long_rank = self.rank
else:
self.long_rank = long_rank
self.suite = suite
def __str__(self):
return '{long_rank} of {suite.name}'.format(**self.__dict__)
def __repr__(self):
constructor = '{cls.__name__}({args})'
args = '{0.rank!r}, Suite.{0.suite.name}'.format(self)
return constructor.format(cls=self.__class__, args=args)
def __bytes__(self):
if self.rank == '10':
rank_byte = b'T'
else:
rank_byte = bytes([ord(self.rank)])
return rank_byte + bytes([self.suite.value])