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
File details
Details for the file pypaya_pgn_parser-0.1.1.tar.gz
.
File metadata
- Download URL: pypaya_pgn_parser-0.1.1.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b1e436d90ee24e7d8e50c734d0a23939bd23d6b0c9469106107bc202da4c12f |
|
MD5 | 09e23d4c466a1b8042329c8d51c7b288 |
|
BLAKE2b-256 | e9753d82c32594629d366f27d31be994c700ac923a8ddbeb260b3bfc3b0a7754 |
File details
Details for the file pypaya_pgn_parser-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: pypaya_pgn_parser-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8ae9bb910d30567816452b53976e231ded79fef3ff8b58a7ebe61b3674b8652 |
|
MD5 | 0b4a651a693d2beded9ebb413a0d6c0e |
|
BLAKE2b-256 | 9f05324b38f6933dda1fc2556b55424bb5a5bfe3dffbede70fcdff2cd656c872 |