Skip to content

Instantly share code, notes, and snippets.

@bemitc
Last active December 9, 2021 16:17
Show Gist options
  • Save bemitc/78971aca6e26eb4d8deb9a90756d22d0 to your computer and use it in GitHub Desktop.
Save bemitc/78971aca6e26eb4d8deb9a90756d22d0 to your computer and use it in GitHub Desktop.
count unique positions in a pgn file
#!/usr/bin/env python3
import sys
import chess.pgn
positions = []
class UniqPositionsVisitor(chess.pgn.BaseVisitor):
def visit_board(self, board):
if board.fen() not in positions:
positions.append(board.fen())
def result(self):
return True
for f in sys.argv[1:]:
pgn = open(f)
while True:
game = chess.pgn.read_game(pgn, Visitor=UniqPositionsVisitor)
if game == None:
break
pgn.close()
print(len(positions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment