Python Libraries for DevOps [Day 15 Task]

Python Libraries for DevOps [Day 15 Task]

1. What Are Python Libraries?

Python's syntax, semantics, and tokens are all contained in the Python Standard Library. It comes with built-in modules that give the user access to basic functions like I/O and a few other essential modules. The Python libraries have been written in the C language for the most part. There are over 200 core modules in the Python standard library. Below are some of Python's common libraries:

  • Numpy: NumPy is one of the most widely used open-source Python packages, focusing on mathematical and scientific computation. It has built-in mathematical functions for convenient computation and multidimensional data. NumPy is recommended over lists because it uses less memory, is faster, and is more convenient.

    Images, sound waves, and other binary raw streams can be represented as a multidimensional array of real values using the NumPy interface for visualization

  • Pandas: Pandas is an open-source library licensed under the Berkeley Software Distribution (BSD). In the domain of data science, this well-known library is widely used. They're mostly used for analysis, manipulation, and cleaning of data, among other things. Pandas allows us to perform simple data modeling and analysis without having to swap to another language like R.

  • ScyPy: Scipy is a Python library. It is an open-source library, specially designed for scientific computing, information processing, and high-level computing. A large number of user-friendly methods and functions for quick and convenient computation are included in the library. Scipy can be used for mathematical computations alongside NumPy.

  • Requests, Beautiful Soup4, and Smtplib. If you are dealing with a task where you need to make a request to an HTTP endpoint, or check the content, or send an email then you can use these three respective modules.

    • Request*: Request is used to make a request to an HTTP endpoint.*

    • BeautifulSoup4*: is used to parse HTML or XML document.*

    • Smtplib*: Is used for sending and receiving email using Simple Mail Transfer Protocol(SMTP)*

2. Hands-On Tasks:

  • Create a Dictionary in Python and write it to a JSON File.

    First import the JSON (a library in Python that is used for JSON files) into the module.

    Then define a dictionary and print it just to show the output.

    we have an inbuilt module called 'JSON' which has an in-built function called dumps(). The dumps() function is used to convert dict to JSON Python.

    Syntax: **json.dumps(dictionary, indent) [**the number of units for indentation].

    • 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

This is just a JSON file created to print the name of the cloud provider and its respective services.

we have used the open() function to read the json file. Then, the file is parsed using json.load() method which gives a dictionary named json_data.

Then just print the json_data [From the output can see].

Later ,Used a for loop which will print the name and services from json_data one by one as per the output .

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

This is a YAML file that will print its content as a JSON file.

  • Install yaml library by using the command pip install pyyaml

  • First will open a yaml file in write mode using the open() function.

    Then it will convert the yaml to a python dictionary using the load() method defined in the YAML module.

    After opening the file, it will convert the Python dictionary obtained from the yaml into the json file. For this, we will use the dump() method defined in the JSON module

Output