Python Libraries for DevOps
📌Introduction to Python Libraries & its importance
A library is a collection of code that makes everyday tasks more efficient.
Python libraries offer pre-written code for reuse, saving time and effort. They enhance productivity by providing high-level abstractions and tools for common tasks.
Python's extensive library ecosystem makes it a versatile language for various applications. Python community offers support and resources for library usage. Many libraries are optimized for performance, making Python competitive for various tasks.
Not only Libraries are designed to work well together, ensuring compatibility but also Libraries implement state-of-the-art techniques, fostering innovation as well and Open-source libraries encourage collaboration and customization by providing access to source code.
👉Popular Python libraries and tools for automating DevOps processes
Pandas
Selenium
Pytest
BrowserStack Automate
TensorFlow
Jenkins
SciPy
Behave
Ansible
Beautiful Soup
📌Python library that helps pass different types of files
📃TXT (Text) Files
📃JSON (JavaScript Object Notation)
📃YAML (YAML Ain't Markup Language)
👉Reading JSON and YAML in Python
As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc.
You should know what libraries one should use in Python for DevOps.
Python has numerous libraries like os, sys, json, yaml etc that DevOps Engineer use in day-to-day tasks
📃TXT (Text) Files:
A TXT file, short for "text file," is a simple and widely used file that contains human-readable text.
They don't have any particular structure or format, other than the characters themselves.
TXT files are commonly used to store documents, plain text annotations, and other types of text data.
Example of a TXT file (example.txt):
Under the clear, starry night, the campfire crackled, filling the air with warmth and stories.
📃JSON (JavaScript Object Notation):
The
json.dumps()
function in Python is used to serialize (or encode) a Python object into a JSON-formatted string.It stands for "JSON (JavaScript Object Notation) dump string."
This function is part of Python's standard library
json
module and is commonly used when you want to convert a Python data structure, such as a dictionary or a list, into a JSON string for various purposes, like transmitting data over the web or saving it to a file.Create a Dictionary in Python and write it to a JSON File.
Here's a step-by-step guide to creating a dictionary in Python and writing it to a JSON file( File name: demojson.py ):
Step 1: Import the JSON module.
import json
Step 2: Create a Dictionary
data = {
"name": "Vyankatesh",
"age": 25,
"isStudent": False,
"address": {
"street": "123 Main St",
"city": "Delhi"
},
"languages": ["Python", "JavaScript", "Java"]
}
Step 3: Write the dictionary to a JSON file.
with open('data.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
# Print the loaded data
print(data)
Here's the complete code from above steps:
import json
data = {
"name": "Vyankatesh",
"age": 25,
"isStudent": False,
"address": {
"street": "123 Main St",
"city": "Delhi"
},
"languages": ["Python", "JavaScript", "Java"]
}
with open('data.json', 'w') as json_file:
json.dump(data, json_file, indent=4)
# Print the loaded data
print(data)
After running this code, you'll find a file named data.json in the same directory as your Python script.
We can use the JSON module to read the provided JSON data and then print the service names of each cloud service provider. Here's how you can do it:
Step 1: Create a task as a 2.py file and write the below code.
import json
# JSON data
json_data = '''
{
"services": {
"debug": "on",
"aws": {
"name": "EC2",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"azure": {
"name": "VM",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"gcp": {
"name": "Compute Engine",
"type": "pay per hour",
"instances": 500,
"count": 500
}
}
}
'''
# Parse JSON data
data = json.loads(json_data)
# Get and print service names
cloud_providers = ["aws", "azure", "gcp"]
for provider in cloud_providers:
service_name = data["services"][provider]["name"]
print(f"Cloud Provider: {provider.upper()}, Service Name: {service_name}")
Step 2: Now run it, you'll see the service names for each cloud service provider (AWS, Azure, and GCP) printed in the console.
Looking it up, .txt files are just plain text storage. While .yaml files offer a human-friendly configuration format with the capability for complicated structures, json files enable structured data interchange.
The particular use case, as well as the type of structure and readability needed for the data being transferred or saved, will determine the format to be used.
Many congratulations 🏉! You learn about Python Libraries.
I hope you enjoy the blog post!
If you do, please show your support by giving it a like ❤, leaving a comment 💬, and spreading the word 📢 to your friends and colleagues 😊