Skip to main content

PGN Parser

A Python library for parsing pgn files into a python friendly format.

The parser is built using canopy, the rest is Python.

The PGN spec is based on (and thanks to) the spec at saremba.de.

Setup

Installing

Make sure you have python 3 installed.

pip install pgn_parser

Then import like so:

from pgn_parser import pgn, parser

Testing

The tests are written using pytest and behave, these must be installed first:

pip install pytest behave

For running unit tests:

pytest

For running behavioural tests:

behave

Building pip distributables

make build

Using

Parsing a pgn file

To parse a pgn, you just give the string to the parser.parse along with the Actions() which the parser uses to create python structures.

>>> from pgn_parser import parser, pgn

>>> game = parser.parse("1. e4 e5", actions=pgn.Actions())
>>> print(game.move(1))
1. e4 e5
>>> print(game.move(1).black.san)
e5

Games

After parsing a game, it will be structured into the following classes which are nested in eachother:

Game: Container for the whole game To get a specific move (5 here) from a game

game.move(5)

To retrieve the Movetext

game.movetext

To access the TagPairs

game.tag_pairs

To access the final score

game.score

Movetext: The container of all the moves, e.g "1. c4 c5 2. e4 e5" It is just a list so can be iterated over to retrieve the moves. Be warned, Movetext[0] will be the first move parsed, whether 1. or 31. so use Game.move() if you want a movenumber

Move: A move is a move number, optionally a white Ply and or a black Ply

Ply: Is the unit of moving, in standard algebraic notation (SAN), e.g. the black ply from "1.e4 e5" is e5

TagPairs: An ordered dictionary of all TagPair objects. These are ordered so it keeps the order read in, but will change to seven tag roster order if printed/stringified.

TagPairs

To store meta data about a game you do so in TagPairs

The header of a pgn file

["Site" "github.com"]

Is represented like so in python

game.tag_pairs["Site"] == "github.com"

Moves

Each move has a move number and two ply's, white and black. Each ply can be anything from empty to having comments, variations and nags.

moves = "1. e4 $1 {a comment} (1.d5)"

Is represented like so:

m1 = game.move(1)

assert m1.white.san == "e4"
assert m1.white.comment == "a comment"
assert m1.white.nags[0] == "$1"
assert m1.white.variations[0].move(1).white.san == "d5"

If a ply is empty, then its san will be represented "".

Limitations

No support for RAV style variations No support for multiple games in one parse, must be single games Doesn't attempt to parse turn times as this is not in the original spec and I am not sure what to support.

Authors

  • Brett Bates - Initial work - github

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Metadata

Release files for pgn-parser 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pgn-parser 1.1.0
File Size Uploaded Provenance Actions
pgn-parser-1.1.0.tar.gz 12.5 kB Without provenance Details for pgn-parser-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pgn-parser 1.1.0
File Interpreter ABI Platform Actions
pgn_parser-1.1.0-py3-none-any.whl Python 3 none any Details for pgn_parser-1.1.0-py3-none-any.whl

Total release size: 24.7 kB

Release files / pgn-parser-1.1.0.tar.gz

Download URL pgn-parser-1.1.0.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
58504a2444cfb8461d58429b8568a1cca514233b12a4b2becfb1cf7f87c9fb60
BLAKE2b-256 checksum
How to use checksums
faf9c447b0ba6a6b710ae4e97d7cfa02c945e18f95bf10bcf4f33068b3823d5c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.6.9

Release files / pgn_parser-1.1.0-py3-none-any.whl

Download URL pgn_parser-1.1.0-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c5a73cb7af56a91e1aa9c2376f463a22f4be4006f49186339f32175a0ba9a320
BLAKE2b-256 checksum
How to use checksums
40ae34194276b66f81c7905783fd9eba95b6dfd909d0887fa52ee65f2d532b36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/3.6.9

Security for pgn-parser 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page