Table of contents
❄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-
Requests - To send HTTP requests (GET, POST, PUT, DELETE) and interact with RESTful APIs.
JSON - To parse JSON file.
YAML - To parse YAML file.
virtualenv - To create an isolated environment.
Pytest - To write test cases for your code.
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)
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 -
json.dump() - To write the JSON file.
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:
Import the json module.
Open json file using the open() function
Read jsonfile using json.load() function
Close the jsonfile using the close() function
Print the name of the services in the output
Services.json file -
Python script to read the json file and print the service names -
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:
We will open the YAML file with an open() function in the yaml module.
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.
Next, we will use json.dumps() function to convert the Python dictionary to json string.
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 -
Python script to read the Yaml file and convert it to json -
Thank you for reading! 📘
Happy learning... 😃