Portable Game Notation#

class PGNDate(year: int | None, month: int | None, day: int | None)#

Represents a date for PGN. Has a year, month, and day.

__init__(year: int | None, month: int | None, day: int | None)#

Initializes a PGNDate with the given year, month, and day. Providing None for any of these fields indicates the term is unknown.

Parameters:
  • year (int | None) – The year field of this date.

  • month (int | None) – The month field of this date.

  • day (int | None) – The day field of this date.

Raise:

ValueError if the given year, month, or day cannot represent a valid date.

property year: int | None#

The year of this date, or None if not specified.

property month: int | None#

The month of this date, or None if not specified.

property day: int | None#

The day of this date, or None if not specified.

__str__() str#

Returns a str of this date in the form of YYYY.MM.DD

>>> str(PGNDate(1990, 4, 10))
'1990.04.10'
>>> str(PGNDate(2015, 7, None))
'2015.07.??'
>>> str(PGNDate(None, None, None))
'????.??.??'
__repr__() str#
__eq__(other: Any) bool#

Returns True if compared with an identical PGNDate

__le__(other: PGNDate) bool#

Returns True if compared with an earlier or equivalent PGNDate

__ge__(other: PGNDate) bool#

Returns True if compared with a later or equivalent PGNDate

__lt__(other: PGNDate) bool#

Returns True if compared with an earlier PGNDate

__gt__(other: PGNDate) bool#

Returns True if compared with a later PGNDate

__hash__() int#
class PGNResult#
static from_str(result_str: str) PGNResult#

Returns the PGNResult corresponding to the given str. Any str besides “1-0”, “0-1”, or “1/2-1/2” corresponds to an Unknown Result.

>>> PGNResult.from_str("1-0") is WHITE_WON
True
>>> PGNResult.from_str("0-1") is BLACK_WON
True
>>> PGNResult.from_str("1/2-1/2") is DRAW_RESULT
True
>>> PGNResult.from_str("*") is UNKNOWN_RESULT
True
>>> PGNResult.from_str("or anything else") is UNKNOWN_RESULT
True
property winner: Color | None#

Returns the Color of the winner, if this result indicates one.

property is_draw: bool#

Returns True if this result is a draw.

property is_unknown: bool#

Returns True if this result is unknown.

__eq__(other: Any) bool#
__str__() str#

Returns a str of the PGN format of this result.

>>> str(WHITE_WON)
'1-0'
>>> str(BLACK_WON)
'0-1'
>>> str(DRAW_RESULT)
'1/2-1/2'
>>> str(UNKNOWN_RESULT)
'*'
__repr__() str#
__hash__() int#
WHITE_WON: PGNResult#
BLACK_WON: PGNResult#
DRAW_RESULT: PGNResult#
UNKNOWN_RESULT: PGNResult#
class PGNGame#
property event: str#

The contents of the “Event” tag, as a str.

property site: str#

The contents of the “Site” tag, as a str.

property round: str#

The contents of the “Round” tag, as a str.

property date: PGNDate#

A PGNResult formed from the “Date” tag. Alternatively looks at “UTCDate” as a fallback. If neither of these are provided, the year, month, and day are marked as unknown.

property white_player: str#

The contents of the “White” tag, as a str.

property black_player: str#

The contents of the “Black” tag, as a str.

property result: PGNResult#

A PGNResult formed from the “Result” tag. If this field is malformed or not provided, an unknown result is returned.

property move_count: int#

The number of moves played in this game.

property moves: list[Move]#

A list of moves played in this game.

property starting_board: Board#

The starting position of this game. Determined by looking at the “FEN” tag, if it is provided. Otherwise, is the same as the standard starting position.

__getitem__(tag: str) str | None#

Gets the raw str value of the given tag. If the tag is absent, returns None.

__hash__() int#
__eq__(other: Any) bool#
class PGNFile#
static open(path: str) PGNFile#

Opens a PGN file for reading.

Raises:

FileNotFoundError If the given path does not lead to a file.

close() None#

Closes a PGN file. Closing an already closed file has no effect.

is_open() bool#

Returns True if this file is open.

next_game() PGNGame | None#

Gets the next game from a file as a PGNGame If the file is exhausted of games, returns None.

Raises:

ValueError if an error is found while parsing.

skip_game() None#

Skips over the next game in a file.

__enter__() PGNFile#
__exit__(exc_type, exc_val, exc_tb) None#