-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
Hey!
Thanks a-lot!
I had a feature request: please perserve comments.
I have no idea how to suggest a change to the canonical file in github, I coded the following:
# Author: Aven Bross
#
# Description: A simple tool to merge several pgn games into a single game with
# variations.
import chess.pgn
import sys
def main():
try:
args = sys.argv[1:]
except IndexError:
raise SystemExit(f"Usage: {sys.argv[0]} <string_to_reverse>")
games = []
for name in args:
pgn = open(name, encoding="utf-8-sig")
game = chess.pgn.read_game(pgn)
while game is not None:
games.append(game)
game = chess.pgn.read_game(pgn)
master_node = chess.pgn.Game()
mlist = []
for game in games:
mlist.extend(game.variations)
variations = [(master_node, mlist)]
done = False
while not done:
newvars = []
done = True
for vnode, nodes in variations:
newmoves = {} # Maps move to its index in newvars.
for node in nodes:
if node.move is None:
continue
elif node.move not in list(newmoves):
nvnode = vnode.add_variation(node.move)
# Grab the first comment to appear.
if not nvnode.comment:
nvnode.comment = node.comment
if len(node.variations) > 0:
done = False
newvars.append((nvnode, node.variations))
newmoves[node.move] = len(newvars) - 1
else:
nvnode, nlist = newvars[newmoves[node.move]]
if len(node.variations) > 0:
done = False
nlist.extend(node.variations)
newvars[newmoves[node.move]] = (nvnode, nlist)
variations = newvars
print(master_node)
main()
As you can see it's actually 3 lines mutated.
So thank you for providing something so easy to be free for use.
Metadata
Metadata
Assignees
Labels
No labels