ruamel.yaml

How to recursively sort YAML using CommentedMap?

余生长醉 提交于 2020-08-10 04:22:11
问题 There's a good example of sorting at the top level, but how would you recurse the sort at all levels? This seems to work: def sort_commentedmap(self, od): res = ruamel.yaml.comments.CommentedMap() for k in sorted(od): res[k] = od[k] if not isinstance(od[k], ruamel.yaml.comments.CommentedSeq): continue for idx, i in enumerate(od[k]): if isinstance(i, str): res[k][int(idx)] = i else: res[k][int(idx)] = self.sort_commentedmap(i) return res Does this look correct? 回答1: In YAML you can have

How to recursively sort YAML using CommentedMap?

独自空忆成欢 提交于 2020-08-10 04:22:10
问题 There's a good example of sorting at the top level, but how would you recurse the sort at all levels? This seems to work: def sort_commentedmap(self, od): res = ruamel.yaml.comments.CommentedMap() for k in sorted(od): res[k] = od[k] if not isinstance(od[k], ruamel.yaml.comments.CommentedSeq): continue for idx, i in enumerate(od[k]): if isinstance(i, str): res[k][int(idx)] = i else: res[k][int(idx)] = self.sort_commentedmap(i) return res Does this look correct? 回答1: In YAML you can have

How to recursively sort YAML (with anchors) using CommentedMap?

左心房为你撑大大i 提交于 2020-08-08 05:40:12
问题 I'm facing issue with the recursive sort solution proposed here I cannot sort YAML file with anchor and sub-elements. The .pop method call is throwing a KeyError exception. Ex: volvo: anchor_struct: &anchor_struct zzz: val: "bar" aaa: val: "foo" aaa: "Authorization" zzz: 341 anchr_val: &anchor_val famous_val lambo: <<: *anchor_struct mykey: myval: enabled: false anchor_struct: <<: *anchor_struct username: user anchor_val: *anchor_val zzz: zorglub www: web File "orderYaml.py", line 36, in

Add a comment in list element in ruamel.yaml

让人想犯罪 __ 提交于 2020-07-09 15:33:27
问题 I am dynamically adding elements in a list in a YAML file using Python and I would like to add a comment alongside each of the elements I am adding. The following are all desired formats: flow_style_example: - [a, b, c] # first list - [d, e] # second list block_style_example: - - a # first list side comment - b - c # second list top comment - - d - e list_of_elements_side_comment: - a # foo - b # bar list_of_elements_top_comment: # comment 1 - a # comment 2 - b For any of the above I have yet

SIngle character y and Y get dumped as YAML 1.1 booleans

余生长醉 提交于 2020-05-17 02:57:31
问题 When I do: from ruamel import yaml seq = ["x", "y", "z", "Y", "true", True] print(yaml.dump(seq, version=(1,1))) it gives: %YAML 1.1 --- [x, y, z, Y, 'true', true] but I expected the y and Y to be quoted, because these get loaded back as booleans because this is YAML 1.1. Moreover this bug, indicates this problem is solved. Why is this bug marked as closed, when it still shows this error even on version ruamel.yaml>=0.15.93? 回答1: You are using the unsafe PyYAML compatibility function dump()

Configuring ruamel.yaml to allow duplicate keys

荒凉一梦 提交于 2020-02-22 06:12:32
问题 I'm trying to use the ruamel.yaml library to process a Yaml document that contains duplicate keys. In this case the duplicate key happens to be a merge key <<: . This is the yaml file, dupe.yml : foo: &ref1 a: 1 bar: &ref2 b: 2 baz: <<: *ref1 <<: *ref2 c: 3 This is my script: import ruamel.yaml yml = ruamel.yaml.YAML() yml.allow_duplicate_keys = True doc = yml.load(open('dupe.yml')) assert doc['baz']['a'] == 1 assert doc['baz']['b'] == 2 assert doc['baz']['c'] == 3 When run, it raises this

When ruamel.yaml loads @dataclass from string, __post_init__ is not called

余生长醉 提交于 2020-02-02 06:02:47
问题 Assume I created a @dataclass class Foo , and added a __post_init__ to perform type checking and processing. When I attempt to yaml.load a !Foo object, __post_init__ is not called. from dataclasses import dataclass, fields from ruamel.yaml import yaml_object, YAML yaml = YAML() @yaml_object(yaml) @dataclass class Foo: foo: int bar: int def __post_init__(self): raise Exception for field in fields(self): value = getattr(self, field.name) typ = field.type if not isinstance(value, typ): raise

How to remove 2 spaces from the output of ruamel.yaml dump?

十年热恋 提交于 2020-01-30 07:42:48
问题 With the help of yaml.indent(sequence=4, offset=2) the output is correct but there is extra space coming in each line and I know it is due to above indent function. Is there any way to remove the 2 extra spaces from each line(I don't wont to use strip()). Code: import sys import ruamel.yaml data = [{'item': 'Food_eat', 'Food': {'foodNo': 42536216,'type': 'fruit','moreInfo': ['organic']}}] yaml = ruamel.yaml.YAML() yaml.indent(sequence=4, offset=2) yaml.dump(data, sys.stdout) Output of above

Dumping unicode with YAML

六月ゝ 毕业季﹏ 提交于 2020-01-24 19:49:05
问题 I'm creating yaml files from csv's that have a lot of unicode characters in them but I can't seem to get it to dump the unicode without it giving me a Decode Error. I'm using the ruamel.yaml library. UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 11: ordinal not in range(128) I've tried parsing strings, unicode strings, encoding with "utf-8" nothing seems to work. I've seen a lot of examples that show adding a representer to solve the issue but they all seem to be using

How to preserve indenting in key-value when dumping yaml

社会主义新天地 提交于 2020-01-15 08:13:29
问题 How to preserve indenting in key-value when dumping yaml ? I am using ruamel yaml Code: in_str='''Pets: Cat: Tom Mouse: Jerry Dog: Scooby ''' import ruamel.yaml, sys results = ruamel.yaml.load(in_str, ruamel.yaml.RoundTripLoader, preserve_quotes=True) results['Pets']['Bird']='Tweety' ruamel.yaml.dump(results, sys.stdout, ruamel.yaml.RoundTripDumper, default_flow_style=True,indent=2, block_seq_indent=2) Output : Pets: Cat: Tom Mouse: Jerry Dog: Scooby Bird: Tweety Expected Output: Pets: Cat: