Let's begin with YAML

Let's begin with YAML

Learn almost all the necessary concepts of YAML in one article only

Prerequisites:

You need to have at least a basic understanding of programming language and logical operators, It would be a plus if you know about dictionaries and lists of Python.

What is YAML?

What is YAML? The first question that clicks in one's mind, in order to begin with it. YAML stands for Yet Another Markup Language and it is used as data serialization language. YAML is case-sensitive and human readable. YAML is language independent as it supports multiple programming languages such as Python and Ruby.

Comments in YAML

As we know, there are comments in every programming language which contain a reference to the code and these comments aren't read by the computer.

We add comments in YAML by writing hash(#) and whitespace after it. Everything written after a hash(#) and whitespace is ignored by the interpreter/compiler.

# This is a good example of comment
#This is not a proper comment (Must add whitespace after hash(#))

Datatypes in YAML With Implementation

Data is always stored in the " Key : Value " pair in YAML, and always remember whitespaces between key, colon(:) and value. It is similar to dictionaries in Python. For example:

# Key : Value
number : 1
name : "Shyam"

Primarily, there are three data types in YAML: Scalar, Sequence & Mapping

  1. Scalar:

    Scalar is the basic datatype in YAML, where each key consists of a single value only. It is further classified into two datatypes: Numeric and String

    • Numeric

        num1 : 23        # integer
        num2 : 3.4       # float
        num3 : True      # Boolean
      
    • String: It can be written with and without quotes, but it is preferred that we write strings in quotes.

        name : "Shyam"  # Both outputs would be same
        name : Shyam
      
      • There are multiline strings also available, these are written by using pipe ( | ) symbol

          multline_str : | 
          This is an example
          of a mulitline
          string
        
  2. Sequence:

    The sequence is used to assign a group of values to a single key. It is similar to the lists used in python. These Lists (sequences) can be written in two styles: Flow style and Block style.

     ---
     # Three dashes are used to divide a YAML file into multiple docs
    
     # Flow style
     list1 : [1,2,3,4,5]
     list2 : ['Hii,','This','is','Shyam']
    
     ---
     # Block Style
     list3 : 
      - 1
      - 2
      - 3
     list4 :
      - 'Hii'
      - 'This'
      - 'is'
      - 'Shyam'
    
  3. Mapping:

    Mapping Data type is also known as Dictionary and it is similar to the dictionaries in Python. It is used where you want to store complex data. Here, each key consists of multiple keys and their associated values such as :

     server :
      username : shyam065
      user_id : 932745
    

    We can also add a sequence(list) inside the dictionaries :

     host :
         hostname : "thispc065"
         hostnumber : 02745
         accounts :       # List defined inside the dictionary
             - first
             - second
             - third
    

Validating YAML in Python File

We've created a file with the name config.yml

server :
    user1 :
        username : godavari
        userid : 34958038450435
        hostname : "thispc065"
        hostnumber : 02745
    user2 :
        username : kaveri
        userid : 934592857493854
        hostname : "thispc065"
        hostnumber : 02745

Now, we will execute & validate this file using the validate.py file

import yaml

with open('config.yml','r') as f:
        try:
            print(yaml.safe_load(f))
        except yaml.YAMLError as er:
            print(er)

Here, you can see the output as python dictionaries and dictionaries of the dictionary (Nested dictionaries)

As we saw in the above output our YAML file is working perfectly. So, we have learned YAML. Keep exploring and now you can try to create your own Docker-compose file with YAML as an advanced skill.

This was almost everything, that you need to know about YAML.

Thanks for your valuable time and please have a look at other blogs published by me.

Any sort of feedback or remark is highly appreciated. Feel free to mail me or you can connect with me on Twitter as well.