Python Libraries #Day15 of 90DaysofDevOps

Python Libraries for DevOps

Python Libraries:

Python libraries are collections of pre-written code modules that are linked together. It has code bundles that can be used again and again in different programs.

  • As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc.

  • Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks.

JSON and YAML in Python

  • As DevOps engineers, we will be working to parse a file into JSON, YAML, and plain TXT formats in Python.

  • Python has numerous libraries like json, yaml, boto3, os, etc that a DevOps Engineer uses in day-to-day tasks. Today, we mainly focus on JSON and yaml.

What is YAML and how is YAML used in Python?

YAML is a human-readable data serialization format similar to JSON, often used for configuration files and structured data representation. It uses indentation and key-value pairs to organize and represent data.

Example:

#YAML data
person:
  name: Namrata
  Profession: DevOps engineer
  city: Banglore

yaml is not a standard library, you will need to the PyYAML package to work with YAML data.
With the PyYAML library, you can read YAML data and convert it into Python objects and vice versa.

Example:

import yaml

 # YAML data
 data = '''
 name: Namrata
 profession: DevOps engineer
 city: Banglore
 '''

 # Parse YAML data to Python dictionary
 parsed_data = yaml.safe_load(data)
 print("Parsed YAML data:", parsed_data)

 # Convert Python dictionary to YAML data
 new_data = {'name': 'Namrata', 'profession': DevOps engineer, 'city': 'Banglore'}
 yaml_data = yaml.safe_dump(new_data)
 print("YAML data:", yaml_data)

What is JSON and how is JSON used in Python?

JSON stands for JavaScript Object Notation and is a lightweight format for storing and transporting data. It is often used when data is sent from a server to a web page. JSON is "self-describing" and easy to understand.

Example:

[
{
"name" : "NamG",
"profession" : "DevOps Engineer",
"city" : "Banglore",
}
]

json is a built-in Python library that provides methods to work with JSON (JavaScript Object Notation) data. It allows you to encode Python objects into JSON format (serialization) and decode JSON data into Python objects (deserialization). DevOps Engineers often use JSON to represent configuration data and exchange data between different systems.

Example:

import json

 # Sample JSON data
 data = '{"name": "NamG", "profession": "DevOps Engineer", "city": "Banglore"}'

 # Decode JSON data to Python dictionary
 parsed_data = json.loads(data)
 print("Decoded JSON data:", parsed_data)

 # Encode Python dictionary to JSON data
 new_data = {"name": "NamG", "profession": "DevOps Engineer", "city": "Banglore"}
 encoded_data = json.dumps(new_data)
 print("Encoded JSON data:", encoded_data)

📌Tasks

  1. Create a Dictionary in Python and write it to a JSON File.

  2. Read a JSON file services.json kept in this folder and print the service names of every cloud service provider.

     output
    
     aws : ec2
     azure : VM
     gcp : compute engine
    

  3. Read YAML file using Python, file services.yaml and read the contents to convert yaml to json.

We can read the YAML file using the PyYAML module’s yaml.load() function. This function parse and converts a YAML object to a Python dictionary (dict object).

To install the PyYAML module, you’ll need to run the pip command, which is the package installer for Python. The below command installs the PyYAML module.

PS C:\Users\namratak\Linux\python_task> pip install pyyaml

Convert yaml to json:

Thank you for reading, and I hope it will be helpful in your DevOps journey. Happy Learning !!!😊💥🚀