rust-pgn-reader-python-binding 1.1.0__tar.gz → 2.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,6 +41,37 @@ version = "0.3.0"
41
41
  source = "registry+https://github.com/rust-lang/crates.io-index"
42
42
  checksum = "b0fc239e0f6cb375d2402d48afb92f76f5404fd1df208a41930ec81eda078bea"
43
43
 
44
+ [[package]]
45
+ name = "crossbeam-deque"
46
+ version = "0.8.6"
47
+ source = "registry+https://github.com/rust-lang/crates.io-index"
48
+ checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
49
+ dependencies = [
50
+ "crossbeam-epoch",
51
+ "crossbeam-utils",
52
+ ]
53
+
54
+ [[package]]
55
+ name = "crossbeam-epoch"
56
+ version = "0.9.18"
57
+ source = "registry+https://github.com/rust-lang/crates.io-index"
58
+ checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
59
+ dependencies = [
60
+ "crossbeam-utils",
61
+ ]
62
+
63
+ [[package]]
64
+ name = "crossbeam-utils"
65
+ version = "0.8.21"
66
+ source = "registry+https://github.com/rust-lang/crates.io-index"
67
+ checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
68
+
69
+ [[package]]
70
+ name = "either"
71
+ version = "1.13.0"
72
+ source = "registry+https://github.com/rust-lang/crates.io-index"
73
+ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
74
+
44
75
  [[package]]
45
76
  name = "heck"
46
77
  version = "0.5.0"
@@ -210,13 +241,34 @@ dependencies = [
210
241
  "proc-macro2",
211
242
  ]
212
243
 
244
+ [[package]]
245
+ name = "rayon"
246
+ version = "1.10.0"
247
+ source = "registry+https://github.com/rust-lang/crates.io-index"
248
+ checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
249
+ dependencies = [
250
+ "either",
251
+ "rayon-core",
252
+ ]
253
+
254
+ [[package]]
255
+ name = "rayon-core"
256
+ version = "1.12.1"
257
+ source = "registry+https://github.com/rust-lang/crates.io-index"
258
+ checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
259
+ dependencies = [
260
+ "crossbeam-deque",
261
+ "crossbeam-utils",
262
+ ]
263
+
213
264
  [[package]]
214
265
  name = "rust_pgn_reader_python_binding"
215
- version = "1.1.0"
266
+ version = "2.1.0"
216
267
  dependencies = [
217
268
  "nom",
218
269
  "pgn-reader",
219
270
  "pyo3",
271
+ "rayon",
220
272
  "shakmaty",
221
273
  ]
222
274
 
