Sunday, March 6, 2022

Building a sample package

Now we will discuss how to build a package with one sample package example. We will build a masifutil package using the following modules and a sub-package:

• The mycalculator.py module: We already built this module for the Importing modules section.

• The myrandom.py module: This module was also built for the Importing modules section.

• The advcalc sub-package: This will be a sub-package and will contain one module in it (advcalculator.py). We will define an init file for this sub-package but it will be empty.

The advcalculator.py module has additional functions for calculating the square root and log using base 10 and base 2. The source code for this module is shown next:

# advcalculator.py with sqrt, log and ln functions

import math

def sqrt(x):

"""This function takes square root of a number"""

return math.sqrt(x)

def log(x):

"""This function returns log of base 10"""

return math.log(x,10)

def ln(x):

"""This function returns log of base 2"""

return math.log(x,2)

The file structure of the masifutil package with init files will look like this:




In the next step, we will build a new main script (pkgmain1.py) to consume the modules from the package or masifutil subfolder. In this script, we will import the modules from the main package and sub-package using the folder structure, and then use the module functions to compute two random numbers, the sum and difference of the two numbers, and the square root and logarithmic values of the first random numbers. The source code for pkgmain1.py is as follows:

# pkgmain0.py with direct import

import masifutil.mycalculator as calc

import masifutil.myrandom as rand

import masifutil.advcalc.advcalculator as acalc

def my_main():

""" This is a main function which generates two random\

numbers and then apply calculator functions on them """

x = rand.random_2d()

y = rand.random_1d()

sum = calc.add(x,y)

diff = calc.subtract(x,y)

sroot = acalc.sqrt(x)

log10x = acalc.log(x)

log2x = acalc.ln(x)

print("x = {}, y = {}".format(x, y))

print("sum is {}".format(sum))

print("diff is {}".format(diff))

print("square root is {}".format(sroot))

print("log base of 10 is {}".format(log10x))

print("log base of 2 is {}".format(log2x))

""" This is executed only if the special variable '__name__' is

set as main"""

if __name__ == "__main__":

my_main()

Here, we will be using the package name and module name to import the modules, which is cumbersome especially when we need to import the sub-packages. We can also use the following statements with the same results:

# mypkgmain1.py with from statements

from masifutil import mycalculator as calc

from masifutil import myrandom as rand

from masifutil.advcalc import advcalculator as acalc

#rest of the code is the same as in mypkgmain1.py

As mentioned earlier, the use of the empty __init__.py file is optional. But we have added it in this case for illustration purposes.

Share:

0 comments:

Post a Comment