📍Introduction
📚Welcome to Part 3 of the Python for DevOps Blog Series!🐍
In this blog, we will learn about Modules, Packages, Libraries, and File handling in Python. We will be learning Python from the basics to all the necessary things you will require for all your DevOps tasks. Let's unlock Python's potential in DevOps together!🌟
📍Modules & Libraries
✔Modules
Modules are a block consisting of Python code that has some functions, classes, and tasks written, and well-tested inside it. These modules come in very handy while writing a large codebase where you don't want to write every minute functionality from scratch. There are 2 types of modules in Python:
Built-In Modules - They are provided along with Python which contains various data structures and code blocks with various functionalities. Eg: os module, random module, math module, statistic module, time module, etc.
External Modules - These Modules are created by the people so that while writing Python code the developers can use the functions in these modules while creating large projects. They are to be downloaded externally using the
pip install <module_name>
command. Eg: flask, sklearn, matplotlib, etc.
You can use the modules by using the import
command, type import <module_name>
at the top of your Python code file.
It is not feasible to know all the functions provided by all the modules therefore, you can see the module's documentation from Google. You can also create your module inside a file with a .py
extension.
✔Package
Packages are folders that contain a collection of related modules. It contains an init.py
file inside that makes the folder a Python package. You can import the modules from a package using the command from <package_name> import <module_name>
.
✔Libraries
Libraries in Python contain a collection of Python packages and modules. Libraries are very helpful for developing large-scale projects. Python has a large number of libraries useful for various fields making it popular for developing. Fields like Machine Learning, Data Science, Data visualization, DevOps, etc. Python is widely used.
import <library_name> # To use libraries in Python
from <library> import <module_name> # To import a specific module from the library.
from <library> import * # To import all the modules inside the library.
Popular Python Libraries are:
NumPy: A library for numerical computing, linear algebra, and statistical operations providing support for arrays and matrices and numerous mathematical functions.
Pandas: A library for data manipulation and analysis, used for data visualization, statistical analysis, missing data handling, and offers data structures like DataFrames that make it easy to work with structured data.
Matplotlib: A powerful plotting library that enables the creation of various types of charts and visualizations, 3D plotting, and animations (animated visualizations).
TensorFlow and PyTorch: Libraries for machine learning and deep learning, offering tools to build and train neural networks.
Django and Flask: Web frameworks that simplify web development by providing tools for routing, templating, and handling requests.
os and shutil: Libraries for file and directory management, providing functionalities like file operations, path manipulation, and more.
📍File Handling
Python has built-in file-handling functions. Here are some of the file-handling functions:
open(<filename>, <operation>)
: It is used to open files inside Python. It takes in 2 arguments:<1st argument>
- filename (should be present inside the folder where your Python file resides)<2nd argument>
- which operation do you want to perform on the file it can beread -
'r'
write -
'w'
(it will overwrite the contents in the file)append -
'a'
(it will add the text at the end of the content present in the file.create -
'x'
(creates a file and throws an error if the file already exists)
I will create a text file named file.txt
and put some content in it.
f = open('file.txt', 'r') # This will open the file in read mode
file_data = f.read() # It will read the content in the file and return it.
print(file_data)
f.close # To close the opened file.
Output:
f = open('file.txt', 'w')
f.write("This content is overwritten since this file was opened in the write mode.")
f.close()
After executing the above code you will see the content was written inside the file:
with
: It is another way to handle files this is more used since we have to write less.with open('file.txt', 'a') as f: f.write("\nThis is an example to handle files using with statement.\nThis content will be appended to the existing content since the file is opened in the append mode.") # when we use with we do not need to close the file. # the indentation (space is important) # \n is used to print the content in the next line.
Output:
📍Reading a JSON file
To read a JSON file in Python we have to use an in-built module by doing:
import json
{
"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
}
}
}
Before we dive into how to read a JSON file, let's understand 2 methods/functions inside the JSON module, which will be very useful for reading the JSON file.
loads()
: It converts the JSON string into a Python dictionary. JSON string means simply the above content in the JSON file has been put under a string using''
. And this JSON string is given as a<argument>
to the loads() function.import json x = '{"name": "varun", "age": 21, "gender": "male"}' # This is a string # You can do print(type(x)) output will be <class 'str'> y = json.loads(x) # loads() is a method inside the json module. print(type(y)) print(y)
Output:
dumps()
: It is the opposite ofloads()
,dumps()
converts a Python dictionary into a JSON file.import json x = {"name": "varun", "age": 21, "gender": "male"} print(type(x)) print(x) y = json.dumps(x) print(y) print(type(y)) # This will give string but it is a JSON string
Output:
✔Task:
Read the above JSON file and print the service names of every cloud service provider.
import json # This is a built-in library in Python
with open("services.json", 'r') as f:
json_data = json.loads(f.read())
print(json_data)
print(type(json_data))
# f.read() will return the content in the JSON file
# json.loads() will convert the JSON content into a dictionary and will be stored in json_data.
# make sure that the above JSON file is inside the folder where this Python file resides
Output:
📍Conclusion
Thank you for reading this blog! 📖 Hope you have gained some value.
If you enjoyed this blog and found it helpful, please give it a like 👍, share it with your friends, do share your thoughts, and give me some valuable feedback.😇 Don't forget to follow me for more such blogs! 🌟