@@ -234,9 +286,9 @@ dependencies = [
234
286
 
235
287
  [[package]]
236
288
  name = "syn"
237
- version = "2.0.90"
289
+ version = "2.0.91"
238
290
  source = "registry+https://github.com/rust-lang/crates.io-index"
239
- checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31"
291
+ checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
240
292
  dependencies = [
241
293
  "proc-macro2",
242
294
  "quote",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "rust_pgn_reader_python_binding"
3
- version = "1.1.0"
3
+ version = "2.1.0"
4
4
  edition = "2021"
5
5
 
6
6
  # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -13,3 +13,4 @@ pyo3 = "0.23.3"
13
13
  shakmaty = "0.27"
14
14
  pgn-reader = "0.26.0"
15
15
  nom = "7.1.3"
16
+ rayon = "1.10.0"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rust_pgn_reader_python_binding
3
- Version: 1.1.0
3
+ Version: 2.1.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -18,7 +18,8 @@ Below are some benchmarks on Lichess's 2013-07 chess games (293,459 games).
18
18
  |----------------------------------------------------------------------------|-------------|--------|
19
19
  | [rust-pgn-reader](https://github.com/niklasf/rust-pgn-reader/tree/master) | PGN | 1s |
20
20
  | rust_pgn_reader_python_binding | PGN | 4.7s |
21
- | rust_pgn_reader_python_binding | parquet | 4.3s |
21
+ | rust_pgn_reader_python_binding, parse_game (single_threaded) | parquet | 4.3s |
22
+ | rust_pgn_reader_python_binding, parse_games (multithreaded) | parquet | 1.5s |
22
23
  | [chess-library](https://github.com/Disservin/chess-library) | PGN | 2s |
23
24
  | [python-chess](https://github.com/niklasf/python-chess) | PGN | 3+ min |
24
25
 
@@ -9,7 +9,8 @@ Below are some benchmarks on Lichess's 2013-07 chess games (293,459 games).
9
9
  |----------------------------------------------------------------------------|-------------|--------|
10
10
  | [rust-pgn-reader](https://github.com/niklasf/rust-pgn-reader/tree/master) | PGN | 1s |
11
11
  | rust_pgn_reader_python_binding | PGN | 4.7s |
12
- | rust_pgn_reader_python_binding | parquet | 4.3s |
12
+ | rust_pgn_reader_python_binding, parse_game (single_threaded) | parquet | 4.3s |
13
+ | rust_pgn_reader_python_binding, parse_games (multithreaded) | parquet | 1.5s |
13
14
  | [chess-library](https://github.com/Disservin/chess-library) | PGN | 2s |
14
15
  | [python-chess](https://github.com/niklasf/python-chess) | PGN | 3+ min |
15
16
 
@@ -21,7 +21,7 @@ file_path = "lichess_db_standard_rated_2013-07.pgn"
21
21
 
22
22
  a = datetime.now()
23
23
  for game_pgn in split_pgn(file_path):
24
- extractor = rust_pgn_reader_python_binding.parse_moves(game_pgn)
24
+ extractor = rust_pgn_reader_python_binding.parse_game(game_pgn)
25
25
  moves = extractor.moves
26
26
  comments = extractor.comments
27
27
 
@@ -13,7 +13,7 @@ pf = ParquetFile(file_path)
13
13
 
14
14
  for df in pf.iter_row_groups():
15
15
  for row in df.movetext:
16
- extractor = rust_pgn_reader_python_binding.parse_moves(row)
16
+ extractor = rust_pgn_reader_python_binding.parse_game(row)
17
17
  moves = extractor.moves
18
18
  comments = extractor.comments
19
19
 
@@ -0,0 +1,18 @@
1
+ import rust_pgn_reader_python_binding
2
+ from fastparquet import ParquetFile
3
+
4
+ from datetime import datetime
5
+
6
+
7
+ file_path = "2013-07-train-00000-of-00001.parquet"
8
+
9
+
10
+ a = datetime.now()
11
+
12
+ pf = ParquetFile(file_path)
13
+
14
+ for df in pf.iter_row_groups():
15
+ extractors = rust_pgn_reader_python_binding.parse_games(df.movetext)
16
+
17
+ b = datetime.now()
18
+ print(b - a)
@@ -16,10 +16,11 @@ pgn_moves = """
16
16
  13. b3?? { [%eval -4.14] [%clk 0:00:02] } 13... Nf4? { [%eval -2.73] [%clk 0:00:21] } 0-1
17
17
  """
18
18
 
19
- extractor = rust_pgn_reader_python_binding.parse_moves(pgn_moves)
19
+ extractor = rust_pgn_reader_python_binding.parse_game(pgn_moves)
20
20
 
21
21
  print(extractor.moves)
22
22
  print(extractor.comments)
23
23
  print(extractor.valid_moves)
24
24
  print(extractor.evals)
25
25
  print(extractor.clock_times)
26
+ print(extractor.outcome)
@@ -1,9 +1,9 @@
1
- use shakmaty::{uci::UciMove, Chess, Position};
2
-
3
1
  use crate::comment_parsing::parse_comments;
4
2
  use crate::comment_parsing::CommentContent;
5
3
  use pgn_reader::{BufferedReader, RawComment, SanPlus, Skip, Visitor};
6
4
  use pyo3::prelude::*;
5
+ use rayon::prelude::*;
6
+ use shakmaty::{uci::UciMove, Chess, Outcome, Position};
7
7
  use std::io::Cursor;
8
8
 
9
9
  mod comment_parsing;
@@ -26,6 +26,9 @@ struct MoveExtractor {
26
26
  #[pyo3(get)]
27
27
  clock_times: Vec<(u32, u8, f64)>,
28
28
 
29
+ #[pyo3(get)]
30
+ outcome: Option<String>,
31
+
29
32
  pos: Chess,
30
33
  }
31
34
 
@@ -40,6 +43,7 @@ impl MoveExtractor {
40
43
  comments: Vec::with_capacity(100),
41
44
  evals: Vec::with_capacity(100),
42
45
  clock_times: Vec::with_capacity(100),
46
+ outcome: None,
43
47
  }
44
48
  }
45
49
  }
@@ -124,14 +128,19 @@ impl Visitor for MoveExtractor {
124
128
  Skip(true) // stay in the mainline
125
129
  }
126
130
 
131
+ fn outcome(&mut self, _outcome: Option<Outcome>) {
132
+ self.outcome = _outcome.map(|o| match o {
133
+ Outcome::Decisive { winner } => format!("{:?}", winner),
134
+ Outcome::Draw => "Draw".to_string(),
135
+ });
136
+ }
137
+
127
138
  fn end_game(&mut self) -> Self::Result {
128
139
  self.valid_moves
129
140
  }
130
141
  }
131
142
 
132
- /// Parses PGN movetext and returns a list of SAN moves and parsed comments
133
- #[pyfunction]
134
- fn parse_moves(pgn: &str) -> PyResult<MoveExtractor> {
143
+ fn parse_single_game(pgn: &str) -> PyResult<MoveExtractor> {
135
144
  let mut reader = BufferedReader::new(Cursor::new(pgn));
136
145
  let mut extractor = MoveExtractor::new();
137
146
 
@@ -147,9 +156,31 @@ fn parse_moves(pgn: &str) -> PyResult<MoveExtractor> {
147
156
  }
148
157
  }
149
158
 
159
+ fn parse_multiple_games(pgns: Vec<String>) -> PyResult<Vec<MoveExtractor>> {
160
+ let results: Vec<PyResult<MoveExtractor>> =
161
+ pgns.par_iter().map(|pgn| parse_single_game(pgn)).collect();
162
+
163
+ // Convert results to a single PyResult, aggregating errors
164
+ let extractors: Vec<MoveExtractor> = results.into_iter().collect::<Result<Vec<_>, _>>()?;
165
+ Ok(extractors)
166
+ }
167
+
168
+ /// Parses a full PGN game and returns a list of SAN moves and parsed comments
169
+ #[pyfunction]
170
+ fn parse_game(pgn: &str) -> PyResult<MoveExtractor> {
171
+ parse_single_game(pgn)
172
+ }
173
+
174
+ /// In parallel, parse a set of games
175
+ #[pyfunction]
176
+ fn parse_games(pgns: Vec<String>) -> PyResult<Vec<MoveExtractor>> {
177
+ parse_multiple_games(pgns)
178
+ }
179
+
150
180
  /// A Python module implemented in Rust.
151
181
  #[pymodule]
152
182
  fn rust_pgn_reader_python_binding(m: &Bound<'_, PyModule>) -> PyResult<()> {
153
- m.add_function(wrap_pyfunction!(parse_moves, m)?)?;
183
+ m.add_function(wrap_pyfunction!(parse_game, m)?)?;
184
+ m.add_function(wrap_pyfunction!(parse_games, m)?)?;
154
185
  Ok(())
155
186
  }
@@ -4,7 +4,7 @@ import rust_pgn_reader_python_binding
4
4
 
5
5
  class TestPgnExtraction(unittest.TestCase):
6
6
  def run_extractor(self, pgn_string):
7
- extractor = rust_pgn_reader_python_binding.parse_moves(pgn_string)
7
+ extractor = rust_pgn_reader_python_binding.parse_game(pgn_string)
8
8
  return extractor
9
9
 
10
10
  def test_short_pgn(self):