Skip to content

Instantly share code, notes, and snippets.

@arigesher
Created January 26, 2018 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arigesher/a8d50462a8e1f47ddb60e644a7a18c87 to your computer and use it in GitHub Desktop.
Save arigesher/a8d50462a8e1f47ddb60e644a7a18c87 to your computer and use it in GitHub Desktop.
YAML for better output formatting
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import yaml\n",
"\n",
"\n",
"def yamlformat(to_format):\n",
" out = yaml.dump(to_format, default_flow_style=False)\n",
" return out\n",
"\n",
"\n",
"def yamlprint(to_print):\n",
" out = yamlformat(to_print)\n",
" print(out)\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"some_dict = {\n",
" \"foo\": \"bar\",\n",
" \"baz\": {\n",
" \"wow\": [\n",
" \"item1\",\n",
" \"item2\",\n",
" \"item3\"\n",
" ]\n",
" }\n",
"}\n",
"\n",
"\n",
"just_a_list_maam = [\"apples\", \"oranges\", \"bananas\", \"bananas???\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"- apples\n",
"- oranges\n",
"- bananas\n",
"- bananas???\n",
"\n"
]
}
],
"source": [
"yamlprint(just_a_list_maam)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"baz:\n",
" wow:\n",
" - item1\n",
" - item2\n",
" - item3\n",
"foo: bar\n",
"\n"
]
}
],
"source": [
"yamlprint(some_dict)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment