Polyglot opening book reading

chess.polyglot.open_reader(path)

Creates a reader for the file at the given path.

The following example opens a book to find all entries for the start position:

>>> import chess
>>> import chess.polyglot
>>>
>>> board = chess.Board()
>>>
>>> with chess.polyglot.open_reader("data/polyglot/performance.bin") as reader:
...    for entry in reader.find_all(board):
...        print(entry.move(), entry.weight, entry.learn)
e2e4 1 0
d2d4 1 0
c2c4 1 0
class chess.polyglot.Entry

An entry from a Polyglot opening book.

key

The Zobrist hash of the position.

raw_move

The raw binary representation of the move. Use the move() method to extract a move object from this.

weight

An integer value that can be used as the weight for this entry.

learn

Another integer value that can be used for extra information.

move(chess960=False)

Gets the move (as a Move object).

class chess.polyglot.MemoryMappedReader(filename)

Maps a Polyglot opening book to memory.

find_all(board, minimum_weight=1, exclude_moves=())

Seeks a specific position and yields corresponding entries.

find(board, minimum_weight=1, exclude_moves=())

Finds the main entry for the given position or Zobrist hash.

The main entry is the first entry with the highest weight.

By default entries with weight 0 are excluded. This is a common way to delete entries from an opening book without compacting it. Pass minimum_weight 0 to select all entries.

Raises:IndexError if no entries are found. Use get() if you prefer to get None instead of an exception.
choice(board, minimum_weight=1, exclude_moves=(), random=<module 'random' from '/home/docs/checkouts/readthedocs.org/user_builds/python-chess/envs/v0.23.6/lib/python3.5/random.py'>)

Uniformly selects a random entry for the given position.

Raises:IndexError if no entries are found.
weighted_choice(board, exclude_moves=(), random=<module 'random' from '/home/docs/checkouts/readthedocs.org/user_builds/python-chess/envs/v0.23.6/lib/python3.5/random.py'>)

Selects a random entry for the given position, distributed by the weights of the entries.

Raises:IndexError if no entries are found.
close()

Closes the reader.

chess.polyglot.POLYGLOT_RANDOM_ARRAY = [0x9D39247E33776D41, ..., 0xF8D626AAAF278509]

Array of 781 polyglot compatible pseudo random values for Zobrist hashing.

chess.polyglot.zobrist_hash(board, _hasher=<chess.polyglot.ZobristHasher object>)

Calculates the Polyglot Zobrist hash of the position.

A Zobrist hash is an XOR of pseudo-random values picked from an array. Which values are picked is decided by features of the position, such as piece positions, castling rights and en passant squares.