From 23f543c6e5fc387ad4a385aa5060539d7f56e988 Mon Sep 17 00:00:00 2001 From: Karol Brejna Date: Thu, 25 Mar 2021 11:08:21 +0100 Subject: [PATCH] Remove unused import; resolve some python code style violations --- .gitignore | 3 ++ download_games.py | 16 +++----- download_tourments.py | 30 ++++++-------- main.py | 38 +++++++++--------- modules/api/api.py | 6 +-- modules/bcolors/bcolors.py | 2 +- modules/fishnet/fishnet.py | 3 +- modules/investigate/investigate.py | 34 ++++++++-------- modules/puzzle/analysed.py | 2 +- modules/puzzle/position_list.py | 63 ++++++++++++++++-------------- modules/puzzle/puzzle.py | 34 ++++++++-------- opening_counter.py | 22 ++++------- 12 files changed, 119 insertions(+), 134 deletions(-) diff --git a/.gitignore b/.gitignore index 617fbb9..9e8b7fa 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ Stockfish slack_key.txt venv *pgn +.idea/ +.venv +.DS_Store diff --git a/download_games.py b/download_games.py index 81c846c..71ce254 100644 --- a/download_games.py +++ b/download_games.py @@ -3,32 +3,28 @@ """Downloading chess puzzles for lichess.org""" import argparse -import chess -import chess.pgn import logging -import os import sys + import requests -import chess -import re -import time parser = argparse.ArgumentParser(description=__doc__) -parser.add_argument("--token", metavar="TOKEN",default="", +parser.add_argument("--token", metavar="TOKEN", default="", help="secret token for the lichess api") parser.add_argument("username", metavar="USERNAME", help="Username in lichess") parser.add_argument("--quiet", dest="loglevel", default=logging.DEBUG, action="store_const", const=logging.INFO, help="substantially reduce the number of logged messages") -parser.add_argument("--max", metavar="MAX",default="60", +parser.add_argument("--max", metavar="MAX", default="60", help="max number of games") settings = parser.parse_args() logging.basicConfig(format="%(message)s", level=settings.loglevel, stream=sys.stdout) -logging.debug("Downloading games from: "+settings.username) +logging.debug("Downloading games from: " + settings.username) -response = requests.get('https://lichess.org/api/games/user/'+settings.username+'?max='+settings.max+'&token=' + settings.token+'&perfType=blitz,rapid,classical&opening=true') +response = requests.get( + 'https://lichess.org/api/games/user/' + settings.username + '?max=' + settings.max + '&token=' + settings.token + '&perfType=blitz,rapid,classical&opening=true') pgn = str(response.text) all_games = open("games.pgn", "w") all_games.write(pgn) diff --git a/download_tourments.py b/download_tourments.py index 1fb2e3b..53af32c 100644 --- a/download_tourments.py +++ b/download_tourments.py @@ -2,34 +2,26 @@ """Downloading chess puzzles for lichess.org""" -import argparse -import chess -import chess.pgn import logging -import os -import sys -import requests -import chess -import re -import time +import requests # tourments tourment_ids = ['25MtoToy', -'E14kHVwX', -'tdntXNhy', -'sj5GoEdS', -'C4zdQLax', -'wobqi6QP', -'T4RW1ux2', -'nzw7OKBq'] + 'E14kHVwX', + 'tdntXNhy', + 'sj5GoEdS', + 'C4zdQLax', + 'wobqi6QP', + 'T4RW1ux2', + 'nzw7OKBq'] all_games = open("games.pgn", "w") pgn = "" for id in tourment_ids: - print ('https://lichess.org/api/tournament/'+id+'/games') - response = requests.get('https://lichess.org/api/tournament/'+id+'/games') - pgn = pgn +'\n'+ str(response.text) + print('https://lichess.org/api/tournament/' + id + '/games') + response = requests.get('https://lichess.org/api/tournament/' + id + '/games') + pgn = pgn + '\n' + str(response.text) all_games.write(pgn) all_games.close() diff --git a/main.py b/main.py index 73780e8..4a89ba4 100755 --- a/main.py +++ b/main.py @@ -3,17 +3,17 @@ """Creating chess puzzles for lichess.org""" import argparse -import chess -import chess.uci -import chess.pgn import logging -import os import sys -from modules.fishnet.fishnet import stockfish_command -from modules.puzzle.puzzle import puzzle + +import chess.pgn +import chess.uci + +from modules.api.api import post_puzzle from modules.bcolors.bcolors import bcolors +from modules.fishnet.fishnet import stockfish_command from modules.investigate.investigate import investigate -from modules.api.api import post_puzzle +from modules.puzzle.puzzle import puzzle parser = argparse.ArgumentParser(description=__doc__) @@ -35,6 +35,7 @@ # Optionally fix colors on Windows and in journals if the colorama module # is available. import colorama + wrapper = colorama.AnsiToWin32(sys.stdout) if wrapper.should_wrap(): sys.stdout = wrapper.stream @@ -56,26 +57,26 @@ game_id = 0 while True: game = chess.pgn.read_game(all_games) - if game == None: + if game is None: break node = game - game_id = game_id + 1 + game_id = game_id + 1 logging.debug(bcolors.WARNING + "Game ID: " + str(game_id) + bcolors.ENDC) - logging.debug(bcolors.WARNING + "Game headers: " + str(game) + bcolors.ENDC) - + logging.debug(bcolors.WARNING + "Game headers: " + str(game) + bcolors.ENDC) + prev_score = chess.uci.Score(None, None) puzzles = [] - + logging.debug(bcolors.OKGREEN + "Game Length: " + str(game.end().board().fullmove_number)) logging.debug("Analysing Game..." + bcolors.ENDC) - + engine.ucinewgame() - + while not node.is_end(): next_node = node.variation(0) engine.position(next_node.board()) - + engine.go(depth=settings.depth) cur_score = info_handler.info["score"][1] logging.debug(bcolors.OKGREEN + node.board().san(next_node.move) + bcolors.ENDC) @@ -83,11 +84,12 @@ logging.debug(" Mate: " + str(cur_score.mate) + bcolors.ENDC) if investigate(prev_score, cur_score, node.board()): logging.debug(bcolors.WARNING + " Investigate!" + bcolors.ENDC) - puzzles.append(puzzle(node.board(), next_node.move, str(game_id), engine, info_handler, game, settings.strict)) - + puzzles.append( + puzzle(node.board(), next_node.move, str(game_id), engine, info_handler, game, settings.strict)) + prev_score = cur_score node = next_node - + for i in puzzles: logging.debug(bcolors.WARNING + "Generating new puzzle..." + bcolors.ENDC) i.generate(settings.depth) diff --git a/modules/api/api.py b/modules/api/api.py index 151690e..087cd83 100644 --- a/modules/api/api.py +++ b/modules/api/api.py @@ -1,10 +1,8 @@ -import requests -import chess import logging -import re -import time + from modules.bcolors.bcolors import bcolors + def post_puzzle(puzzle): logging.debug(bcolors.WARNING + "NEW PUZZLE GENERATED" + bcolors.ENDC) logging.info(bcolors.OKBLUE + str(puzzle.to_pgn()) + bcolors.ENDC) diff --git a/modules/bcolors/bcolors.py b/modules/bcolors/bcolors.py index 74ec987..f22168e 100644 --- a/modules/bcolors/bcolors.py +++ b/modules/bcolors/bcolors.py @@ -6,4 +6,4 @@ class bcolors: FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' - UNDERLINE = '\033[4m' \ No newline at end of file + UNDERLINE = '\033[4m' diff --git a/modules/fishnet/fishnet.py b/modules/fishnet/fishnet.py index 9635983..7c0ba93 100644 --- a/modules/fishnet/fishnet.py +++ b/modules/fishnet/fishnet.py @@ -59,8 +59,7 @@ def stockfish_filename(): def update_stockfish(filename): print("Looking up %s ..." % filename) - headers = {} - headers["User-Agent"] = "Python-Puzzle-Generator" + headers = {"User-Agent": "Python-Puzzle-Generator"} # Only update to newer versions try: diff --git a/modules/investigate/investigate.py b/modules/investigate/investigate.py index 58710ee..078c74e 100644 --- a/modules/investigate/investigate.py +++ b/modules/investigate/investigate.py @@ -1,35 +1,33 @@ import chess + def sign(a): - if a > 0: - return 1 - elif a < 0: - return -1 - else: - return 0 + return (a > 0) - (a < 0) + def material_value(board): - return sum(v * (len(board.pieces(pt, True)) + len(board.pieces(pt, False))) for v, pt in zip([0,3,3,5.5,9], chess.PIECE_TYPES)) + return sum(v * (len(board.pieces(pt, True)) + len(board.pieces(pt, False))) for v, pt in + zip([0, 3, 3, 5.5, 9], chess.PIECE_TYPES)) + def material_count(board): return chess.popcount(board.occupied) + def investigate(a, b, board): # determine if the difference between position A and B # is worth investigating for a puzzle. if a.cp is not None and b.cp is not None: - if (((a.cp > -110 and a.cp < 850 and b.cp > 200 and b.cp < 850) - or (a.cp > -850 and a.cp < 110 and b.cp < -200 and b.cp > -850)) - and material_value(board) > 3 - and material_count(board) > 6): + if (((-110 < a.cp < 850 and 200 < b.cp < 850) + or (-850 < a.cp < 110 and -200 > b.cp > -850)) + and material_value(board) > 3 + and material_count(board) > 6): return True - elif (a.cp is not None - and b.mate is not None - and material_value(board) > 3): - if ((a.cp < 110 and sign(b.mate) == -1) or (a.cp > -110 and sign(b.mate) == 1) ): + elif a.cp is not None and b.mate is not None and material_value(board) > 3: + if (a.cp < 110 and sign(b.mate) == -1) or (a.cp > -110 and sign(b.mate) == 1): return True elif (a.mate is not None - and b.mate is not None): - if sign(a.mate) == sign(b.mate): #actually means that they're opposite + and b.mate is not None): + if sign(a.mate) == sign(b.mate): # actually means that they're opposite return True - return False \ No newline at end of file + return False diff --git a/modules/puzzle/analysed.py b/modules/puzzle/analysed.py index 64de1e5..934c2f9 100644 --- a/modules/puzzle/analysed.py +++ b/modules/puzzle/analysed.py @@ -15,4 +15,4 @@ def sort_val(self): elif self.evaluation.mate is not None: return self.sign(self.evaluation.mate) * (abs(100 + self.evaluation.mate)) * 10000 else: - return 0 \ No newline at end of file + return 0 diff --git a/modules/puzzle/position_list.py b/modules/puzzle/position_list.py index 21ab5c7..8f6b1d3 100644 --- a/modules/puzzle/position_list.py +++ b/modules/puzzle/position_list.py @@ -1,13 +1,15 @@ +import logging +from operator import methodcaller + import chess import chess.uci -import logging -import os + from modules.bcolors.bcolors import bcolors from modules.puzzle.analysed import analysed -from operator import methodcaller + class position_list: - def __init__(self, position, engine, info_handler, player_turn=True, best_move=None, evaluation=None, strict = True): + def __init__(self, position, engine, info_handler, player_turn=True, best_move=None, evaluation=None, strict=True): self.position = position.copy() self.engine = engine self.info_handler = info_handler @@ -61,10 +63,10 @@ def evaluate_best(self, depth): if self.best_move.bestmove is not None: self.evaluation = self.info_handler.info["score"][1] self.next_position = position_list(self.position.copy(), - self.engine, - self.info_handler, - not self.player_turn, - strict = self.strict) + self.engine, + self.info_handler, + not self.player_turn, + strict=self.strict) self.next_position.position.push(self.best_move.bestmove) logging.debug("Best Move: " + self.best_move.bestmove.uci() + bcolors.ENDC) logging.debug(bcolors.OKBLUE + " CP: " + str(self.evaluation.cp)) @@ -90,7 +92,8 @@ def evaluate_legals(self, depth): logging.debug("... and " + str(max(0, len(self.analysed_legals) - 3)) + " more moves" + bcolors.ENDC) def material_difference(self): - return sum(v * (len(self.position.pieces(pt, True)) - len(self.position.pieces(pt, False))) for v, pt in zip([0,3,3,5.5,9], chess.PIECE_TYPES)) + return sum(v * (len(self.position.pieces(pt, True)) - len(self.position.pieces(pt, False))) for v, pt in + zip([0, 3, 3, 5.5, 9], chess.PIECE_TYPES)) def material_count(self): return chess.popcount(self.position.occupied) @@ -98,25 +101,25 @@ def material_count(self): def is_complete(self, category, color, first_node, first_val): if self.next_position is not None: if ((category == 'Mate' and not self.ambiguous()) - or (category == 'Material' and self.next_position.next_position is not None)): + or (category == 'Material' and self.next_position.next_position is not None)): return self.next_position.is_complete(category, color, False, first_val) - + if category == 'Material': if color: - if (self.material_difference() > 0.2 - and abs(self.material_difference() - first_val) > 0.1 - and first_val < 2 - and self.evaluation.mate is None - and self.material_count() > 6): + if (self.material_difference() > 0.2 + and abs(self.material_difference() - first_val) > 0.1 + and first_val < 2 + and self.evaluation.mate is None + and self.material_count() > 6): return True else: return False else: - if (self.material_difference() < -0.2 - and abs(self.material_difference() - first_val) > 0.1 - and first_val > -2 - and self.evaluation.mate is None - and self.material_count() > 6): + if (self.material_difference() < -0.2 + and abs(self.material_difference() - first_val) > 0.1 + and first_val > -2 + and self.evaluation.mate is None + and self.material_count() > 6): return True else: return False @@ -128,22 +131,22 @@ def is_complete(self, category, color, first_node, first_val): def ambiguous(self): # If strict == False then it will generate more tactics but more ambiguous - move_number = 1 if self.strict == True else 2 + move_number = 1 if self.strict else 2 if len(self.analysed_legals) > 1: if (self.analysed_legals[0].evaluation.cp is not None - and self.analysed_legals[1].evaluation.cp is not None): + and self.analysed_legals[1].evaluation.cp is not None): if (self.analysed_legals[0].evaluation.cp > -210 - or self.analysed_legals[move_number].evaluation.cp < -90): + or self.analysed_legals[move_number].evaluation.cp < -90): return True if (self.analysed_legals[0].evaluation.mate is not None - and self.analysed_legals[1].evaluation.mate is not None): + and self.analysed_legals[1].evaluation.mate is not None): if (self.analysed_legals[0].evaluation.mate < 1 - and self.analysed_legals[1].evaluation.mate < 1): - return True - if (self.analysed_legals[0].evaluation.mate is not None - and self.analysed_legals[1].evaluation.cp is not None): - if (self.analysed_legals[1].evaluation.cp < -200): + and self.analysed_legals[1].evaluation.mate < 1): return True + if self.analysed_legals[0].evaluation.mate is None or self.analysed_legals[1].evaluation.cp is None: + return + if self.analysed_legals[1].evaluation.cp < -200: + return True return False def game_over(self): diff --git a/modules/puzzle/puzzle.py b/modules/puzzle/puzzle.py index 00e5329..cd40f6d 100644 --- a/modules/puzzle/puzzle.py +++ b/modules/puzzle/puzzle.py @@ -1,17 +1,19 @@ -from modules.puzzle.position_list import position_list -from modules.bcolors.bcolors import bcolors -import json import logging -import os + import chess import chess.pgn + +from modules.bcolors.bcolors import bcolors +from modules.puzzle.position_list import position_list + + class puzzle: def __init__(self, last_pos, last_move, game_id, engine, info_handler, game, strict): self.last_pos = last_pos.copy() self.last_move = last_move self.game_id = game_id last_pos.push(last_move) - self.positions = position_list(last_pos, engine, info_handler, strict = strict) + self.positions = position_list(last_pos, engine, info_handler, strict=strict) self.game = game def to_dict(self): @@ -21,16 +23,16 @@ def to_dict(self): 'last_pos': self.last_pos.fen(), 'last_move': self.last_move.uci(), 'move_list': self.positions.move_list() - } + } def to_pgn(self): fen = self.last_pos.fen() board = chess.Board(fen) game = chess.pgn.Game().from_board(board) - + # In the tactic the first to move is the one who lost - result = '1-0' # result of the tactic not the game - if board.turn: # turn return true if white + result = '1-0' # result of the tactic not the game + if board.turn: # turn return true if white result = '0-1' node = game.add_variation(self.last_move) @@ -47,13 +49,13 @@ def color(self): def is_complete(self): return (self.positions.is_complete( - self.positions.category(), - self.color(), - True, - self.positions.material_difference() - ) - and not self.positions.ambiguous() - and len(self.positions.move_list()) > 2) + self.positions.category(), + self.color(), + True, + self.positions.material_difference() + ) + and not self.positions.ambiguous() + and len(self.positions.move_list()) > 2) def generate(self, depth): self.positions.generate(depth) diff --git a/opening_counter.py b/opening_counter.py index 0738844..af5b261 100644 --- a/opening_counter.py +++ b/opening_counter.py @@ -1,19 +1,11 @@ #!/usr/bin/env python """Creating chess puzzles for lichess.org""" -import collections import argparse -import chess -import chess.uci -import chess.pgn +import collections import logging -import os -import sys -from modules.fishnet.fishnet import stockfish_command -from modules.puzzle.puzzle import puzzle -from modules.bcolors.bcolors import bcolors -from modules.investigate.investigate import investigate -from modules.api.api import post_puzzle + +import chess.pgn parser = argparse.ArgumentParser(description=__doc__) @@ -38,12 +30,12 @@ game_id = 0 while True: game = chess.pgn.read_game(all_games) - if game == None: + if game is None: break - if (game.headers["White"] == "engendrio"): - ecos_white.append(game.headers["ECO"]+'-'+game.headers["Opening"]) + if game.headers["White"] == "engendrio": + ecos_white.append(game.headers["ECO"] + '-' + game.headers["Opening"]) else: - ecos_black.append(game.headers["ECO"]+'-'+game.headers["Opening"]) + ecos_black.append(game.headers["ECO"] + '-' + game.headers["Opening"]) print("WHITE_____") print(collections.Counter(ecos_white))