rust-pgn-reader-python-binding 2.2.0__tar.gz → 2.4.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.
Files changed (16) hide show
  1. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/Cargo.lock +1 -1
  2. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/Cargo.toml +1 -1
  3. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/PKG-INFO +3 -2
  4. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/README.md +2 -2
  5. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/example_str.py +4 -0
  6. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/lib.rs +63 -1
  7. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/test.py +106 -14
  8. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/.github/workflows/CI.yml +0 -0
  9. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/.gitignore +0 -0
  10. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/benches/parquet_bench.rs +0 -0
  11. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/pyproject.toml +0 -0
  12. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/bench.py +0 -0
  13. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/bench_parquet.py +0 -0
  14. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/bench_parquet_parallel.py +0 -0
  15. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/comment_parsing.rs +0 -0
  16. {rust_pgn_reader_python_binding-2.2.0 → rust_pgn_reader_python_binding-2.4.0}/src/example_rowgroup.py +0 -0
@@ -1229,7 +1229,7 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
1229
1229
 
1230
1230
  [[package]]
1231
1231
  name = "rust_pgn_reader_python_binding"
1232
- version = "2.2.0"
1232
+ version = "2.4.0"
1233
1233
  dependencies = [
1234
1234
  "arrow",
1235
1235
  "criterion",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "rust_pgn_reader_python_binding"
3
- version = "2.2.0"
3
+ version = "2.4.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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rust_pgn_reader_python_binding
3
- Version: 2.2.0
3
+ Version: 2.4.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -9,7 +9,7 @@ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
9
9
 
10
10
  # rust_pgn_reader_python_binding
11
11
  ## Fast PGN parsing bindings for Python
12
- This project adds Python bindings to [rust-pgn-reader](https://github.com/niklasf/rust-pgn-reader)
12
+ This project adds Python bindings to [rust-pgn-reader](https://github.com/niklasf/rust-pgn-reader). In addition, it also parses and extracts [%clk ..] and [%eval ..] tags from comments.
13
13
 
14
14
  ## Benchmarks
15
15
  Below are some benchmarks on Lichess's 2013-07 chess games (293,459 games).
@@ -36,3 +36,4 @@ For a more thorough tutorial, follow https://lukesalamone.github.io/posts/how-to
36
36
 
37
37
  Linux/WSL-only:
38
38
  `py-spy record -s -F -n -f speedscope --output profile.speedscope -- python ./src/bench_parquet.py`
39
+
@@ -1,6 +1,6 @@
1
1
  # rust_pgn_reader_python_binding
2
2
  ## Fast PGN parsing bindings for Python
3
- This project adds Python bindings to [rust-pgn-reader](https://github.com/niklasf/rust-pgn-reader)
3
+ This project adds Python bindings to [rust-pgn-reader](https://github.com/niklasf/rust-pgn-reader). In addition, it also parses and extracts [%clk ..] and [%eval ..] tags from comments.
4
4
 
5
5
  ## Benchmarks
6
6
  Below are some benchmarks on Lichess's 2013-07 chess games (293,459 games).
@@ -26,4 +26,4 @@ For a more thorough tutorial, follow https://lukesalamone.github.io/posts/how-to
26
26
  `py-spy record -s -F -f speedscope --output profile.speedscope -- python ./src/bench_parquet.py`
27
27
 
28
28
  Linux/WSL-only:
29
- `py-spy record -s -F -n -f speedscope --output profile.speedscope -- python ./src/bench_parquet.py`
29
+ `py-spy record -s -F -n -f speedscope --output profile.speedscope -- python ./src/bench_parquet.py`
@@ -24,3 +24,7 @@ print(extractor.valid_moves)
24
24
  print(extractor.evals)
25
25
  print(extractor.clock_times)
26
26
  print(extractor.outcome)
27
+ print(extractor.position_status.is_checkmate)
28
+ print(extractor.position_status.is_stalemate)
29
+ print(extractor.position_status.is_game_over)
30
+ print(extractor.position_status.legal_move_count)
@@ -1,14 +1,36 @@
1
1
  use crate::comment_parsing::parse_comments;
2
2
  use crate::comment_parsing::CommentContent;
3
- use pgn_reader::{BufferedReader, RawComment, SanPlus, Skip, Visitor};
3
+ use pgn_reader::{BufferedReader, RawComment, RawHeader, SanPlus, Skip, Visitor};
4
4
  use pyo3::prelude::*;
5
5
  use rayon::prelude::*;
6
6
  use rayon::ThreadPoolBuilder;
7
+ use shakmaty::Color;
7
8
  use shakmaty::{uci::UciMove, Chess, Outcome, Position};
8
9
  use std::io::Cursor;
9
10
 
10
11
  mod comment_parsing;
11
12
 
13
+ #[pyclass]
14
+ #[derive(Clone)]
15
+ pub struct PositionStatus {
16
+ #[pyo3(get)]
17
+ is_checkmate: bool,
18
+
19
+ #[pyo3(get)]
20
+ is_stalemate: bool,
21
+
22
+ #[pyo3(get)]
23
+ legal_move_count: usize,
24
+
25
+ #[pyo3(get)]
26
+ is_game_over: bool,
27
+
28
+ #[pyo3(get)]
29
+ insufficient_material: (bool, bool),
30
+
31
+ #[pyo3(get)]
32
+ turn: bool,
33
+ }
12
34
  #[pyclass]
13
35
  /// A Visitor to extract SAN moves and comments from PGN movetext
14
36
  pub struct MoveExtractor {
@@ -30,6 +52,12 @@ pub struct MoveExtractor {
30
52
  #[pyo3(get)]
31
53
  outcome: Option<String>,
32
54
 
55
+ #[pyo3(get)]
56
+ headers: Vec<(String, String)>,
57
+
58
+ #[pyo3(get)]
59
+ position_status: Option<PositionStatus>,
60
+
33
61
  pos: Chess,
34
62
  }
35
63
 
@@ -45,13 +73,46 @@ impl MoveExtractor {
45
73
  evals: Vec::with_capacity(100),
46
74
  clock_times: Vec::with_capacity(100),
47
75
  outcome: None,
76
+ headers: Vec::with_capacity(10),
77
+ position_status: None,
78
+ }
79
+ }
80
+
81
+ fn turn(&self) -> bool {
82
+ match self.pos.turn() {
83
+ Color::White => true,
84
+ Color::Black => false,
48
85
  }
49
86
  }
87
+
88
+ fn update_position_status(&mut self) {
89
+ // TODO this checks legal_moves() a bunch of times
90
+ self.position_status = Some(PositionStatus {
91
+ is_checkmate: self.pos.is_checkmate(),
92
+ is_stalemate: self.pos.is_stalemate(),
93
+ legal_move_count: self.pos.legal_moves().len(),
94
+ is_game_over: self.pos.is_game_over(),
95
+ insufficient_material: (
96
+ self.pos.has_insufficient_material(Color::White),
97
+ self.pos.has_insufficient_material(Color::Black),
98
+ ),
99
+ turn: match self.pos.turn() {
100
+ Color::White => true,
101
+ Color::Black => false,
102
+ },
103
+ });
104
+ }
50
105
  }
51
106
 
52
107
  impl Visitor for MoveExtractor {
53
108
  type Result = bool;
54
109
 
110
+ fn header(&mut self, key: &[u8], value: RawHeader<'_>) {
111
+ let key_str = String::from_utf8_lossy(key).into_owned();
112
+ let value_str = String::from_utf8_lossy(value.as_bytes()).into_owned();
113
+ self.headers.push((key_str, value_str));
114
+ }
115
+
55
116
  fn begin_game(&mut self) {
56
117
  self.moves.clear();
57
118
  self.pos = Chess::default();
@@ -134,6 +195,7 @@ impl Visitor for MoveExtractor {
134
195
  Outcome::Decisive { winner } => format!("{:?}", winner),
135
196
  Outcome::Draw => "Draw".to_string(),
136
197
  });
198
+ self.update_position_status();
137
199
  }
138
200
 
139
201
  fn end_game(&mut self) -> Self::Result {
@@ -87,6 +87,9 @@ class TestPgnExtraction(unittest.TestCase):
87
87
  self.assertTrue(extractor.valid_moves == valid_reference)
88
88
  self.assertTrue(extractor.evals == evals_reference)
89
89
  self.assertTrue(extractor.clock_times == clock_times_reference)
90
+ self.assertFalse(extractor.position_status.is_checkmate)
91
+ self.assertFalse(extractor.position_status.is_stalemate)
92
+ self.assertFalse(extractor.position_status.is_game_over)
90
93
 
91
94
  def test_full_pgn(self):
92
95
  pgn_moves = """
@@ -183,12 +186,33 @@ class TestPgnExtraction(unittest.TestCase):
183
186
  valid_reference = True
184
187
  evals_reference = []
185
188
  clock_times_reference = []
189
+ headers_reference = [
190
+ ("Event", "Rated Classical game"),
191
+ ("Site", "https://lichess.org/lhy6ehiv"),
192
+ ("White", "goerch"),
193
+ ("Black", "niltonrosao001"),
194
+ ("Result", "0-1"),
195
+ ("UTCDate", "2013.06.30"),
196
+ ("UTCTime", "22:10:02"),
197
+ ("WhiteElo", "1702"),
198
+ ("BlackElo", "2011"),
199
+ ("WhiteRatingDiff", "-3"),
200
+ ("BlackRatingDiff", "+5"),
201
+ ("ECO", "A46"),
202
+ ("Opening", "Indian Game: Spielmann-Indian"),
203
+ ("TimeControl", "600+8"),
204
+ ("Termination", "Normal"),
205
+ ]
186
206
 
187
207
  self.assertTrue(extractor.moves == moves_reference)
188
208
  self.assertTrue(extractor.comments == comments_reference)
189
209
  self.assertTrue(extractor.valid_moves == valid_reference)
190
210
  self.assertTrue(extractor.evals == evals_reference)
191
211
  self.assertTrue(extractor.clock_times == clock_times_reference)
212
+ self.assertTrue(extractor.headers == headers_reference)
213
+ self.assertFalse(extractor.position_status.is_checkmate)
214
+ self.assertFalse(extractor.position_status.is_stalemate)
215
+ self.assertFalse(extractor.position_status.is_game_over)
192
216
 
193
217
  def test_full_pgn_annotated(self):
194
218
  pgn_moves = """
@@ -301,10 +325,13 @@ class TestPgnExtraction(unittest.TestCase):
301
325
  self.assertTrue(extractor.valid_moves == valid_reference)
302
326
  self.assertTrue(extractor.evals == evals_reference)
303
327
  self.assertTrue(extractor.clock_times == clock_times_reference)
328
+ self.assertFalse(extractor.position_status.is_checkmate)
329
+ self.assertFalse(extractor.position_status.is_stalemate)
330
+ self.assertFalse(extractor.position_status.is_game_over)
304
331
 
305
332
  def test_multithreaded(self):
306
333
  pgns = [
307
- "1. d4 Nf6 2. Nf3 c5 3. e3 b6 4. Nc3 e6 5. Bb5 a6 6. Bd3 Bb7 7. O-O b5 8. b3 d5 9. Bb2 Nbd7",
334
+ "1. Nf3 g6 2. b3 Bg7 3. Nc3 e5 4. Bb2 e4 5. Ng1 d6 6. Rb1 a5 7. Nxe4 Bxb2 8. Rxb2 Nf6 9. Nxf6+ Qxf6 10. Rb1 Ra6 11. e3 Rb6 12. d4 a4 13. bxa4 Nc6 14. Rxb6 cxb6 15. Bb5 Bd7 16. Bxc6 Bxc6 17. Nf3 Bxa4 18. O-O O-O 19. Re1 Rc8 20. Re2 d5 21. Ne5 Qf5 22. Qd3 Bxc2 23. Qxf5 Bxf5 24. h3 b5 25. Rb2 f6 26. Ng4 Rc6 27. Nh6+ Kg7 28. Nxf5+ gxf5 29. Rxb5 Rc7 30. Rxd5 Kg6 31. f4 Kh5 32. Rxf5+ Kh4 33. Rxf6 Kg3 34. d5 Rc1# 0-1",
308
335
  "1. e4 {asdf} e5 2. Nf3 Nc6 3. Bb5 Nf6 4. O-O {hello} Bc5 5. d3 d6 6. h3 h6 7. c3 O-O",
309
336
  ]
310
337
  extractor = rust_pgn_reader_python_binding.parse_games(pgns)
@@ -316,24 +343,74 @@ class TestPgnExtraction(unittest.TestCase):
316
343
 
317
344
  moves_reference = [
318
345
  [
319
- "d2d4",
320
- "g8f6",
321
346
  "g1f3",
322
- "c7c5",
323
- "e2e3",
324
- "b7b6",
347
+ "g7g6",
348
+ "b2b3",
349
+ "f8g7",
325
350
  "b1c3",
326
- "e7e6",
351
+ "e7e5",
352
+ "c1b2",
353
+ "e5e4",
354
+ "f3g1",
355
+ "d7d6",
356
+ "a1b1",
357
+ "a7a5",
358
+ "c3e4",
359
+ "g7b2",
360
+ "b1b2",
361
+ "g8f6",
362
+ "e4f6",
363
+ "d8f6",
364
+ "b2b1",
365
+ "a8a6",
366
+ "e2e3",
367
+ "a6b6",
368
+ "d2d4",
369
+ "a5a4",
370
+ "b3a4",
371
+ "b8c6",
372
+ "b1b6",
373
+ "c7b6",
327
374
  "f1b5",
328
- "a7a6",
329
- "b5d3",
330
- "c8b7",
375
+ "c8d7",
376
+ "b5c6",
377
+ "d7c6",
378
+ "g1f3",
379
+ "c6a4",
331
380
  "e1g1",
381
+ "e8g8",
382
+ "f1e1",
383
+ "f8c8",
384
+ "e1e2",
385
+ "d6d5",
386
+ "f3e5",
387
+ "f6f5",
388
+ "d1d3",
389
+ "a4c2",
390
+ "d3f5",
391
+ "c2f5",
392
+ "h2h3",
332
393
  "b6b5",
333
- "b2b3",
334
- "d7d5",
335
- "c1b2",
336
- "b8d7",
394
+ "e2b2",
395
+ "f7f6",
396
+ "e5g4",
397
+ "c8c6",
398
+ "g4h6",
399
+ "g8g7",
400
+ "h6f5",
401
+ "g6f5",
402
+ "b2b5",
403
+ "c6c7",
404
+ "b5d5",
405
+ "g7g6",
406
+ "f2f4",
407
+ "g6h5",
408
+ "d5f5",
409
+ "h5h4",
410
+ "f5f6",
411
+ "h4g3",
412
+ "d4d5",
413
+ "c7c1",
337
414
  ],
338
415
  [
339
416
  "e2e4",
@@ -355,6 +432,21 @@ class TestPgnExtraction(unittest.TestCase):
355
432
 
356
433
  self.assertTrue(extractor[0].moves == moves_reference[0])
357
434
  self.assertTrue(extractor[1].moves == moves_reference[1])
435
+ self.assertTrue(extractor[0].position_status.is_checkmate)
436
+ self.assertFalse(extractor[0].position_status.is_stalemate)
437
+ self.assertTrue(extractor[0].position_status.is_game_over)
438
+ self.assertTrue(extractor[0].position_status.legal_move_count == 0)
439
+ self.assertTrue(extractor[0].position_status.turn == 1)
440
+ self.assertTrue(extractor[0].position_status.insufficient_material == (0, 0))
441
+
442
+ self.assertTrue(extractor[1].position_status is None)
443
+ extractor[1].update_position_status()
444
+ self.assertFalse(extractor[1].position_status.is_checkmate)
445
+ self.assertFalse(extractor[1].position_status.is_stalemate)
446
+ self.assertFalse(extractor[1].position_status.is_game_over)
447
+ self.assertTrue(extractor[1].position_status.legal_move_count == 36)
448
+ self.assertTrue(extractor[1].position_status.turn == 1)
449
+ self.assertTrue(extractor[1].position_status.insufficient_material == (0, 0))
358
450
 
359
451
 
360
452
  if __name__ == "__main__":