在python中禁用yaml文件的别名 [英] Disabling alias for yaml file in python

查看:38
本文介绍了在python中禁用yaml文件的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我希望防止别名出现在我的 YAML 文件中.无论如何我可以禁用生成的 YAML 文件中的别名,以实现预期的输出?

I have a problem that I wish to prevent aliases from occurring in my YAML file. Is there anyway I can disable the aliases in the YAML file generated, to achieve the intended output?

我当前拥有的 YAML 文件如下:

The current YAML file I have is this below:

agents:
-   start: [0, 0]
    goal: [2, 0]
    name: agent0
-   start: [2, 0]
    goal: [0, 0]
    name: agent1
map:
    dimensions: [3, 3]
    obstacles:
    - !!python/tuple [0, 1]
    - !!python/tuple [2, 1]

当我更新每个代理的 YAML 文件时,当他们到达目标点时,我会遇到这个问题,即别名是起始值和目标值而不是值我想要,即 [0,0] 为开始,[0,0] 为目标.

As I updating the YAML file for each of the agents whenever they reached their goal point with the value of the goal to the start, I will encounter this problem of alias being the value of the start and goal value instead of the value I want, i.e. [0,0] for start, [0,0] for goal.

agents:
- start: &id001 [2, 0]
  goal: *id001
  name: agent0
- start: &id002 [0, 0]
  goal: *id002
  name: agent1
map:
  dimensions: [3, 3]
  obstacles:
  - !!python/tuple [0, 1]
  - !!python/tuple [2, 1]

预期输出:

agents:
- start: [2, 0]
  goal: [2, 0]
  name: agent0
- start: [0, 0]
  goal: [0, 0]
  name: agent1
map:
  dimensions: [3, 3]
  obstacles:
  - !!python/tuple [0, 1]
  - !!python/tuple [2, 1]

我在 Python 中运行了以下代码来更新文件:

I have following code that I run in Python to update the file:

def updateInput(self, agent):
        yaml = ruamel.yaml.YAML()
        with open('input.yaml') as f:
            doc = yaml.load(f)
            self.get_updated_dict(doc, agent)

        with open('input.yaml', 'w') as y:
            yaml.dump(doc, y)

        return {}

    def get_updated_dict(self, doc, agent):
        obj = doc
        if obj["agents"][0]["name"] == agent:
            goal_state = obj["agents"][0]["goal"]
            obj["agents"][0]["start"] = goal_state   
        return doc

推荐答案

ruamel.yaml 的代表阶段创建别名,但对于每条数据它使用 ignore_aliases 方法(默认为 False)检查别名是否应该被忽略的那条数据.

The representer stage of ruamel.yaml creates the aliases, but for every piece of data it checks with the ignore_aliases method (defaulting to False) whether the aliases should be ignored for that piece of data.

在下面我取消了所有别名,但您可以使其依赖于(类型)数据正在被代表.(这只是说明原理,你不必首先使用别名编写 YAML 版本,只需添加行

In the following I suppress all aliases, but you can make this dependent on the (type of) data that is being represented. (This just shows the principle, you don't first have to write the YAML version with aliases, just add the line

yaml.representer.ignore_aliases = lambda *data: True

实例化后的任何地方 yaml)

anywhere after instantiation yaml)

import sys
import ruamel.yaml

yaml_str = """\
agents:
- start: &id001 [2, 0]
  goal: *id001
  name: agent0
- start: &id002 [0, 0]
  goal: *id002
  name: agent1
map:
  dimensions: [3, 3]
  obstacles:
  - !!python/tuple [0, 1]
  - !!python/tuple [2, 1]
"""

yaml = ruamel.yaml.YAML()
yaml.representer.ignore_aliases = lambda *data: True
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)

给出:

agents:
- start: [2, 0]
  goal: [2, 0]
  name: agent0
- start: [0, 0]
  goal: [0, 0]
  name: agent1
map:
  dimensions: [3, 3]
  obstacles:
  - !!python/tuple [0, 1]
  - !!python/tuple [2, 1]

但请注意,如果您有自递归数据,您将必须等待很长时间才能写入文件(取决于光盘容量的课程).

Take care though that if you have self-recursing data, you're going to have to wait a long time until your file gets written (depending of course on your disc-capacity).

这篇关于在python中禁用yaml文件的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