Created
June 21, 2018 14:42
-
-
Save messa/59d0d339a1a27396e48306d2dc962c04 to your computer and use it in GitHub Desktop.
YAML str representer
This file contains 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 characters
import yaml | |
def yaml_dump(obj): | |
return yaml.dump(obj, width=120, default_flow_style=False, Dumper=_CustomDumper) | |
class _CustomDumper (yaml.SafeDumper): | |
pass | |
def _str_representer(dumper, data): | |
style = '|' if '\n' in data else None | |
return dumper.represent_scalar('tag:yaml.org,2002:str', data, style=style) | |
_CustomDumper.add_representer(str, _str_representer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment