Skip to content

Commit

Permalink
Incorporate changes from black and mypy. Add test case for when get_a…
Browse files Browse the repository at this point in the history
…s_tuple=True.
  • Loading branch information
evanjhoward11 committed Mar 15, 2024
1 parent 77d966d commit 6ed49a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
26 changes: 16 additions & 10 deletions stockfish/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,16 @@ def is_move_correct(self, move_value: str) -> bool:
self.info = old_self_info
return is_move_correct

def get_wdl_stats(self, get_as_tuple: bool = False) -> Union[List[int], Tuple[int, int, int], None]:
def get_wdl_stats(
self, get_as_tuple: bool = False
) -> Union[list[int] | tuple[int, ...] | None]:
"""Returns Stockfish's win/draw/loss stats for the side to move.
Args:
get_as_tuple:
Option to return the wdl stats as a tuple instead of a list
`Boolean`. Default is `False`.
Returns:
A list or tuple of three integers, unless the game is over (in which case
`None` is returned).
Expand All @@ -736,9 +738,11 @@ def get_wdl_stats(self, get_as_tuple: bool = False) -> Union[List[int], Tuple[in
split_line = [line.split(" ") for line in lines if " multipv 1 " in line][-1]
wdl_index = split_line.index("wdl")

wdl_stats = [int(split_line[i]) for i in range(wdl_index + 1, wdl_index + 4)]

if get_as_tuple:
return tuple([int(split_line[i]) for i in range(wdl_index + 1, wdl_index + 4)])
return [int(split_line[i]) for i in range(wdl_index + 1, wdl_index + 4)]
return tuple(wdl_stats)
return wdl_stats

def does_current_engine_version_have_wdl_option(self) -> bool:
"""Returns whether the user's version of Stockfish has the option
Expand Down Expand Up @@ -923,13 +927,15 @@ def get_top_moves(
# get move
"Move": self._pick(line, "pv"),
# get cp if available
"Centipawn": int(self._pick(line, "cp")) * perspective
if "cp" in line
else None,
"Centipawn": (
int(self._pick(line, "cp")) * perspective if "cp" in line else None
),
# get mate if available
"Mate": int(self._pick(line, "mate")) * perspective
if "mate" in line
else None,
"Mate": (
int(self._pick(line, "mate")) * perspective
if "mate" in line
else None
),
}

# add more info if verbose
Expand Down
3 changes: 3 additions & 0 deletions tests/stockfish/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,9 @@ def test_get_wdl_stats(self, stockfish: Stockfish):
wdl_stats_3 = stockfish.get_wdl_stats()
assert isinstance(wdl_stats_3, list) and len(wdl_stats_3) == 3

wdl_stats_4 = stockfish.get_wdl_stats(get_as_tuple=True)
assert isinstance(wdl_stats_4, tuple) and len(wdl_stats_4) == 3

stockfish.set_fen_position("8/8/8/8/8/3k4/3p4/3K4 w - - 0 1")
assert stockfish.get_wdl_stats() is None

Expand Down

0 comments on commit 6ed49a9

Please sign in to comment.