Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import collections
import yaml
# Try to preserve order in the front matter.
def dict_representer(dumper, data):
return dumper.represent_dict(data.items())
def dict_constructor(loader, node):
return collections.OrderedDict(loader.construct_pairs(node))
_mapping_tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG
yaml.Dumper.add_representer(collections.OrderedDict, dict_representer)
yaml.Loader.add_constructor(_mapping_tag, dict_constructor)
yaml.FullLoader.add_constructor(_mapping_tag, dict_constructor)
# Represent null as blank.
def none_representer(dumper, _):
return dumper.represent_scalar(',2002:null', '')
yaml.Dumper.add_representer(type(None), none_representer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment