Tuesday, March 1, 2022

Writing reusable modules

 For a module to be declared reusable, it has to have the following characteristics:

• Independent functionality

• General-purpose functionality

• Conventional coding style

• Well-defined documentation

If a module or package does not have these characteristics, it would be very hard, if not impossible, to reuse it in other programs. We will discuss each characteristic one by one.

Independent functionality

The functions in a module should offer functionality independent of other modules and independent of any local or global variables. The more independent the functions are, the more reusable the module is. If it has to use other modules, it has to be minimal.

In our example of mycalculator.py, the two functions are completely independent and can be reused by other programs:



In the case of myrandom.py, we are using the random system library to provide the functionality of generating random numbers. This is still a very reusable module because the random library is one of the built-in modules in Python:


In cases where we have to use third-party libraries in our modules, we can get into problems when sharing our modules with others if the target environment does not have the third-party libraries already installed.

To elaborate this problem further, we'll introduce a new module, mypandas.py, which will leverage the basic functionality of the famous pandas library. For simplicity, we added only one function to it, which is to print the DataFrame as per the dictionary that is provided as an input variable to the function.

The code snippet of mypandas.py is as follows:

#mypandas.py

import pandas

def print_dataframe(dict):

"""This function output a dictionary as a data frame """

brics = pandas.DataFrame(dict)

print(brics)

Our mypandas.py module will be using the pandas library to create a dataframe object from the dictionary. This dependency is shown in the next block diagram as well:


Note that the pandas library is not a built-in or system library. When we try to share this module with others without defining a clear dependency on a third-party library (pandas in this case), the program that will try to use this module will give the following error message:

ImportError: No module named pandas'

This is why it is important that the module is as independent as possible. If we have to use third-party libraries, we need to define clear dependencies and use an appropriate packaging approach. This will be discussed in the Sharing a package topic in later blog.












Share:

0 comments:

Post a Comment