rust-pgn-reader-python-binding 2.3.0__tar.gz → 2.5.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.5.0}/Cargo.lock +11 -11
  2. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/Cargo.toml +1 -1
  3. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/PKG-INFO +3 -2
  4. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/README.md +2 -2
  5. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/example_str.py +4 -0
  6. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/lib.rs +68 -0
  7. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/test.py +114 -14
  8. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/.github/workflows/CI.yml +0 -0
  9. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/.gitignore +0 -0
  10. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/benches/parquet_bench.rs +0 -0
  11. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/pyproject.toml +0 -0
  12. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/bench.py +0 -0
  13. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/bench_parquet.py +0 -0
  14. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/bench_parquet_parallel.py +0 -0
  15. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/comment_parsing.rs +0 -0
  16. {rust_pgn_reader_python_binding-2.3.0 → rust_pgn_reader_python_binding-2.5.0}/src/example_rowgroup.py +0 -0
@@ -382,9 +382,9 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
382
382
 
383
383
  [[package]]
384
384
  name = "cc"
385
- version = "1.2.5"
385
+ version = "1.2.7"
386
386
  source = "registry+https://github.com/rust-lang/crates.io-index"
387
- checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
387
+ checksum = "a012a0df96dd6d06ba9a1b29d6402d1a5d77c6befd2566afdc26e10603dc93d7"
388
388
  dependencies = [
389
389
  "jobserver",
390
390
  "libc",
@@ -1171,9 +1171,9 @@ dependencies = [
1171
1171
 
1172
1172
  [[package]]
1173
1173
  name = "quote"
1174
- version = "1.0.37"
1174
+ version = "1.0.38"
1175
1175
  source = "registry+https://github.com/rust-lang/crates.io-index"
1176
- checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
1176
+ checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
1177
1177
  dependencies = [
1178
1178
  "proc-macro2",
1179
1179
  ]
@@ -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.5.0"
1233
1233
  dependencies = [
1234
1234
  "arrow",
1235
1235
  "criterion",
@@ -1280,18 +1280,18 @@ checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4"
1280
1280
 
1281
1281
  [[package]]
1282
1282
  name = "serde"
1283
- version = "1.0.216"
1283
+ version = "1.0.217"
1284
1284
  source = "registry+https://github.com/rust-lang/crates.io-index"
1285
- checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
1285
+ checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
1286
1286
  dependencies = [
1287
1287
  "serde_derive",
1288
1288
  ]
1289
1289
 
1290
1290
  [[package]]
1291
1291
  name = "serde_derive"
1292
- version = "1.0.216"
1292
+ version = "1.0.217"
1293
1293
  source = "registry+https://github.com/rust-lang/crates.io-index"
1294
- checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
1294
+ checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
1295
1295
  dependencies = [
1296
1296
  "proc-macro2",
1297
1297
  "quote",
@@ -1342,9 +1342,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
1342
1342
 
1343
1343
  [[package]]
1344
1344
  name = "syn"
1345
- version = "2.0.91"
1345
+ version = "2.0.94"
1346
1346
  source = "registry+https://github.com/rust-lang/crates.io-index"
1347
- checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
1347
+ checksum = "987bc0be1cdea8b10216bd06e2ca407d40b9543468fafd3ddfb02f36e77f71f3"
1348
1348
  dependencies = [
1349
1349
  "proc-macro2",
1350
1350
  "quote",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "rust_pgn_reader_python_binding"
3
- version = "2.3.0"
3
+ version = "2.5.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.5.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,12 @@ pub struct MoveExtractor {
33
55
  #[pyo3(get)]
34
56
  headers: Vec<(String, String)>,
35
57
 
58
+ #[pyo3(get)]
59
+ castling_rights: Vec<(bool, bool, bool, bool)>,
60
+
61
+ #[pyo3(get)]
62
+ position_status: Option<PositionStatus>,
63
+
36
64
  pos: Chess,
37
65
  }
38
66
 
@@ -49,8 +77,35 @@ impl MoveExtractor {
49
77
  clock_times: Vec::with_capacity(100),
50
78
  outcome: None,
51
79
  headers: Vec::with_capacity(10),
80
+ castling_rights: Vec::with_capacity(100),
81
+ position_status: None,
82
+ }
83
+ }
84
+
85
+ fn turn(&self) -> bool {
86
+ match self.pos.turn() {
87
+ Color::White => true,
88
+ Color::Black => false,
52
89
  }
53
90
  }
91
+
92
+ fn update_position_status(&mut self) {
93
+ // TODO this checks legal_moves() a bunch of times
94
+ self.position_status = Some(PositionStatus {
95
+ is_checkmate: self.pos.is_checkmate(),
96
+ is_stalemate: self.pos.is_stalemate(),
97
+ legal_move_count: self.pos.legal_moves().len(),
98
+ is_game_over: self.pos.is_game_over(),
99
+ insufficient_material: (
100
+ self.pos.has_insufficient_material(Color::White),
101
+ self.pos.has_insufficient_material(Color::Black),
102
+ ),
103
+ turn: match self.pos.turn() {
104
+ Color::White => true,
105
+ Color::Black => false,
106
+ },
107
+ });
108
+ }
54
109
  }
55
110
 
56
111
  impl Visitor for MoveExtractor {
@@ -78,6 +133,17 @@ impl Visitor for MoveExtractor {
78
133
  self.pos.play_unchecked(&m);
79
134
  let uci = UciMove::from_standard(&m);
80
135
  self.moves.push(uci.to_string());
136
+ // self.pos.castles().castling_rights();
137
+ // println!("castling {:?}", self.pos.castles());
138
+ let castling_bitboard = self.pos.castles().castling_rights();
139
+ let castling_rights = (
140
+ castling_bitboard.contains(shakmaty::Square::A1),
141
+ castling_bitboard.contains(shakmaty::Square::H1),
142
+ castling_bitboard.contains(shakmaty::Square::A8),
143
+ castling_bitboard.contains(shakmaty::Square::H8),
144
+ );
145
+
146
+ self.castling_rights.push(castling_rights);
81
147
  }
82
148
  Err(err) => {
83
149
  eprintln!("error in game: {} {}", err, san_plus);
@@ -144,6 +210,7 @@ impl Visitor for MoveExtractor {
144
210
  Outcome::Decisive { winner } => format!("{:?}", winner),
145
211
  Outcome::Draw => "Draw".to_string(),
146
212
  });
213
+ self.update_position_status();
147
214
  }
148
215
 
149
216
  fn end_game(&mut self) -> Self::Result {
@@ -205,6 +272,7 @@ fn parse_games(pgns: Vec<String>, num_threads: Option<usize>) -> PyResult<Vec<Mo
205
272
  .map_err(|err| pyo3::exceptions::PyValueError::new_err(err))
206
273
  }
207
274
 
275
+ // Parser for chess PGN notation
208
276
  #[pymodule]
209
277
  fn rust_pgn_reader_python_binding(m: &Bound<'_, PyModule>) -> PyResult<()> {
210
278
  m.add_function(wrap_pyfunction!(parse_game, m)?)?;
@@ -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,47 @@ 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))
450
+
451
+ def test_castling(self):
452
+ pgn_moves = """
453
+ 1. e4 e5 2. Bc4 c6 3. Nf3 d6 4. Rg1 f6 5. Rh1 g6 6. Ke2 b6 7. Ke1 g5
454
+ """
455
+
456
+ extractor = self.run_extractor(pgn_moves)
457
+
458
+ castling_reference = [
459
+ (True, True, True, True),
460
+ (True, True, True, True),
461
+ (True, True, True, True),
462
+ (True, True, True, True),
463
+ (True, True, True, True),
464
+ (True, True, True, True),
465
+ (True, False, True, True),
466
+ (True, False, True, True),
467
+ (True, False, True, True),
468
+ (True, False, True, True),
469
+ (False, False, True, True),
470
+ (False, False, True, True),
471
+ (False, False, True, True),
472
+ (False, False, True, True),
473
+ ]
474
+
475
+ self.assertTrue(extractor.castling_rights == castling_reference)
376
476
 
377
477
 
378
478
  if __name__ == "__main__":