Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yaml multi nested and python dictionary

Tags:

python

yaml

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}}}}} 
like image 269
Linus Avatar asked Sep 18 '12 00:09

Linus


People also ask

Does Saltstack use YAML?

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.

How do I create a nested YAML file in Python?

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.

Is nested dictionary possible in Python?

In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.

What is YAML Safe_load?

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.


1 Answers

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}}
like image 190
gvalkov Avatar answered Nov 11 '22 06:11

gvalkov