I am new to yaml the output is in the expected format but wondering if there is a better way to construct the below nested objects in yaml.
import yaml
yaml.load ("""
test1:
a:
a1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
b1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
c1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
b:
a1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
b1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
c1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
c:
a1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
b1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
c1:
a2:
a3: 0
b3: 0
c3: 0
b2:
a3: 0
b3: 0
c3: 0
c2:
a3: 0
b3: 0
c3: 0
""")
output:
'test1': {'a': {'a1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}, 'c1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}, 'b1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}}, 'c': {'a1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}, 'c1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}, 'b1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}}, 'b': {'a1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}, 'c1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}, 'b1': {'c2': {'c3': 0, 'a3': 0, 'b3': 0}, 'a2': {'c3': 0, 'a3': 0, 'b3': 0}, 'b2': {'c3': 0, 'a3': 0, 'b3': 0}}}}}
Welcome to the world of Saltstack Salt. Simply put, it is a configuration management system written in Python and controlled using YAML with Jinja templating.
Creating a nested YAML file in python is relatively simple. First, you need to create a dictionary containing all the data you want to store in the YAML file. This dictionary contains three keys: 'foo', 'baz', and 'nested'. The value associated with the 'nested' key is a dictionary containing two key-value pairs.
In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.
Loading a YAML Document Safely Using safe_load() safe_load(stream) Parses the given and returns a Python object constructed from the first document in the stream. safe_load recognizes only standard YAML tags and cannot construct an arbitrary Python object.
I can think of two ways that will save you some typing on the yaml front.
Use the short mapping syntax:
test1:
a:
a1:
a2: {a3: 0, b3: 0, c3: 0}
b2: {a3: 0, b3: 0, c3: 0}
c2: {a3: 0, b3: 0, c3: 0}
b1:
a2: {a3: 0, b3: 0, c3: 0}
b2: {a3: 0, b3: 0, c3: 0}
c2: {a3: 0, b3: 0, c3: 0}
c1:
a2: {a3: 0, b3: 0}
Use aliases:
entry: &aliasentry
a2: {a3: 0, b3: 0, c3: 0}
b2: {a3: 0, b3: 0, c3: 0}
c2: {a3: 0, b3: 0, c3: 0}
test1:
a:
a1: *aliasentry
b1: *aliasentry
c1: {a2: {a3: 0, b3: 0}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With