11"""Module for card."""
2+
23from __future__ import annotations
34
4- from typing import Any , Union
5+
6+ CARD_DESCRIPTION_LENGTH = 2
57
68# fmt: off
79rank_map = {
1012}
1113suit_map = {
1214 "C" : 0 , "D" : 1 , "H" : 2 , "S" : 3 ,
13- "c" : 0 , "d" : 1 , "h" : 2 , "s" : 3
15+ "c" : 0 , "d" : 1 , "h" : 2 , "s" : 3 ,
1416}
1517# fmt: on
1618
@@ -63,33 +65,33 @@ class Card:
6365 The string parameter of the constructor should be exactly 2 characters.
6466
6567 >>> Card("9h") # OK
66- >>> Card("9h ") # ERROR
68+ >>> Card("9h ") # ERROR
6769
6870 TypeError: Construction with unsupported type
6971 The parameter of the constructor should be one of the following types: [int,
7072 str, Card].
71- >>> Card(0) # OK. The 0 stands 2 of Clubs
72- >>> Card("2c") # OK
73- >>> Card("2C") # OK. Capital letter is also accepted.
74- >>> Card(Card(0)) # OK
75- >>> Card(0.0) # ERROR. float is not allowed
73+ >>> Card(0) # OK. The 0 stands 2 of Clubs
74+ >>> Card("2c") # OK
75+ >>> Card("2C") # OK. Capital letter is also accepted.
76+ >>> Card(Card(0)) # OK
77+ >>> Card(0.0) # ERROR. float is not allowed
7678
7779 TypeError: Setting attribute
7880 >>> c = Card("2c")
79- >>> c.__id = 1 # ERROR
80- >>> c._Card__id = 1 # ERROR
81+ >>> c.__id = 1 # ERROR
82+ >>> c._Card__id = 1 # ERROR
8183
8284 TypeError: Deliting attribute
8385 >>> c = Card("2c")
84- >>> del c.__id # ERROR
85- >>> del c._Card__id # ERROR
86+ >>> del c.__id # ERROR
87+ >>> del c._Card__id # ERROR
8688
8789 """
8890
8991 __slots__ = ["__id" ]
9092 __id : int
9193
92- def __init__ (self , other : Union [ int , str , Card ]) :
94+ def __init__ (self , other : int | str | Card ) -> None :
9395 """Construct card object.
9496
9597 If the passed argument is integer, it's set to `self.__id`.
@@ -128,7 +130,7 @@ def id_(self) -> int:
128130 return self .__id
129131
130132 @staticmethod
131- def to_id (other : Union [ int , str , Card ] ) -> int :
133+ def to_id (other : int | str | Card ) -> int :
132134 """Return the Card ID integer as API.
133135
134136 If the passed argument is integer, it's returned with doing nothing.
@@ -149,17 +151,20 @@ def to_id(other: Union[int, str, Card]) -> int:
149151 """
150152 if isinstance (other , int ):
151153 return other
152- elif isinstance (other , str ):
153- if len (other ) != 2 :
154- raise ValueError (f"The length of value must be 2. passed: { other } " )
154+ if isinstance (other , str ):
155+ if len (other ) != CARD_DESCRIPTION_LENGTH :
156+ msg = (
157+ f"The length of value must be { CARD_DESCRIPTION_LENGTH } . "
158+ f"passed: { other } "
159+ )
160+ raise ValueError (msg )
155161 rank , suit , * _ = tuple (other )
156162 return rank_map [rank ] * 4 + suit_map [suit ]
157- elif isinstance (other , Card ):
163+ if isinstance (other , Card ):
158164 return other .id_
159165
160- raise TypeError (
161- f"Type of parameter must be int, str or Card. passed: { type (other )} "
162- )
166+ msg = f"Type of parameter must be int, str or Card. passed: { type (other )} "
167+ raise TypeError (msg )
163168
164169 def describe_rank (self ) -> str :
165170 """Calculate card rank.
@@ -212,7 +217,7 @@ def describe_card(self) -> str:
212217 """
213218 return self .describe_rank () + self .describe_suit ()
214219
215- def __eq__ (self , other : Any ) -> bool :
220+ def __eq__ (self , other : object ) -> bool :
216221 """Return equality. This is special method.
217222
218223 Args:
@@ -262,10 +267,12 @@ def __hash__(self) -> int:
262267 """int: Special method for `hash(self)`."""
263268 return hash (self .id_ )
264269
265- def __setattr__ (self , name : str , value : Any ) -> None :
266- """Set an attribute. This causes TypeError since assignment to attribute is prevented."""
267- raise TypeError ("Card object does not support assignment to attribute" )
270+ def __setattr__ (self , name : str , value : object ) -> None :
271+ """Set an attribute. This causes TypeError since assignment is prevented."""
272+ msg = "Card object does not support assignment to attribute"
273+ raise TypeError (msg )
268274
269275 def __delattr__ (self , name : str ) -> None :
270- """Delete an attribute. This causes TypeError since deletion of attribute is prevented."""
271- raise TypeError ("Card object does not support deletion of attribute" )
276+ """Delete an attribute. This causes TypeError since deletion is prevented."""
277+ msg = "Card object does not support deletion of attribute"
278+ raise TypeError (msg )
0 commit comments