【发布时间】:2022-11-03 15:50:45
【问题描述】:
是否可以根据设置的变量执行某种条件代码块?我正在尝试模板化具有一些 yaml 配置文件的项目。我希望 yaml 配置的一部分是可选的,但我不知道是否有办法使用cookiecutter 来做到这一点。我知道cookiecutter 支持可选文件和目录。
这是一个例子:
{{ cookiecutter.pipeline_name }}:
models:
{{ cookiecutter.model_name }}:
inference:
instance_type: {{ cookiecutter.instance_type }}
containers:
- image:
name: {{ cookiecutter.image_name }}
repo: {{ cookiecutter.image_repo }}
tag: {{ cookiecutter.image_tag }}
provider: ecr
data: {{ cookiecutter.model_artifact }}
async_inference_config:
s3_output_path: {{ cookiecutter.async_output_path }}
max_concurrent_invocations_per_instance: {{ cookiecutter.max_invocations }}
async_inference_config 块应该是可选的。如果用户没有填写async_output_path 和max_invocations 变量,那么应该删除整个块。如果这不可能,我可以创建 2 个不同的 cookiecutter 模板。但考虑到这两个模板之间的唯一区别是async_inference_config 块,这似乎是一种浪费。
【问题讨论】:
-
您可以使用后生成挂钩 (cookiecutter.readthedocs.io/en/stable/advanced/hooks.html) 并通过这种方式获得最大的灵活性。
标签: python templates cookiecutter