python - conda 包的 yaml 文件以编程方式更新

标签 python anaconda jinja2 conda ruamel.yaml

我有一个 python 脚本,想要读取一组 conda 食谱并使用一些信息更新它们(例如文件: https://github.com/williamjamir/staged-recipes/blob/850760fb63c0fc000b95ac27680ec018fa94dcb7/recipes/pyexcel-ezodf/meta.yaml ):

我正在使用这个:

from ruamel.yaml import YAML

from yaml.constructor import ConstructorError
from yaml.scanner import ScannerError

yaml = YAML(typ='jinja2')
yaml.allow_duplicate_keys = True
with open(file_name) as fp:
     yalm_file = yaml.load(fp)

当我使用以下方式打印原始文件 yaml_file 时:

with open(path_file, 'w') as fp:
    yaml.dump(yaml_file, fp, allow_unicode=True, explicit_start=True) 

输出包含大量与类型相关的标签和注释 数据如:

 --- !!python/object/apply:ruamel.yaml.comments.CommentedMap
 dictitems:
 about: !!python/object/apply:ruamel.yaml.comments.CommentedMap
 dictitems: {home: 'https://github.com/soedinglab/xxmotif', license: 
 GPLv3, license_file: LICENSE,
 summary: 'eXhaustive, weight matriX-based motif discovery in nucleotide sequences'}
state:
  _yaml_format: !!python/object/new:ruamel.yaml.comments.Format
    state: !!python/tuple
    - null
    - {_flow_style: false}

我该如何解决这个问题?

最佳答案

YAML 实例的 dump() 方法不接受您提供的参数 (allow_unicode=True,explicit_start=True) 。由于您没有提供完整的工作程序,我只能猜测您(还)执行了import ruamel.yaml as yaml(甚至import yaml)。

由 jinja2 插件完成的转换处理标准 jinja2 模板语法(通常在使用 YAML 解析器解析之前进行处理)需要在加载和转储时完成。因此,您需要使用相同的 YAML(typ='jinja2') 实例来执行此操作:

import sys
file_name = 'meta.yaml'

from ruamel.yaml import YAML

from yaml.constructor import ConstructorError
from yaml.scanner import ScannerError

yaml = YAML(typ='jinja2')
yaml.allow_duplicate_keys = True
yaml.indent(sequence=4, offset=2)
yaml.preserve_quotes = True
# yaml.explicit_start = True
with open(file_name) as fp:
     data = yaml.load(fp)

yaml.dump(data, sys.stdout)

准确给出您在往返过程中的输入:

{% set name = "pyexcel-ezodf" %}
{% set version = "0.3.3" %}
{% set sha256 = "26ddddc61c6bbe2641a15964ba57eaf92a171478e7ed9efb9ae4db1567d0998
c" %}

package:
  name: {{ name|lower }}
  version: {{ version }}

source:
  fn: {{ name }}-{{ version }}.tar.gz
  # The github url is been used because the tar.gz from pypi is missing the CONT
RIBUTORS.rst file
  url: https://github.com/pyexcel/{{ name }}/archive/v{{ version }}.tar.gz
  sha256: {{ sha256 }}

build:
  noarch: python
  number: 0
  script: python setup.py install --single-version-externally-managed --record r
ecord.txt

requirements:
  build:
    - python
    - setuptools

  run:
    - python
    - lxml

test:
  imports:
    - ezodf


about:
  home: https://github.com/pyexcel/pyexcel-ezodf
  license: MIT
  license_family: MIT
  license_file: '{{ environ["RECIPE_DIR"] }}/LICENSE'
  summary: 'A Python package to create/manipulate OpenDocumentFormat files'
  description: |
    'ezodf is a Python package to create new or open existing' +
    'OpenDocument (ODF) files to extract, add, modify or delete document data' +
    'forked from dead project https://bitbucket.org/mozman/ezodf' +
    'format and to/from databases' +
    ''
  dev_url: https://github.com/pyexcel/pyexcel-ezodf

extra:
  recipe-maintainers:
    - williamjamir

您不需要设置 allow_unicode,这是 YAML.dump() 的默认设置。

关于python - conda 包的 yaml 文件以编程方式更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46677139/

相关文章:

python - 如何在 python 中向 Bokeh 图添加误差线?

python - 解析和处理表格.txt 文件

javascript - 如何通过单击按钮来更新 Flask 应用程序中的一个元素?

python - Python click 模块没有输出?

python-3.x - PyInstaller 和 Nuitka 生成大得离谱的文件。如何缩小尺寸?

for 循环中的 Ansible Jinja2 模板条件

python - 在 Python 中读取 CSV 文件并将数据传递给 Jinja 模板

python - 有没有办法避免遍历 python 中 itertools.combinations() 生成的所有可能的组合?

python - 如何阅读 Python 请求的响应?

python - 无法将大小为 47040000 的数组 reshape 为预训练神经网络的形状 (60000,32,32,1)