Day-15 Task: Python Libraries for DevOps

Day-15 Task: Python Libraries for DevOps


1. Python In-Built Libraries
A Python library is a collection of pre-written code that provides specific functionalities and can be imported and used in other Python programs. A library can contain functions, classes, and modules that can be used by other programs, saving time and effort in development.

Python libraries are often created to solve specific problems and make it easier for developers to write code by providing pre-built solutions. They can be open-source or proprietary and can be installed via Python’s package manager, pip.

In DevOps, Python libraries can be used to automate tasks, interact with APIs and cloud services, and manipulate data, among other things. Common DevOps tasks that can be facilitated by Python libraries include server configuration and management, software deployment, testing, and monitoring.

2. JSON File
JSON stands for “JavaScript Object Notation”, but it’s not limited to just JavaScript and can be used in many programming languages. A JSON file is a file format used for storing and exchanging data.

JSON files are composed of key-value pairs, where the keys are strings and the values can be strings, numbers, arrays, objects, booleans, or null.

Usage: JSON is often used to transfer data between web applications and servers as it is lightweight, easy to read and write, and can be parsed by most programming languages.

Extension: .json

Example of a JSON file:

{
  "name": "XYZ",
  "age": 100,
  "email": "abcde@gmail.com",
}
Enter fullscreen mode

Exit fullscreen mode

3. YAML File
YAML stands for “YAML Ain’t Markup Language” which uses indentation to represent data hierarchies and is designed to be easy to read and write by humans.

Like JSON, YAML is a key-value-based format, but it has a more relaxed syntax and can handle more complex data structures, including lists and nested objects.

Usage: YAML file is used for configuration files and data exchange between different programming languages.

Extension: .yaml or .yml

Example of a YAML File:
name: XYZ
age: 100
email: abcde@gmail.com
Enter fullscreen mode

Exit fullscreen mode

JSON in Python

In Python, JSON data is represented as String. We use the JSON module for this.

Below two methods is used to convert Python to JSON-

i) dumps(): Converts Python file to JSON format without creating file

ii) dump(): Converts Python file to JSON format by creating a file

Below two methods are used to convert JSON to Python-

i) loads(): Convert JSON to Python without reading from file

ii) load(): Convert JSON to Python by reading from file. json.load() takes a file object and returns the JSON object.

4. Tasks
1.Create a Dictionary in Python and write it to a json File.

import json

# Create a dictionary
data = {
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

# Specify the file path
file_path = "data.json"

# Write the dictionary to a JSON file
with open(file_path, "w") as json_file:
    json.dump(data, json_file)

print("Dictionary written to JSON file successfully.")

Enter fullscreen mode

Exit fullscreen mode

In this example, the data dictionary contains some sample data. The json.dump() function is used to write the dictionary to the specified file path. Make sure to replace “data.json” with the desired file path where you want to save the JSON data.

After running this code, you’ll find a file named data.json in the same directory as your Python script, containing the JSON representation of the dictionary.

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
Enter fullscreen mode

Exit fullscreen mode

services.json file:

Create a services.json file in the same directory with the following content:

code{
    "aws": "ec2",
    "azure": "VM",
    "gcp": "compute engine"
}
Enter fullscreen mode

Exit fullscreen mode

Now, you can use the following Python code to read the JSON file and print the service names of each cloud service provider:

import json

# Read the JSON file
file_path = "services.json"

with open(file_path, "r") as json_file:
    data = json.load(json_file)

# Print service names for each cloud service provider
for provider, service in data.items():
    print(f"{provider} : {service}")
Enter fullscreen mode

Exit fullscreen mode

Make sure that the services.json file is in the same directory as your Python script. After running the code, you should see the desired output:

aws : ec2
azure : VM
gcp : compute engine
Enter fullscreen mode

Exit fullscreen mode

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

1.Install the pyyaml library if you haven’t already:

pip install pyyaml
Enter fullscreen mode

Exit fullscreen mode

Create a services.yamlfile in the same directory with the following content:

aws: ec2
azure: VM
gcp: compute engine
Enter fullscreen mode

Exit fullscreen mode

Use the following Python code to read the YAML file, parse it, and then convert it to JSON:


import yaml
import json

# Read the YAML file
file_path = "services.yaml"

with open(file_path, "r") as yaml_file:
    yaml_data = yaml.safe_load(yaml_file)

# Convert YAML data to JSON
json_data = json.dumps(yaml_data, indent=4)

print(json_data)

Enter fullscreen mode

Exit fullscreen mode

In this code, the yaml.safe_load() function is used to parse the YAML file into a Python dictionary. Then, the json.dumps() function is used to convert the dictionary to JSON format with proper indentation.

After running the code, you’ll get the JSON representation of the YAML data printed in the console.



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.