<commit_before>import sys
from time import strftime
from datetime import datetime
# from os.path import dirname, abspath
# project_dir = dirname(dirname(dirname(abspath(__file__))))
# sys.path.append(project_dir)

from Game import *
from bots.HumanConsole import HumanConsole
from bots.RandomBot import RandomBot
from bots.RandomGroupingBot import RandomGroupingBot


class GTPengine:

    def __init__(self):
        self.game = Game()
        self.bot = RandomBot()
        self.stdout = sys.stdout
        self.logfile = open('log_' + strftime('%d-%m-%Y_%H-%M-%S') + '.txt', 'w')
        self.write_log('  start: ', self.bot.__class__.__name__ + ', ' + __file__)
        self.gtp_commands = {}
        gtp_methods = [
            self.set_player_type,  # not a GTP-command
            self.protocol_version,
            self.name,
            self.version,
            self.known_command,
            self.list_commands,
            self.quit,
            self.boardsize,
            self.clear_board,
            self.komi,
            self.play,
            self.genmove,
            self.showboard,
        ]
        # create a dictionary with the method-name as key and the method as
        # value. in that way, valid GTP commands serve directly as keys to
        # get the corresponding method
        for method in gtp_methods:
            self.gtp_commands[method.__name__] = method

        self.player_types = {}
        player_types_arr = [
            HumanConsole,
            RandomBot,
            RandomGroupingBot,
        ]
        for player_type in player_types_arr:
            self.player_types[player_type.__name__] = player_type

    def set_player_type(self, args):
        if len(args) == 0:
            self.send_failure_response('no player type passed')
            return
        player_type = args[0]
        if player_type not in self.player_types:
            self.send_failure_response('player type ' + player_type + ' unknown')
            return
        self.bot = self.player_types[player_type]()
        self.send_success_response('switched to player type ' + player_type)

    def run(self):
        while True:
            stdin_line = sys.stdin.readline().strip()
            self.write_log('receive: ', stdin_line)
            if len(stdin_line) == 0:  # ignore empty lines
                continue
            # print('reading input: ' + stdin_line)
            parts = stdin_line.split(' ')
            command = parts[0]
            if command not in self.gtp_commands:
                self.send_failure_response('command unknown: ' + stdin_line)
                continue
            self.gtp_commands[command](parts[1:])

    def write_log(self, type, message):
        self.logfile.write('[' + datetime.now().strftime('%d.%m.%Y %H:%M:%S.%f') + '] ' + type + message.strip() + '\n')
        self.logfile.flush()

    def write_out(self, message):<commit_msg>define stdin as class variable<commit_after>import sys
from time import strftime
from datetime import datetime
# from os.path import dirname, abspath
# project_dir = dirname(dirname(dirname(abspath(__file__))))
# sys.path.append(project_dir)

from Game import *
from bots.HumanConsole import HumanConsole
from bots.RandomBot import RandomBot
from bots.RandomGroupingBot import RandomGroupingBot


class GTPengine:

    def __init__(self):
        self.game = Game()
        self.bot = RandomBot()
        self.stdin = sys.stdin
        self.stdout = sys.stdout
        self.logfile = open('log_' + strftime('%d-%m-%Y_%H-%M-%S') + '.txt', 'w')
        self.write_log('  start: ', self.bot.__class__.__name__ + ', ' + __file__)
        self.gtp_commands = {}
        gtp_methods = [
            self.set_player_type,  # not a GTP-command
            self.protocol_version,
            self.name,
            self.version,
            self.known_command,
            self.list_commands,
            self.quit,
            self.boardsize,
            self.clear_board,
            self.komi,
            self.play,
            self.genmove,
            self.showboard,
        ]
        # create a dictionary with the method-name as key and the method as
        # value. in that way, valid GTP commands serve directly as keys to
        # get the corresponding method
        for method in gtp_methods:
            self.gtp_commands[method.__name__] = method

        self.player_types = {}
        player_types_arr = [
            HumanConsole,
            RandomBot,
            RandomGroupingBot,
        ]
        for player_type in player_types_arr:
            self.player_types[player_type.__name__] = player_type

    def set_player_type(self, args):
        if len(args) == 0:
            self.send_failure_response('no player type passed')
            return
        player_type = args[0]
        if player_type not in self.player_types:
            self.send_failure_response('player type ' + player_type + ' unknown')
            return
        self.bot = self.player_types[player_type]()
        self.send_success_response('switched to player type ' + player_type)

    def run(self):
        while True:
            stdin_line = self.stdin.readline().strip()
            self.write_log('receive: ', stdin_line)
            if len(stdin_line) == 0:  # ignore empty lines
                continue
            # print('reading input: ' + stdin_line)
            parts = stdin_line.split(' ')
            command = parts[0]
            if command not in self.gtp_commands:
                self.send_failure_response('command unknown: ' + stdin_line)
                continue
            self.gtp_commands[command](parts[1:])

    def write_log(self, type, message):
        self.logfile.write('[' + datetime.now().strftime('%d.%m.%Y %H:%M:%S.%f') + '] ' + type + message.strip() + '\n')
        self.logfile.flush()

    def write_out(self, message):
{'fraction_non_alphanumeric': 0.10247229326513214, 'fraction_numerical': 0.0017050298380221654, 'mean_word_length': 2.0615866388308977, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5032048', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1678, 'n_words': 387}