A lightweight Python package that provides efficient parsing capabilities for Portable Game Notation (PGN) chess files
Project description
pypaya-pgn-parser
A lightweight Python package that provides efficient parsing capabilities for Portable Game Notation (PGN) chess files.
Features
- Fast and memory-efficient PGN parsing
- Robust handling of various PGN formats and edge cases
- Easy-to-use API for integrating into your chess applications
- No external dependencies for core functionality
Installation
You can install pypaya-pgn-parser using pip:
pip install pypaya-pgn-parser
Usage
Here's a quick example of how to use pypaya-pgn-parser:
from io import StringIO
from pypaya_pgn_parser.pgn_parser import PGNParser
def process_pgn_file(file_path):
# Initialize the parser
parser = PGNParser()
# Read the entire file into a StringIO object
with open(file_path, 'r') as file:
file_content = file.read()
pgn_stringio = StringIO(file_content)
game_count = 0
while True:
# Parse the next game
result = parser.parse(pgn_stringio)
# Check if we've reached the end of the file or encountered an error
if result is None:
print(f"Reached end of file or encountered an error after processing {game_count} games.")
break
game_info, game_moves = result
# Process the game
process_game(game_count, game_info, game_moves)
game_count += 1
print(f"Total games processed: {game_count}")
def process_game(game_number, game_info, game_moves):
print(f"\nGame {game_number + 1} information:")
for header, value in zip(["Event", "Site", "Date", "Round", "White", "Black", "Result"], game_info):
print(f"{header}: {value}")
print("\nMoves:")
print(game_moves)
# You can add more processing here, such as analyzing the moves,
# storing the data, or any other operation you need to perform on each game
# Usage
file_path = "example.pgn"
process_pgn_file(file_path)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for pypaya_pgn_parser-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8ae9bb910d30567816452b53976e231ded79fef3ff8b58a7ebe61b3674b8652 |
|
MD5 | 0b4a651a693d2beded9ebb413a0d6c0e |
|
BLAKE2b-256 | 9f05324b38f6933dda1fc2556b55424bb5a5bfe3dffbede70fcdff2cd656c872 |