Polyglot opening book reading¶
-
chess.polyglot.
open_reader
(path: PathLike) → MemoryMappedReader[source]¶ 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
[source]¶ An entry from a Polyglot opening book.
-
key
: int¶ The Zobrist hash of the position.
-
weight
: int¶ An integer value that can be used as the weight for this entry.
-
learn
: int¶ Another integer value that can be used for extra information.
-
-
class
chess.polyglot.
MemoryMappedReader
(filename: PathLike)[source]¶ Maps a Polyglot opening book to memory.
-
find_all
(board: Union[chess.Board, int], *, minimum_weight: int = 1, exclude_moves: Container[chess.Move] = []) → Iterator[Entry][source]¶ Seeks a specific position and yields corresponding entries.
-
find
(board: Union[chess.Board, int], *, minimum_weight: int = 1, exclude_moves: Container[chess.Move] = []) → Entry[source]¶ 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_weight0
to select all entries.- Raises
IndexError
if no entries are found. Useget()
if you prefer to getNone
instead of an exception.
-
choice
(board: Union[chess.Board, int], *, minimum_weight: int = 1, exclude_moves: Container[chess.Move] = [], random=<module 'random' from '/home/docs/checkouts/readthedocs.org/user_builds/python-chess/envs/v0.31.4/lib/python3.8/random.py'>) → Entry[source]¶ Uniformly selects a random entry for the given position.
- Raises
IndexError
if no entries are found.
-
weighted_choice
(board: Union[chess.Board, int], *, exclude_moves: Container[chess.Move] = [], random=<module 'random' from '/home/docs/checkouts/readthedocs.org/user_builds/python-chess/envs/v0.31.4/lib/python3.8/random.py'>) → Entry[source]¶ Selects a random entry for the given position, distributed by the weights of the entries.
- Raises
IndexError
if no entries are found.
-
-
chess.polyglot.
POLYGLOT_RANDOM_ARRAY
= [0x9D39247E33776D41, ..., 0xF8D626AAAF278509]¶ Array of 781 polyglot compatible pseudo random values for Zobrist hashing.
-
chess.polyglot.
zobrist_hash
(board: chess.Board, *, _hasher: Callable[[chess.Board], int] = <chess.polyglot.ZobristHasher object>) → int[source]¶ 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.