Closed aidanheerdegen closed 3 months ago
This is how to allow duplicate keys
https://yaml.readthedocs.io/en/latest/api/#duplicate-keys
yaml = ruamel.yaml.YAML()
yaml.allow_duplicate_keys = True
yaml.load(stream)
Not sure if we need to make sure the two libraries are consistent with what key values they keep?
There is an existing issue here where others are annoyed pyyaml isn't following the standard
https://github.com/yaml/pyyaml/issues/165
some work-arounds to make pyyaml disallow duplicate keys
Not sure if we need to make sure the two libraries are consistent with what key values they keep?
To be specific ruamel
ignores subsequent duplicate values, whereas pyyaml
overwrites them.
So given dupe.yaml
:
a: 1
b: 2
a: 3
Running this:
from ruamel.yaml import YAML
import yaml as pyyaml
yaml = YAML()
yaml.allow_duplicate_keys = True
with open('dupe.yaml') as stream:
data = yaml.load(stream)
print(data)
with open('dupe.yaml') as stream:
data = pyyaml.safe_load(stream)
print(data)
produces this:
$ python test.py
{'a': 1, 'b': 2}
{'a': 3, 'b': 2}
This is blocking answering this query on the forum because there is a duplicate key in a config, and ruamel
can't deal with it and throws an exception.
It seems ruamel yaml is pickier than PyYAML, because I go this error
when trying to checkout a new branch from an existing experiment that has duplicate
qsub_flags
options in theconfig.yaml
:https://github.com/aidanheerdegen/access_esm_mio_run/blob/mio_v4_master/config.yaml
The experiment ran without error, so my supposition is that PyYAML silently ignores the duplicate keys. We should suppress the exception, but warn if possible.