Last active
January 25, 2023 11:08
-
-
Save oglops/c70fb69eef42d40bed06 to your computer and use it in GitHub Desktop.
Revisions
-
oglops revised this gist
Jul 20, 2022 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,5 +21,6 @@ def __init__(self, *args, **kwargs): with open('test.yaml') as f: d = yaml.load(f, Loader=OrderedLoader) print(d) # pretty print ordered dict import json print(json.dumps(d, indent=4)) -
oglops revised this gist
Jul 20, 2022 . 1 changed file with 25 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ import yaml from yaml import SafeDumper, Dumper, SafeLoader from collections import OrderedDict class OrderedDumper(SafeDumper): def __init__(self, *args, **kwargs): super(OrderedDumper, self).__init__(*args, **kwargs) represent_dict_order = lambda self, data: self.represent_mapping(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items()) self.add_representer(OrderedDict, represent_dict_order) data = OrderedDict([(2, 'b'), (1, 'a'), (3, [{'children': [{4: 'c'}, {5: 'd'}]}])]) print(yaml.dump(data, Dumper=OrderedDumper)) class OrderedLoader(SafeLoader): def __init__(self, *args, **kwargs): super(OrderedLoader, self).__init__(*args, **kwargs) construct_dict_order = lambda self, data: OrderedDict(self.construct_pairs(data)) self.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_dict_order) with open('test.yaml') as f: d = yaml.load(f, Loader=OrderedLoader) print(d) import json print(json.dumps(d, indent=4)) -
oglops revised this gist
Jul 20, 2022 . No changes.There are no files selected for viewing
-
oglops revised this gist
Jul 20, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ def dict_representer(dumper, data): return dumper.represent_dict(data.items()) def dict_constructor(loader, node): -
oglops revised this gist
Mar 2, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/env python try: # for python newer than 2.7 from collections import OrderedDict -
oglops created this gist
Mar 2, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ try: # for python newer than 2.7 from collections import OrderedDict except ImportError: # use backport from pypi from ordereddict import OrderedDict import yaml # try to use LibYAML bindings if possible try: from yaml import CLoader as Loader, CDumper as Dumper except ImportError: from yaml import Loader, Dumper from yaml.representer import SafeRepresenter _mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG def dict_representer(dumper, data): return dumper.represent_dict(data.iteritems()) def dict_constructor(loader, node): return OrderedDict(loader.construct_pairs(node)) Dumper.add_representer(OrderedDict, dict_representer) Loader.add_constructor(_mapping_tag, dict_constructor) Dumper.add_representer(str, SafeRepresenter.represent_str) Dumper.add_representer(unicode, SafeRepresenter.represent_unicode) # output = yaml.dump(data, Dumper=Dumper, default_flow_style=False) # data = yaml.load(stream, Loader=Loader) # abc: # x: # 0: null # y: # 1: null yml_dict = OrderedDict( abc=OrderedDict( [('x', OrderedDict([(0, None)])), ('y', OrderedDict([(1, None)]))])) import json print(json.dumps(yml_dict, indent=2)) print # dump ordereddict to yaml output = yaml.dump(yml_dict, Dumper=Dumper, default_flow_style=False) print output # directly write to a file object to save memory. with open('result.yml', 'w') as f: yaml.dump(yml_dict, f, default_flow_style=False)