Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created June 16, 2019 05:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/dd8717c19075263e03f689cbc15326c4 to your computer and use it in GitHub Desktop.
Save chadaustin/dd8717c19075263e03f689cbc15326c4 to your computer and use it in GitHub Desktop.
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('tag:yaml.org,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