Skip to content

Instantly share code, notes, and snippets.

@hairmare
Created June 8, 2023 21:37
Show Gist options
  • Save hairmare/17ed4398dfdee1de1ca7b42c8a5750ad to your computer and use it in GitHub Desktop.
Save hairmare/17ed4398dfdee1de1ca7b42c8a5750ad to your computer and use it in GitHub Desktop.
ruamel multiline scalar roundtripping
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: ruamel.yaml in /usr/lib/python3.11/site-packages (0.17.24)\n",
"Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /usr/lib64/python3.11/site-packages (from ruamel.yaml) (0.2.7)\n"
]
}
],
"source": [
"import sys\n",
"!{sys.executable} -m pip install ruamel.yaml"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"# demonstrates roudntripping of scalar formatting and comments + whitespace\n",
"\n",
"default: i am string\n",
"single: 'single'\n",
"double: \"\" # and empty as well\n",
"\n",
"\n",
"# final line break and any trailing empty lines are clipped\n",
"clipped: |\n",
" clipped example\n",
"# final line break and any trailing empty lines are stripped\n",
"stripped: |-\n",
" stripped example\n",
"\n",
"\n",
" below two spaces\n",
" key: in the string\n",
"\n",
"\n",
"\n",
"key: test\n",
"# an awful lot of whitespace before the key above\n",
"kept: |+\n",
" will keep all the trailing lines\n",
"\n",
"\n",
"...\n"
]
}
],
"source": [
"from ruamel.yaml import YAML\n",
"from ruamel.yaml.scalarstring import LiteralScalarString\n",
"\n",
"demo = \"\"\"\\\n",
"# demonstrates roudntripping of scalar formatting and comments + whitespace\n",
"\n",
"default: i am string\n",
"single: 'single'\n",
"double: \"\" # and empty as well\n",
"\n",
"\n",
"# final line break and any trailing empty lines are clipped\n",
"clipped: |\n",
" clipped example\n",
"# final line break and any trailing empty lines are stripped\n",
"stripped: |-\n",
" stripped example\n",
"\n",
"\n",
" below two spaces\n",
" key: in the string\n",
"\n",
"\n",
"\n",
"key: test\n",
"# an awful lot of whitespace before the key above\n",
"kept: |+\n",
" will keep all the trailing lines\n",
" \n",
" \n",
" \"\"\"\n",
"\n",
"yaml = YAML()\n",
"yaml.preserve_quotes = True\n",
"\n",
"code = yaml.load(demo)\n",
"# dump to demonstrate that everything is preserved when we no-op\n",
"yaml.dump(code, sys.stdout)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"code['clipped']='clipped example\\n'\n",
"# demonstrates roudntripping of scalar formatting and comments + whitespace\n",
"\n",
"default: i am string\n",
"single: 'single'\n",
"double: \"\" # and empty as well\n",
"\n",
"\n",
"# final line break and any trailing empty lines are clipped\n",
"clipped: |\n",
" clipped example\n",
"# final line break and any trailing empty lines are stripped\n",
"stripped: |-\n",
" stripped example\n",
"\n",
"\n",
" below two spaces\n",
" key: in the string\n",
"\n",
"\n",
"\n",
"key: test\n",
"# an awful lot of whitespace before the key above\n",
"kept: |+\n",
" will keep all the trailing lines\n",
"\n",
"\n",
"...\n"
]
}
],
"source": [
"# update clipped example\n",
"print(f\"{code['clipped']=}\")\n",
"code['clipped'] = \"\"\"clipped example\n",
"\"\"\"\n",
"yaml.dump(code, sys.stdout)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"code['stripped']='stripped example\\n\\n\\nbelow two spaces\\nkey: in the string'\n",
"# demonstrates roudntripping of scalar formatting and comments + whitespace\n",
"\n",
"default: i am string\n",
"single: 'single'\n",
"double: \"\" # and empty as well\n",
"\n",
"\n",
"# final line break and any trailing empty lines are clipped\n",
"clipped: |\n",
" clipped example\n",
"# final line break and any trailing empty lines are stripped\n",
"stripped: |-\n",
" stripped example\n",
"\n",
"\n",
" below two spaces\n",
" key: in the string\n",
"\n",
"\n",
"\n",
"key: test\n",
"# an awful lot of whitespace before the key above\n",
"kept: |+\n",
" will keep all the trailing lines\n",
"\n",
"\n",
"...\n"
]
}
],
"source": [
"print(f\"{code['stripped']=}\")\n",
"code['stripped'] = LiteralScalarString(\"\"\"stripped example\n",
"\n",
"\n",
"below two spaces\n",
"key: in the string\"\"\")\n",
"\n",
"yaml.dump(code, sys.stdout)\n"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"code['kept']='will keep all the trailing lines\\n\\n\\n'\n",
"# demonstrates roudntripping of scalar formatting and comments + whitespace\n",
"\n",
"default: i am string\n",
"single: 'single'\n",
"double: \"\" # and empty as well\n",
"\n",
"\n",
"# final line break and any trailing empty lines are clipped\n",
"clipped: |\n",
" clipped example\n",
"# final line break and any trailing empty lines are stripped\n",
"stripped: |-\n",
" stripped example\n",
"\n",
"\n",
" below two spaces\n",
" key: in the string\n",
"\n",
"\n",
"\n",
"key: test\n",
"# an awful lot of whitespace before the key above\n",
"kept: |+\n",
" will keep all the trailing lines\n",
"\n",
"\n",
"...\n"
]
}
],
"source": [
"print(f\"{code['kept']=}\")\n",
"code['kept'] = LiteralScalarString(\"\"\"will keep all the trailing lines\n",
"\n",
"\n",
"\"\"\")\n",
"yaml.dump(code, sys.stdout)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment