Skip to content

Commit

Permalink
Adds Violation Class to PlayDetails. Fixes Issue #204
Browse files Browse the repository at this point in the history
  • Loading branch information
KCNilssen committed Mar 27, 2024
1 parent c65db52 commit 8adfe28
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions mlbstatsapi/models/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ class CodeDesc:
code: str
description: Optional[str] = None

@dataclass
class Violation:
"""
Attributes
----------
type : str
the type of violation during the play
description : str
the description of the violation that occured
player : player
the player that caused the violation
"""
type: Optional[str] = None
description: Optional[str] = None
player: Optional[dict] = None

@dataclass(repr=False)
class Count:
Expand Down Expand Up @@ -332,12 +348,14 @@ class PlayDetails:
trailcolor: Optional[str] = None
fromcatcher: Optional[bool] = None
disengagementnum: Optional[int] = None
violation: Optional[Union[Violation, dict]] = field(default_factory=dict)

def __post_init__(self):
self.call = CodeDesc(**self.call) if self.call else self.call
self.batside = CodeDesc(**self.batside) if self.batside else self.batside
self.pitchhand = CodeDesc(**self.pitchhand) if self.pitchhand else self.pitchhand
self.type = CodeDesc(**self.type) if self.type else self.type
self.violation = Violation(**self.violation) if self.violation else self.violation

def __repr__(self) -> str:
kws = [f'{key}={value}' for key, value in self.__dict__.items() if value is not None and value]
Expand Down
4 changes: 2 additions & 2 deletions tests/external_tests/game/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class TestGame(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.mlb = Mlb()
cls.game = cls.mlb.get_game(662242)
cls.game = cls.mlb.get_game(717911)

@classmethod
def tearDownClass(cls) -> None:
pass

def test_game_creation(self):
self.assertEqual(self.game.id, 662242)
self.assertEqual(self.game.id, 717911)
self.assertIsInstance(self.game, Game)

def test_game_attrs(self):
Expand Down

0 comments on commit 8adfe28

Please sign in to comment.