rust-pgn-reader-python-binding 3.0.0__tar.gz → 3.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.
Files changed (17) hide show
  1. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/Cargo.lock +1 -1
  2. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/Cargo.toml +1 -1
  3. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/PKG-INFO +10 -3
  4. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/README.md +9 -2
  5. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/lib.rs +97 -8
  6. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/.github/workflows/CI.yml +0 -0
  7. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/.gitignore +0 -0
  8. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/benches/parquet_bench.rs +0 -0
  9. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/pyproject.toml +0 -0
  10. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/bench.py +0 -0
  11. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/bench_parquet.py +0 -0
  12. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/bench_parquet_arrow.py +0 -0
  13. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/bench_parquet_parallel.py +0 -0
  14. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/comment_parsing.rs +0 -0
  15. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/example_rowgroup.py +0 -0
  16. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/example_str.py +0 -0
  17. {rust_pgn_reader_python_binding-3.0.0 → rust_pgn_reader_python_binding-3.1.0}/src/test.py +0 -0
@@ -1394,7 +1394,7 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
1394
1394
 
1395
1395
  [[package]]
1396
1396
  name = "rust_pgn_reader_python_binding"
1397
- version = "3.0.0"
1397
+ version = "3.1.0"
1398
1398
  dependencies = [
1399
1399
  "arrow",
1400
1400
  "arrow-array",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "rust_pgn_reader_python_binding"
3
- version = "3.0.0"
3
+ version = "3.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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: rust_pgn_reader_python_binding
3
- Version: 3.0.0
3
+ Version: 3.1.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -23,15 +23,21 @@ Below are some benchmarks on Lichess's 2013-07 chess games (293,459 games) on an
23
23
  | rust_pgn_reader_python_binding | PGN | 4.7s |
24
24
  | rust_pgn_reader_python_binding, parse_game (single_threaded) | parquet | 3.3s |
25
25
  | rust_pgn_reader_python_binding, parse_games (multithreaded) | parquet | 0.5s |
26
+ | rust_pgn_reader_python_binding, parse_game_moves_arrow_chunked_array (multithreaded) | parquet | 0.35s |
26
27
  | [chess-library](https://github.com/Disservin/chess-library) | PGN | 2s |
27
28
  | [python-chess](https://github.com/niklasf/python-chess) | PGN | 3+ min |
28
29
 
29
- The main reason for rust_pgn_reader_python_binding being slower (at least in single threaded mode) than rust-pgn-reader is because of vector / string allocations (that show up as malloc's and free's in profiling).
30
+ To replicate, download `2013-07-train-00000-of-00001.parquet` and then run:
30
31
 
31
- To replicate, download `2013-07-train-00000-of-00001.parquet` and then run `python bench_parquet_parallel.py`
32
+ `python bench_parquet.py` (single-threaded parse_game)
33
+
34
+ `python bench_parquet_parallel.py` (multithreaded parse_games)
35
+
36
+ `python bench_parquet_arrow.py` (multithreaded parse_game_moves_arrow_chunked_array)
32
37
 
33
38
  ## Building
34
39
  `maturin develop`
40
+
35
41
  `maturin develop --release`
36
42
 
37
43
  For a more thorough tutorial, follow https://lukesalamone.github.io/posts/how-to-create-rust-python-bindings/
@@ -44,4 +50,5 @@ Linux/WSL-only:
44
50
 
45
51
  ## Testing
46
52
  `cargo test`
53
+
47
54
  `python -m unittest src/test.py`
@@ -14,15 +14,21 @@ Below are some benchmarks on Lichess's 2013-07 chess games (293,459 games) on an
14
14
  | rust_pgn_reader_python_binding | PGN | 4.7s |
15
15
  | rust_pgn_reader_python_binding, parse_game (single_threaded) | parquet | 3.3s |
16
16
  | rust_pgn_reader_python_binding, parse_games (multithreaded) | parquet | 0.5s |
17
+ | rust_pgn_reader_python_binding, parse_game_moves_arrow_chunked_array (multithreaded) | parquet | 0.35s |
17
18
  | [chess-library](https://github.com/Disservin/chess-library) | PGN | 2s |
18
19
  | [python-chess](https://github.com/niklasf/python-chess) | PGN | 3+ min |
19
20
 
20
- The main reason for rust_pgn_reader_python_binding being slower (at least in single threaded mode) than rust-pgn-reader is because of vector / string allocations (that show up as malloc's and free's in profiling).
21
+ To replicate, download `2013-07-train-00000-of-00001.parquet` and then run:
21
22
 
22
- To replicate, download `2013-07-train-00000-of-00001.parquet` and then run `python bench_parquet_parallel.py`
23
+ `python bench_parquet.py` (single-threaded parse_game)
24
+
25
+ `python bench_parquet_parallel.py` (multithreaded parse_games)
26
+
27
+ `python bench_parquet_arrow.py` (multithreaded parse_game_moves_arrow_chunked_array)
23
28
 
24
29
  ## Building
25
30
  `maturin develop`
31
+
26
32
  `maturin develop --release`
27
33
 
28
34
  For a more thorough tutorial, follow https://lukesalamone.github.io/posts/how-to-create-rust-python-bindings/
@@ -35,4 +41,5 @@ Linux/WSL-only:
35
41
 
36
42
  ## Testing
37
43
  `cargo test`
44
+
38
45
  `python -m unittest src/test.py`
@@ -6,7 +6,7 @@ use pyo3_arrow::PyChunkedArray;
6
6
  use rayon::prelude::*;
7
7
  use rayon::ThreadPoolBuilder;
8
8
  use shakmaty::Color;
9
- use shakmaty::{uci::UciMove, Chess, Outcome, Position, Square};
9
+ use shakmaty::{uci::UciMove, Chess, Outcome, Position, Role, Square};
10
10
  use std::io::Cursor;
11
11
 
12
12
  mod comment_parsing;
@@ -17,13 +17,13 @@ mod comment_parsing;
17
17
  pub struct PyUciMove {
18
18
  pub from_square: u8,
19
19
  pub to_square: u8,
20
- pub promotion: Option<char>,
20
+ pub promotion: Option<u8>,
21
21
  }
22
22
 
23
23
  #[pymethods]
24
24
  impl PyUciMove {
25
25
  #[new]
26
- fn new(from_square: u8, to_square: u8, promotion: Option<char>) -> Self {
26
+ fn new(from_square: u8, to_square: u8, promotion: Option<u8>) -> Self {
27
27
  PyUciMove {
28
28
  from_square,
29
29
  to_square,
@@ -41,24 +41,42 @@ impl PyUciMove {
41
41
  Square::new(self.to_square as u32).to_string()
42
42
  }
43
43
 
44
+ #[getter]
45
+ fn get_promotion_name(&self) -> Option<String> {
46
+ self.promotion.and_then(|p_u8| {
47
+ Role::try_from(p_u8)
48
+ .map(|role| format!("{:?}", role)) // Get the debug representation (e.g., "Queen")
49
+ .ok()
50
+ })
51
+ }
52
+
44
53
  // __str__ method for Python representation
45
54
  fn __str__(&self) -> String {
46
- let promo_char = self.promotion.map_or("".to_string(), |p| p.to_string());
55
+ let promo_str = self.promotion.map_or("".to_string(), |p_u8| {
56
+ Role::try_from(p_u8)
57
+ .map(|role| role.char().to_string())
58
+ .unwrap_or_else(|_| "".to_string()) // Handle potential error if u8 is not a valid Role
59
+ });
47
60
  format!(
48
61
  "{}{}{}",
49
62
  Square::new(self.from_square as u32),
50
63
  Square::new(self.to_square as u32),
51
- promo_char
64
+ promo_str
52
65
  )
53
66
  }
54
67
 
55
68
  // __repr__ for a more developer-friendly representation
56
69
  fn __repr__(&self) -> String {
70
+ let promo_repr = self.promotion.map_or("None".to_string(), |p_u8| {
71
+ Role::try_from(p_u8)
72
+ .map(|role| format!("Some('{}')", role.char()))
73
+ .unwrap_or_else(|_| format!("Some(InvalidRole({}))", p_u8))
74
+ });
57
75
  format!(
58
- "PyUciMove(from_square={}, to_square={}, promotion={:?})",
76
+ "PyUciMove(from_square={}, to_square={}, promotion={})",
59
77
  Square::new(self.from_square as u32),
60
78
  Square::new(self.to_square as u32),
61
- self.promotion
79
+ promo_repr
62
80
  )
63
81
  }
64
82
  }
@@ -211,7 +229,7 @@ impl Visitor for MoveExtractor {
211
229
  let py_uci_move = PyUciMove {
212
230
  from_square: from as u8,
213
231
  to_square: to as u8,
214
- promotion: promo_opt.map(|p| p.char()),
232
+ promotion: promo_opt.map(|p_role| p_role as u8),
215
233
  };
216
234
  self.moves.push(py_uci_move);
217
235
  self.push_castling_bitboards();
@@ -441,3 +459,74 @@ fn rust_pgn_reader_python_binding(m: &Bound<'_, PyModule>) -> PyResult<()> {
441
459
  m.add_class::<PyUciMove>()?;
442
460
  Ok(())
443
461
  }
462
+
463
+ #[cfg(test)]
464
+ mod pyucimove_tests {
465
+ use super::*;
466
+ use shakmaty::{Role, Square};
467
+
468
+ #[test]
469
+ fn test_py_uci_move_no_promotion() {
470
+ let uci_move = PyUciMove::new(Square::E2 as u8, Square::E4 as u8, None);
471
+ assert_eq!(uci_move.from_square, Square::E2 as u8);
472
+ assert_eq!(uci_move.to_square, Square::E4 as u8);
473
+ assert_eq!(uci_move.promotion, None);
474
+ assert_eq!(uci_move.get_from_square_name(), "e2");
475
+ assert_eq!(uci_move.get_to_square_name(), "e4");
476
+ assert_eq!(uci_move.get_promotion_name(), None);
477
+ assert_eq!(uci_move.__str__(), "e2e4");
478
+ assert_eq!(
479
+ uci_move.__repr__(),
480
+ "PyUciMove(from_square=e2, to_square=e4, promotion=None)"
481
+ );
482
+ }
483
+
484
+ #[test]
485
+ fn test_py_uci_move_with_queen_promotion() {
486
+ let uci_move = PyUciMove::new(Square::E7 as u8, Square::E8 as u8, Some(Role::Queen as u8));
487
+ assert_eq!(uci_move.from_square, Square::E7 as u8);
488
+ assert_eq!(uci_move.to_square, Square::E8 as u8);
489
+ assert_eq!(uci_move.promotion, Some(Role::Queen as u8));
490
+ assert_eq!(uci_move.get_from_square_name(), "e7");
491
+ assert_eq!(uci_move.get_to_square_name(), "e8");
492
+ assert_eq!(uci_move.get_promotion_name(), Some("Queen".to_string()));
493
+ assert_eq!(uci_move.__str__(), "e7e8q");
494
+ assert_eq!(
495
+ uci_move.__repr__(),
496
+ "PyUciMove(from_square=e7, to_square=e8, promotion=Some('q'))"
497
+ );
498
+ }
499
+
500
+ #[test]
501
+ fn test_py_uci_move_with_rook_promotion() {
502
+ let uci_move = PyUciMove::new(Square::A7 as u8, Square::A8 as u8, Some(Role::Rook as u8));
503
+ assert_eq!(uci_move.from_square, Square::A7 as u8);
504
+ assert_eq!(uci_move.to_square, Square::A8 as u8);
505
+ assert_eq!(uci_move.promotion, Some(Role::Rook as u8));
506
+ assert_eq!(uci_move.get_from_square_name(), "a7");
507
+ assert_eq!(uci_move.get_to_square_name(), "a8");
508
+ assert_eq!(uci_move.get_promotion_name(), Some("Rook".to_string()));
509
+ assert_eq!(uci_move.__str__(), "a7a8r");
510
+ assert_eq!(
511
+ uci_move.__repr__(),
512
+ "PyUciMove(from_square=a7, to_square=a8, promotion=Some('r'))"
513
+ );
514
+ }
515
+
516
+ #[test]
517
+ fn test_py_uci_move_invalid_promotion_val() {
518
+ // Test with a u8 value that doesn't correspond to a valid Role
519
+ let uci_move = PyUciMove::new(Square::B7 as u8, Square::B8 as u8, Some(99)); // 99 is not a valid Role
520
+ assert_eq!(uci_move.from_square, Square::B7 as u8);
521
+ assert_eq!(uci_move.to_square, Square::B8 as u8);
522
+ assert_eq!(uci_move.promotion, Some(99));
523
+ assert_eq!(uci_move.get_from_square_name(), "b7");
524
+ assert_eq!(uci_move.get_to_square_name(), "b8");
525
+ assert_eq!(uci_move.get_promotion_name(), None); // Should be None as 99 is invalid
526
+ assert_eq!(uci_move.__str__(), "b7b8"); // Should produce no promotion char
527
+ assert_eq!(
528
+ uci_move.__repr__(),
529
+ "PyUciMove(from_square=b7, to_square=b8, promotion=Some(InvalidRole(99)))"
530
+ );
531
+ }
532
+ }