python - 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)
解决方案
推荐阅读
- javascript - 强制 v-validate 更新规则(使用 Vue)
- java - java.util.logging.Logger:“超时”后文件扩展名的增量
- android - Android Studio 布局错误:无效的 region.op - 只允许相交和不同
- r - 在ggplot R中改变y轴
- qt - 哪个类似于 QML 中的 QGraphicsScene?
- php - 在 php 中将会话变量改回 $variable
- javascript - 三个js的两个场景:CubeGeometry和PointsGeometry——只出现一个
- orientdb - OrientDB的现状
- graphql - Apollo GraphQL 无法为嵌套字段调用解析器
- r - 在列上单独使用 tidyr 函数