Skip to content

Commit 2dc8ef2

Browse files
committed
put dataclasses and exceptions in a separate file
1 parent 948f02c commit 2dc8ef2

File tree

3 files changed

+125
-116
lines changed

3 files changed

+125
-116
lines changed

stockfish/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
from .models import Stockfish, StockfishException
1+
from .models import Stockfish
2+
from .types import StockfishException
23

34
__all__ = ["Stockfish", "StockfishException"]

stockfish/models.py

Lines changed: 7 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -19,120 +19,16 @@
1919
import platform
2020
from collections.abc import Sequence
2121

22-
23-
@dataclass
24-
class StockfishVersion:
25-
text: str
26-
major_minor: str = ""
27-
major: int = 0
28-
minor: int = 0
29-
patch: str = ""
30-
sha: str = ""
31-
is_dev_build: bool = False
32-
33-
34-
@dataclass
35-
class StockfishParameters:
36-
debug_log_file: str
37-
threads: int
38-
hash: int
39-
ponder: bool
40-
multipv: int
41-
skill_level: int
42-
move_overhead: int
43-
slow_mover: int
44-
uci_chess960: bool
45-
uci_limit_strength: bool
46-
uci_elo: int
47-
contempt: int
48-
min_split_depth: int
49-
minimum_thinking_time: int
50-
uci_show_wdl: bool | None = None
51-
52-
def to_dict(self) -> dict[str, str | int | bool]:
53-
mappings: dict[str, str | int | bool | None] = {
54-
"Debug Log File": self.debug_log_file,
55-
"Threads": self.threads,
56-
"Hash": self.hash,
57-
"Ponder": self.ponder,
58-
"MultiPV": self.multipv,
59-
"Skill Level": self.skill_level,
60-
"Move Overhead": self.move_overhead,
61-
"Slow Mover": self.slow_mover,
62-
"UCI_Chess960": self.uci_chess960,
63-
"UCI_LimitStrength": self.uci_limit_strength,
64-
"UCI_Elo": self.uci_elo,
65-
"Contempt": self.contempt,
66-
"Min Split Depth": self.min_split_depth,
67-
"Minimum Thinking Time": self.minimum_thinking_time,
68-
"UCI_ShowWDL": self.uci_show_wdl,
69-
}
70-
return {k: v for k, v in mappings.items() if v is not None}
71-
72-
def update(self, params: dict[str, str | int | bool]) -> None:
73-
mappings: dict[str, str] = {
74-
"Debug Log File": "debug_log_file",
75-
"Hash": "hash",
76-
"MultiPV": "multipv",
77-
"Skill Level": "skill_level",
78-
"UCI_LimitStrength": "uci_limit_strength",
79-
"Threads": "threads",
80-
"Ponder": "ponder",
81-
"Move Overhead": "move_overhead",
82-
"Slow Mover": "slow_mover",
83-
"UCI_Chess960": "uci_chess960",
84-
"UCI_Elo": "uci_elo",
85-
"Contempt": "contempt",
86-
"Min Split Depth": "min_split_depth",
87-
"Minimum Thinking Time": "minimum_thinking_time",
88-
"UCI_ShowWDL": "uci_show_wdl",
89-
}
90-
91-
for dict_key, value in params.items():
92-
field_name = mappings.get(dict_key)
93-
if field_name is None:
94-
continue
95-
if (
96-
type(getattr(self, field_name)) is not type(value)
97-
and field_name != "uci_show_wdl"
98-
):
99-
raise ValueError("wrong type")
100-
setattr(self, field_name, value)
101-
102-
103-
@dataclass
104-
class MoveEvaluation:
105-
move: str
106-
centipawn: int | None
107-
mate: int | None
108-
time: int | None = None
109-
nodes: int | None = None
110-
multipv_line: int | None = None
111-
nodes_per_second: int | None = None
112-
selective_depth: int | None = None
113-
wdl: str | None = None
114-
115-
def to_dict(self) -> dict[str, str | int | None]:
116-
mappings: dict[str, str | int | None] = {
117-
"Move": self.move,
118-
"Centipawn": self.centipawn,
119-
"Mate": self.mate,
120-
"Time": self.time,
121-
"Nodes": self.nodes,
122-
"MultiPVLine": self.multipv_line,
123-
"NodesPerSecond": self.nodes_per_second,
124-
"SelectiveDepth": self.selective_depth,
125-
"WDL": self.wdl,
126-
}
127-
return {
128-
k: v
129-
for k, v in mappings.items()
130-
if v is not None or k in ("Centipawn", "Mate")
131-
}
22+
from .types import (
23+
MoveEvaluation,
24+
StockfishParameters,
25+
StockfishException,
26+
StockfishVersion,
27+
)
13228

