Colab中的配置代码是否可以发送到需要文件的对象?

2024-09-30 03:25:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Colab的一个扩展纯python的应用程序中使用Python3。需要大的配置文件。我希望它们放在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)

Tags: selfconfiggenomeparsetype笔记本assertfilename

热门问题