<commit_before><commit_msg>class to handle settings input<commit_after>from sys import stdin, stdout
import time


class Settings:
    """
    Seperate parsing settings from game instance logic. All the settings come
    at the start of the game, in a set of lines via stdin.
    """
    # TODO: explain how each is used (or not)
    STRING_SETTINGS = [
        'your_bot',
        'your_botid'
    ]
    NUMERICAL_SETTINGS = [
        'timebank',
        'time_per_move',
        'field_width',
        'field_height',
        'max_rounds'
    ]
    CSV_SETTINGS = [
        'player_names'
    ]

    @classmethod
    def _parse_settings(cls):
        settings = {}
        while True:
            line = stdin.readline().strip()

            # wait until we have a line
            if len(line) <= 0:
                time.sleep(1)
                continue

            if not line.startswith('settings'):
                # The first message after the settings should be update game
                # round 0, which we can safely throw away.
                if not line == 'update game round 0':
                    raise Exception('Threw away important line')
                break
            else:
                cls._add_setting(settings, line)

        return settings

    @classmethod
    def _add_setting(cls, settings, line):
        _, key, value = line.split()

        if key in cls.STRING_SETTINGS:
            settings[key] = value
        elif key in cls.NUMERICAL_SETTINGS:
            settings[key] = int(value)
        elif key in cls.CSV_SETTINGS:
            settings[key] = value.split(',')
        else:
            raise Exception('Unrecognised setting', line)

{'fraction_non_alphanumeric': 0.0830367734282325, 'fraction_numerical': 0.002372479240806643, 'mean_word_length': 1.7209677419354839, 'pattern_counts': {'":': 0, '<': 4, '<?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': '5904245', 'n_tokens_mistral': 475, 'n_tokens_neox': 460, 'n_words': 145}