Skip to content

Instantly share code, notes, and snippets.

@LouiS0616
Created March 1, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouiS0616/164fc8db572411080e8128d4e0be4861 to your computer and use it in GitHub Desktop.
Save LouiS0616/164fc8db572411080e8128d4e0be4861 to your computer and use it in GitHub Desktop.
pyyamlについて:タグの加え方
import yaml
from yaml import ScalarNode, SequenceNode
def _flatten(_, node):
def _inner(n):
if isinstance(n, ScalarNode):
yield n
elif isinstance(n, SequenceNode):
for e in n.value:
yield from _inner(e)
else:
assert False
return [v.value for v in _inner(node)]
yaml.add_constructor('!Flatten', _flatten)
#
with open('sample.yml') as fin:
data = yaml.load(fin.read(), Loader=yaml.FullLoader)
print(data)
a: &nums [4, 5, 6]
b: !!Flatten [1, 2, 3, *nums]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment