首页 > 解决方案 > Colab 中的配置代码可以发送到需要文件的对象吗?

问题描述

我在 Colab 中使用 Python3 在扩展整洁 python 的应用程序中。需要大型配置文件。我希望它们位于 Colab 笔记本中以便于修改,但整洁 python 中的 Config 对象需要一个路径/文件。我可以从笔记本中编写一个配置文件并发送它,但这似乎很笨拙。有没有办法以某种方式打包笔记本的一部分并使其看起来像一个文件?

来自整洁python Config 对象的相关代码是:

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)

标签: pythongoogle-colaboratoryconfiguration-files

解决方案


推荐阅读