Day15 of #90DaysOfDevOps Challenge
Python Library

Day15 of #90DaysOfDevOps Challenge

❄Python Libraries:

The collection of modules used in Python while writing different programs is known as libraries. With the help of these libraries, we can write the most complex programs in Python.

As a DevOps engineer, when we are writing the automation scripts for the build, test, and deployment of our application, there are a lot of libraries available in Python which make our task possible-

  1. Requests - To send HTTP requests (GET, POST, PUT, DELETE) and interact with RESTful APIs.
  2. JSON - To parse JSON file.
  3. YAML - To parse YAML file.
  4. virtualenv - To create an isolated environment.
  5. Pytest - To write test cases for your code.
  6. Boto3 - Allows managing AWS infrastructure using Python script.


❄Tasks:

1️⃣Task- Create a Dictionary in Python and write it to a JSON File.

Here I have used the JSON module to convert the dictionary to JSON. As we know the JSON format is very similar to the dictionary in Python. Both store the objects in key-value pair under {}.

Example:

import json
dict = { "Name" : "Anna", "Age" : "20", "ID" : "1" }
json_object = json.dumps(dict, indent = 1)
print(json_object)        
No alt text provided for this image

2️⃣Task - Read a json file services.json kept in the folder and print the service names of every cloud service provider.

output

aws : ec2
azure : VM
gcp : compute engine
        

There are two essential functions of the JSON module -

  1. json.dump() - To write the JSON file.
  2. json.load() - To read the JSON file.

Here we will use json.load function to read the json file.

import json

with open("service.json") as jsonfile:
  jsonObject = json.load(jsonfile)
  jsonfile.close()
print(jsonObject)
print(output)
print("aws : ", jsonObject["services"]["aws"]["name"])
print("aws : ", jsonObject["services"]["azure"]["name"])
print("aws : ", jsonObject["services"]["gcp"]["name"])        

Steps:

  1. Import the json module.
  2. Open json file using the open() function
  3. Read jsonfile using json.load() function
  4. Close the jsonfile using the close() function
  5. Print the name of the services in the output

Services.json file -

No alt text provided for this image

Python script to read the json file and print the service names -

No alt text provided for this image

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

To convert the yaml file to json, we will first convert it to a Python dictionary and then to a json string.

Steps:

  1. We will open the YAML file with an open() function in yaml module.
  2. We will use yaml.load() function in the yaml module to take the yaml file as input and convert it to a Python dictionary. We mention the type of loader in the Loader argument.
  3. Next, we will use json.dumps() function to convert the Python dictionary to json string.
  4. At last, we will print the json string to get the output.

import json  
import yaml 
from yaml import SafeLoader  
with open("service.yaml") as yaml_file:  
  py_dict = yaml.load(yaml_file, Loader=SafeLoader )      
  json_object = json.dumps(py_dict, indent=4)

print("Yaml is converted to json", json_object)        

service.yaml file -

No alt text provided for this image

Python script to read the Yaml file and convert it to json -

No alt text provided for this image

Thank you for reading! 📘

Happy learning... 😃


To view or add a comment, sign in

More articles by Aishwarya keshri

  • Day26 of #90DaysOfDevOps Challenge

    Jenkins Pipeline: Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery…

  • Day24 of #90DaysOfDevOps Challenge

    ❄️Project: Containerise and deploy a node.js application using Jenkins Job.

  • Day23 of #90DaysOfDevOps Challenge

    CI/CD Continuous integration and continuous delivery help in automating the software development lifecycle stages…

    2 Comments
  • Day22 of #90DaysOfDevOps Challenge

    ❄️Jenkins Jenkins is an open-source tool that helps in creating pipelines and automating the software development…

  • Day21 of #90DaysOfDevOps Challenge

    Important interview questions and Answers for Docker: 1. What is the difference between an Image, Container and Engine?…

  • Day20 of #90DaysOfDevOps Challenge

    Docker Cheat Sheet: 🔹Docker images- Show all locally stored top-level images: docker images Pull an image from a…

  • Day 19 of #90DaysOfDevOps Challenge

    Docker Volumes When we are working on docker, the data we store gets lost when the container is destroyed. So, to…

    2 Comments
  • Day 18 of #90DaysOf DevOps Challenge

    ❄Docker Compose Using docker commands, we can only run and manage a single container at a time, but there can be…

  • Day17 of #90DaysOfDevOps Challenge

    Dockerfile: Instead of manually creating docker images by running multiple commands one by one, we can write a script…

    1 Comment
  • Day 16 of #90DaysOfDevOps Challenge

    ❄Docker: Docker is a containerization tool that helps us create a lightweight container with all the required packages…

Explore content categories