Можно ли отправить код конфигурации в Colab объекту, ожидающему файл? - PullRequest
0 голосов
/ 10 июля 2019

Я использую Python3 в Colab в приложении, расширяющем аккуратный Python. Большие файлы конфигурации не требуется. Я бы хотел, чтобы они были в записной книжке Colab для легкой модификации, но объект Config в neat-python ожидает путь / файл. Я мог бы написать файл конфигурации из записной книжки и отправить его, но это кажется очень неуклюжим. Есть ли способ как-то упаковать часть ноутбука и сделать так, чтобы он выглядел как файл?

Соответствующий код из объекта конфигурации neat-python:

class Config(object):
    """A simple container for user-configurable parameters of NEAT."""

    __params = [ConfigParameter('pop_size', int),
                ConfigParameter('fitness_criterion', str),
                ConfigParameter('fitness_threshold', float),
                ConfigParameter('reset_on_extinction', bool),
                ConfigParameter('no_fitness_termination', bool, False)]

    def __init__(self, genome_type, reproduction_type, species_set_type, stagnation_type, filename):
        # Check that the provided types have the required methods.
        assert hasattr(genome_type, 'parse_config')
        assert hasattr(reproduction_type, 'parse_config')
        assert hasattr(species_set_type, 'parse_config')
        assert hasattr(stagnation_type, 'parse_config')

        self.genome_type = genome_type
        self.reproduction_type = reproduction_type
        self.species_set_type = species_set_type
        self.stagnation_type = stagnation_type

        if not os.path.isfile(filename):
            raise Exception('No such config file: ' + os.path.abspath(filename))

        parameters = ConfigParser()
        with open(filename) as f:
            if hasattr(parameters, 'read_file'):
                parameters.read_file(f)
            else:
                parameters.readfp(f)
...