Exemple #1
0
def make_dfs(filename, playername):

    pgn_text = open(filename).read()
    pgn_game = pgn.PGNGame()

    dat = pgn.loads(pgn_text)  # Returns a list of PGNGame
    sdat = pgn.dumps(pgn_game)  # Returns a string with a pgn game

    f_g = dat[0]
    games = list()

    for game in dat:
        keys = [key for key in dir(game) if not '_' in key]
        game_dict = dict()
        for key in keys:
            game_dict[key] = game.__getattribute__(key)
        games.append(game_dict)

    df = pd.DataFrame(games)
    split_timecontrol(df)

    df.whiteelo = pd.to_numeric(df.whiteelo, errors='coerce')
    df.blackelo = pd.to_numeric(df.blackelo, errors='coerce')

    #df.dropna(inplace=True)

    me_black = df.loc[df.black == playername]
    me_white = df.loc[df.white == playername]

    return me_black, me_white, df
Exemple #2
0
    def test_dumps_special(self):
        '''Tests ``dumps`` function with move commentary and null tag'''
        game = pgn.PGNGame('XYZ')
        game.moves = ['{comment}', 'e4', 'e5', '{in}', 'd4', '{lol}', '1-0']

        dump = pgn.dumps(game)
        first_expected = '[Event "XYZ"]\n[Site "?"]'
        last_expected = '{comment} 1. e4 e5 {in} 2. d4 {lol} 1-0'
        
        assert dump.startswith(first_expected)
        assert dump.endswith(last_expected)
Exemple #3
0
def get_pgn():
    global white_name, black_name

    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    p.wait()
    pgn_text = p.stdout.read().replace('\r', '\n')
    game = pgn.PGNGame()
    game = pgn.loads(pgn_text)[0]
    white_name = make_comparable(game.white)
    black_name = make_comparable(game.black)

    moves = strip_pgn(game)
    return moves
Exemple #4
0
def get_pgn():
    global white_player
    global black_player

    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    p.wait()
    pgn_text = p.stdout.read().replace('\r','\n')
    game = pgn.PGNGame()
    game = pgn.loads(pgn_text)[0]

    white_player = game.white.split(',')[0]
    black_player = game.black.split(',')[0]

    moves = strip_pgn(game)
    return moves
Exemple #5
0
def game_fixture():
    game = pgn.PGNGame(
        'F/S Return Match',
        'Belgrade, Serbia Yugoslavia|JUG',
        '1992.11.04',
        '29',
        'Fischer, Robert J.',
        'Spassky, Boris V.',
        '1/2-1/2'
    )

    game.annotator = 'Renato'
    game.plycount = '3'
    game.timecontrol = '40/7200:3600'
    game.time = '12:32:43'
    game.termination = 'abandoned'
    game.mode = 'ICS'
    game.fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/R1BQKBNR'

    game.moves = ['e4', 'e5', 'd4', 'd5', 'f3', '1/2-1/2']

    return game
Exemple #6
0
import pgn
import glob

pgn_text = ''
print('Opening pgn files')

# the example on https://github.com/renatopp/pgnparser for large pgn files did not work
# as a result, this is pretty slow (even with only 1 1mb pgn file)

# thanks to http://stackoverflow.com/questions/5013532/open-file-by-filename-wildcard
path = "data/*.pgn"
for filename in glob.glob(path):
    with open(filename, 'r') as f:
        pgn_text += f.read()

pgn_games = pgn.PGNGame()
games = pgn.loads(pgn_text)
number_of_games = len(games)

print(str(number_of_games) + " games loaded.\n")

results = map(lambda x: x.result, games)


def win_rate(result):
    return (float(len(filter(lambda x: x == result, results))) /
            number_of_games) * 100


white_wins = win_rate('1-0')
black_wins = win_rate('0-1')
# -*- coding: utf-8 -*-
# @Time    : 2018/1/26 17:21
# @Author  : play4fun
# @File    : pgnparser1.py
# @Software: PyCharm
"""
pgnparser1.py:
"""

import pgn

# pgn_text = open('morphy.pgn').read()
pgn_text = open('example.pgn').read()
pgn_game = pgn.PGNGame()

x = pgn.loads(pgn_text)
print(x)  # Returns a list of PGNGame
# [<PGNGame "Fischer, Robert J." vs "Spassky, Boris V.">]
d = pgn.dumps(pgn_game)
print(d)  # Returns a string with a pgn game
'''
[<PGNGame "Fischer, Robert J." vs "Spassky, Boris V.">]
[Event "?"]
[Site "?"]
[Date "?"]
[Round "?"]
[White "?"]
[Black "?"]
[Result "?"]
'''
Exemple #8
0
def open_file(fi):
    pgn_text = open(fi).read()
    pgn_game = pgn.PGNGame(pgn_text)

    #print pgn.loads(pgn_text)
    print pgn.dumps(pgn_game)