|
19 | 19 | import platform |
20 | 20 | from collections.abc import Sequence |
21 | 21 |
|
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 | +) |
132 | 28 |
|
133 | 29 |
|
134 | 30 | class Stockfish: |
135 | | - """Integrates the [Stockfish chess engine](https://stockfishchess.org/) with Python.""" |
| 31 | + """Integrates the [Stockfish chess engine](https://stockfishchess.org) with Python.""" |
136 | 32 |
|
137 | 33 | # Used in test_models: will count how many times the del function is called. |
138 | 34 | _del_counter: int = 0 |
@@ -1389,7 +1285,3 @@ def benchmark(self, params: BenchmarkParameters) -> str: |
1389 | 1285 | text = self._read_line() |
1390 | 1286 | if text.split(" ")[0] == "Nodes/second": |
1391 | 1287 | return text |
1392 | | - |
1393 | | - |
1394 | | -class StockfishException(Exception): |
1395 | | - pass |
0 commit comments