Day15 of #90DaysOfDevOps Challenge

Python Libraries

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 the 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... 😃