13329

13430
class Stockfish:
135-
"""Integrates the [Stockfish chess engine](https://stockfishchess.org/) with Python."""
31+
"""Integrates the [Stockfish chess engine](https://stockfishchess.org) with Python."""
13632

13733
# Used in test_models: will count how many times the del function is called.
13834
_del_counter: int = 0
@@ -1389,7 +1285,3 @@ def benchmark(self, params: BenchmarkParameters) -> str:
13891285
text = self._read_line()
13901286
if text.split(" ")[0] == "Nodes/second":
13911287
return text
1392-
1393-
1394-
class StockfishException(Exception):
1395-
pass

stockfish/types.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class StockfishVersion:
6+
text: str
7+
major_minor: str = ""
8+
major: int = 0
9+
minor: int = 0
10+
patch: str = ""
11+
sha: str = ""
12+
is_dev_build: bool = False
13+
14+
15+
@dataclass
16+
class StockfishParameters:
17+
debug_log_file: str
18+
threads: int
19+
hash: int
20+
ponder: bool
21+
multipv: int
22+
skill_level: int
23+
move_overhead: int
24+
slow_mover: int
25+
uci_chess960: bool
26+
uci_limit_strength: bool
27+
uci_elo: int
28+
contempt: int
29+
min_split_depth: int
30+
minimum_thinking_time: int
31+
uci_show_wdl: bool | None = None
32+
33+
def to_dict(self) -> dict[str, str | int | bool]:
34+
mappings: dict[str, str | int | bool | None] = {
35+
"Debug Log File": self.debug_log_file,
36+
"Threads": self.threads,
37+
"Hash": self.hash,
38+
"Ponder": self.ponder,
39+
"MultiPV": self.multipv,
40+
"Skill Level": self.skill_level,
41+
"Move Overhead": self.move_overhead,
42+
"Slow Mover": self.slow_mover,
43+
"UCI_Chess960": self.uci_chess960,
44+
"UCI_LimitStrength": self.uci_limit_strength,
45+
"UCI_Elo": self.uci_elo,
46+
"Contempt": self.contempt,
47+
"Min Split Depth": self.min_split_depth,
48+
"Minimum Thinking Time": self.minimum_thinking_time,
49+
"UCI_ShowWDL": self.uci_show_wdl,
50+
}
51+
return {k: v for k, v in mappings.items() if v is not None}
52+
53+
def update(self, params: dict[str, str | int | bool]) -> None:
54+
mappings: dict[str, str] = {
55+
"Debug Log File": "debug_log_file",
56+
"Hash": "hash",
57+
"MultiPV": "multipv",
58+
"Skill Level": "skill_level",
59+
"UCI_LimitStrength": "uci_limit_strength",
60+
"Threads": "threads",
61+
"Ponder": "ponder",
62+
"Move Overhead": "move_overhead",
63+
"Slow Mover": "slow_mover",
64+
"UCI_Chess960": "uci_chess960",
65+
"UCI_Elo": "uci_elo",
66+
"Contempt": "contempt",
67+
"Min Split Depth": "min_split_depth",
68+
"Minimum Thinking Time": "minimum_thinking_time",
69+
"UCI_ShowWDL": "uci_show_wdl",
70+
}
71+
72+
for dict_key, value in params.items():
73+
field_name = mappings.get(dict_key)
74+
if field_name is None:
75+
continue
76+
if (
77+
type(getattr(self, field_name)) is not type(value)
78+
and field_name != "uci_show_wdl"
79+
):
80+
raise ValueError("wrong type")
81+
setattr(self, field_name, value)
82+
83+
84+
@dataclass
85+
class MoveEvaluation:
86+
move: str
87+
centipawn: int | None
88+
mate: int | None
89+
time: int | None = None
90+
nodes: int | None = None
91+
multipv_line: int | None = None
92+
nodes_per_second: int | None = None
93+
selective_depth: int | None = None
94+
wdl: str | None = None
95+
96+
def to_dict(self) -> dict[str, str | int | None]:
97+
mappings: dict[str, str | int | None] = {
98+
"Move": self.move,
99+
"Centipawn": self.centipawn,
100+
"Mate": self.mate,
101+
"Time": self.time,
102+
"Nodes": self.nodes,
103+
"MultiPVLine": self.multipv_line,
104+
"NodesPerSecond": self.nodes_per_second,
105+
"SelectiveDepth": self.selective_depth,
106+
"WDL": self.wdl,
107+
}
108+
return {
109+
k: v
110+
for k, v in mappings.items()
111+
if v is not None or k in ("Centipawn", "Mate")
112+
}
113+
114+
115+
class StockfishException(Exception):
116+
pass

0 commit comments

Comments
 (0)