Day 15: Python libraries for DevOps

Day 15: Python libraries for DevOps

#90daysofdevops

🔹Python Libraries:

A Python library is a collection of related modules. It contains bundles of code that can be used repeatedly in different programs. It makes Python Programming simpler and convenient for the programmer. As we don’t need to write the same code again and again for different programs.

Some Python Libraries are as follows -

1. numpy: Provides numerical computing capabilities, including arrays, linear algebra, Fourier transforms, and more.

2. pandas: Offers data manipulation and analysis tools, providing data structures like DataFrames for efficient data handling.

3. matplotlib: A popular data visualization library for creating charts, plots, and graphs.

4. scikit-learn: A machine learning library that provides tools for classification, regression, clustering, and more.

5. tensorflow and pytorch: Deep learning frameworks that enable building and training neural networks for tasks like image recognition and natural language processing.

6. flask and Django: Web development frameworks used to build scalable and dynamic web applications.

7. sqlalchemy: A powerful and flexible ORM (Object-Relational Mapping) library for working with databases.

8. pytest and unittest: Testing frameworks for writing and executing unit tests to ensure code quality and functionality.

9. requests: A library for making HTTP requests, used for interacting with web APIs.

10. beautifulsoup and scrapy: Libraries for web scraping and extracting data from HTML and XML documents.

Also, As a DevOps Engineer, you are correct that being able to parse various file formats, such as JSON and YAML, is an important skill. Python provides several libraries that can be used to handle these file formats. Let’s take a look at how to read JSON and YAML files in Python.

Difference Between json.dump() and json.load()

json.dump() is used to convert a Python object into a JSON string and write it to a file.

import json
data = {"name": "John", "age": 30, "city": "New York"}
with open("data.json", "w") as file:
 json.dump(data, file)

json.load() is used to read a JSON string from a file and convert it back into a Python object.

import json
with open("data.json", "r") as file:
 data = json.load(file)
print(data)

Reading JSON in Python: To read JSON files in Python, you can use the built-in json module. Here's an example of how to read a JSON file:

import json
# Open the JSON file
with open('data.json', 'r') as file:
    # Load the JSON data
    data = json.load(file)
# Access the data
print(data)

Reading YAML in Python: To read YAML files in Python, you can use the pyyaml library. You can install it using pip install pyyaml. Here's an example of how to read a YAML file:

import yaml
# Open the YAML file
with open('data.yaml', 'r') as file:
    # Load the YAML data
    data = yaml.safe_load(file)
# Access the data
print(data)

Additional Libraries for DevOps Tasks: In addition to json and pyyaml, there are several other Python libraries commonly used by DevOps Engineers. Some of these libraries include:

  • os: Used for interacting with the operating system, such as working with files, directories, and environment variables.

  • sys: Provides access to system-specific parameters and functions, allowing you to manipulate the Python runtime environment.

  • paramiko: A library for SSH protocol implementation, allowing you to securely connect to remote servers and execute commands.

  • requests: Used for making HTTP requests, making it useful for interacting with web APIs and services.

  • docker: A library for interacting with Docker, enabling you to manage Docker containers and images programmatically.

  • ansible: A powerful automation tool for configuration management, deployment, and orchestration.

  • terraform: A library for interacting with Terraform, a tool for infrastructure provisioning and management.


🔸Tasks:

Task 1) Create a Dictionary in Python and write it to a JSON file.

import json
dict = { "Name" : "Vishesh", "Age" : "19"}
json_object = json.dumps(dict, indent = 1)
print(json_object)

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

services.json
{
  "aws": {
    "name": "ec2"
  },
  "azure": {
    "name": "VM"
  },
  "gcp": {
    "name": "compute engine"
  }
}
import json
# Read the JSON file
with open('service.json', 'r') as file:
 data = json.load(file)
# Extract service names for each cloud provider
for provider, services in data.items():
 service_name = services.get('name')
 print(f"{provider} : {service_name}")

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

services.yaml
aws:
 name: ec2
azure:
 name: VM
gcp:
 name: compute engine
import yaml
import json
# Read the YAML file
with open(‘services.yaml’, ‘r’) as file:
 data = yaml.safe_load(file)
# Convert YAML to JSON
json_data = json.dumps(data)
# Print the JSON data
print(json_data)

Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

Linkedln

Twitter