|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "- ruamel.yaml is a YAML 1.2 loader/dumper package for Python. -> https://yaml.readthedocs.io/en/latest/overview.html\n", |
| 8 | + "- [article](https://medium.com/@deepak7093/dynamic-code-generator-using-yaml-config-and-jinja2-templates-31d8b9663fb0) on using yaml and jinja templating" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "metadata": {}, |
| 14 | + "source": [ |
| 15 | + "Load a yaml file" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 1, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "name": "stdout", |
| 25 | + "output_type": "stream", |
| 26 | + "text": [ |
| 27 | + "config.yaml\n" |
| 28 | + ] |
| 29 | + } |
| 30 | + ], |
| 31 | + "source": [ |
| 32 | + "ls *.yaml" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 2, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "from ruamel.yaml import YAML\n", |
| 42 | + "\n", |
| 43 | + "yaml=YAML(typ='safe') # default, if not specfied, is 'rt' (round-trip)" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 3, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "name": "stdout", |
| 53 | + "output_type": "stream", |
| 54 | + "text": [ |
| 55 | + "{'countries': [{'name': 'India', 'capital': 'Delhi'}, {'name': 'Singapore', 'capital': 'Singapore'}]}\n" |
| 56 | + ] |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "config_data = yaml.load(open('config.yaml'))\n", |
| 61 | + "print(config_data)" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "markdown", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "Dump python classes to Yaml -> https://yaml.readthedocs.io/en/latest/dumpcls.html" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 4, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "name": "stdout", |
| 78 | + "output_type": "stream", |
| 79 | + "text": [ |
| 80 | + "- !User\n", |
| 81 | + " name: Anthon\n", |
| 82 | + " age: 18\n" |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + "source": [ |
| 87 | + "import sys\n", |
| 88 | + "import ruamel.yaml\n", |
| 89 | + "\n", |
| 90 | + "\n", |
| 91 | + "class User(object):\n", |
| 92 | + " def __init__(self, name, age):\n", |
| 93 | + " self.name = name\n", |
| 94 | + " self.age = age\n", |
| 95 | + "\n", |
| 96 | + "\n", |
| 97 | + "yaml = ruamel.yaml.YAML()\n", |
| 98 | + "yaml.register_class(User)\n", |
| 99 | + "yaml.dump([User('Anthon', 18)], sys.stdout)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [] |
| 108 | + } |
| 109 | + ], |
| 110 | + "metadata": { |
| 111 | + "kernelspec": { |
| 112 | + "display_name": "Python 3", |
| 113 | + "language": "python", |
| 114 | + "name": "python3" |
| 115 | + }, |
| 116 | + "language_info": { |
| 117 | + "codemirror_mode": { |
| 118 | + "name": "ipython", |
| 119 | + "version": 3 |
| 120 | + }, |
| 121 | + "file_extension": ".py", |
| 122 | + "mimetype": "text/x-python", |
| 123 | + "name": "python", |
| 124 | + "nbconvert_exporter": "python", |
| 125 | + "pygments_lexer": "ipython3", |
| 126 | + "version": "3.7.3" |
| 127 | + } |
| 128 | + }, |
| 129 | + "nbformat": 4, |
| 130 | + "nbformat_minor": 4 |
| 131 | +} |
0 commit comments