rust-pgn-reader-python-binding 2.3.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.3.0 → rust_pgn_reader_python_binding-2.4.0}/Cargo.lock +1 -1
  2. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/Cargo.toml +1 -1
  3. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/PKG-INFO +3 -2
  4. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/README.md +2 -2
  5. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/example_str.py +4 -0
  6. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/lib.rs +52 -0
  7. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/test.py +88 -14
  8. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/.github/workflows/CI.yml +0 -0
  9. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/.gitignore +0 -0
  10. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/benches/parquet_bench.rs +0 -0
  11. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/pyproject.toml +0 -0
  12. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/bench.py +0 -0
  13. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/bench_parquet.py +0 -0
  14. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/bench_parquet_parallel.py +0 -0
  15. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.4.0}/src/comment_parsing.rs +0 -0
  16. {rust_pgn_reader_python_binding-2.3.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.3.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.3.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.3.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)
@@ -4,11 +4,33 @@ 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 {
@@ -33,6 +55,9 @@ pub struct MoveExtractor {
33
55
  #[pyo3(get)]
34
56
  headers: Vec<(String, String)>,
35
57
 
58
+ #[pyo3(get)]
59
+ position_status: Option<PositionStatus>,
60
+
36
61
  pos: Chess,
37
62
  }
38
63
 
@@ -49,8 +74,34 @@ impl MoveExtractor {
49
74
  clock_times: Vec::with_capacity(100),
50
75
  outcome: None,
51
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,
52
85
  }
53
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
+ }
54
105
  }
55
106
 
56
107
  impl Visitor for MoveExtractor {
@@ -144,6 +195,7 @@ impl Visitor for MoveExtractor {
144
195
  Outcome::Decisive { winner } => format!("{:?}", winner),
145
196
  Outcome::Draw => "Draw".to_string(),
146
197
  });
198
+ self.update_position_status();
147
199
  }
148
200
 
149
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 = """
@@ -207,6 +210,9 @@ class TestPgnExtraction(unittest.TestCase):
207
210
  self.assertTrue(extractor.evals == evals_reference)
208
211
  self.assertTrue(extractor.clock_times == clock_times_reference)
209
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)
210
216
 
211
217
  def test_full_pgn_annotated(self):
212
218
  pgn_moves = """
@@ -319,10 +325,13 @@ class TestPgnExtraction(unittest.TestCase):
319
325
  self.assertTrue(extractor.valid_moves == valid_reference)
320
326
  self.assertTrue(extractor.evals == evals_reference)
321
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)
322
331
 
323
332
  def test_multithreaded(self):
324
333
  pgns = [
325
- "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",
326
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",
327
336
  ]
328
337
  extractor = rust_pgn_reader_python_binding.parse_games(pgns)
@@ -334,24 +343,74 @@ class TestPgnExtraction(unittest.TestCase):
334
343
 
335
344
  moves_reference = [
336
345
  [
337
- "d2d4",
338
- "g8f6",
339
346
  "g1f3",
340
- "c7c5",
341
- "e2e3",
342
- "b7b6",
347
+ "g7g6",
348
+ "b2b3",
349
+ "f8g7",
343
350
  "b1c3",
344
- "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",
345
374
  "f1b5",
346
- "a7a6",
347
- "b5d3",
348
- "c8b7",
375
+ "c8d7",
376
+ "b5c6",
377
+ "d7c6",
378
+ "g1f3",
379
+ "c6a4",
349
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",
350
393
  "b6b5",
351
- "b2b3",
352
- "d7d5",
353
- "c1b2",
354
- "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",
355
414
  ],
356
415
  [
357
416
  "e2e4",
@@ -373,6 +432,21 @@ class TestPgnExtraction(unittest.TestCase):
373
432
 
374
433
  self.assertTrue(extractor[0].moves == moves_reference[0])
375
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))
376
450
 
377
451
 
378
452
  if __name__ == "__main__":