Reading JSON and YAML in Python
As a DevOps Engineer we should be able to parse files, be it txt, json, yaml, etc.
We should know what all libraries one should use in Pythonfor DevOps.
Python has numerous libraries like os
, sys
, json
, yaml
etc that a DevOps Engineer uses in day to day tasks.
Python is one of the most versatile programming languages, widely used in different fields, including web development, data science, machine learning, AI, and more. One of the main reasons for Python's popularity is its extensive collection of libraries. In this blog post, we will introduce some essential Python libraries that every Python developer should know.
1. NumPy
NumPy (Numerical Python) is the foundational package for numerical computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is especially useful for mathematical and logical operations, Fourier transforms, random number capabilities, and much more.
import numpy as np
a = np.array([1, 2, 3])
print(a)
2. Pandas
Pandas is a library built on top of NumPy, offering data structures and data manipulation tools to make data cleaning and analysis fast and easy in Python. It provides data structures like Series and DataFrame, which are perfect for handling and analyzing data.
import pandas as pd
data = {'Name': ['John', 'Anna', 'Peter'],
'Age': [28, 24, 33]}
df = pd.DataFrame(data)
print(df)
3. Matplotlib
Matplotlib is the most widely used data visualization library in Python. It allows you to create a variety of graphs, plots, and charts. It's also highly customizable, allowing you to create aesthetically pleasing visualizations.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y)
plt.show()
4. Scikit-learn
Scikit-learn is a powerful machine learning library. It provides simple and efficient tools for data analysis and modeling. It includes algorithms for classification, regression, clustering, dimensionality reduction, and more.
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(random_state=0)
X = [[ 1, 2, 3],
[11, 12, 13]]
y = [0, 1]
clf.fit(X, y)
5. TensorFlow
TensorFlow is an end-to-end open-source platform for machine learning developed by Google Brain Team. It's used for building and training deep learning models.
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
6. Requests
Requests is a simple and elegant library to send HTTP requests. It abstracts the complexities of making requests behind a beautiful, simple API, allowing you to send HTTP/1.1 requests.
import requests
response = requests.get('https://www.google.com')
print(response.status_code)
In conclusion, the above libraries are just a fraction of what Python has to offer. Each of these libraries has its unique features and uses that make Python an all-in-one programming language. Mastering these libraries can help you boost your productivity and ability to perform complex tasks with just a few lines of code.
Tasks
Create a Dictionary in Python and write it to a json File.
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
Read YAML file using python, file
services.yaml
and read the contents to convert yaml to jsonSee the task details
https://github.com/LondheShubham153/90DaysOfDevOps/tree/master/2023/day15
Happy Python coding!