Python Data Persistence – Package
The concept of the package takes Python’s modular approach to next level. A package is a collection of modules. All modules having the functionality of certain common features are stored in one folder. Python’s standard library contains some packages that are collections of modules. For example, the Tkinter package, which is Python’s implementation of the TCL/Tk UI toolkit, consists of several modules such as Tkinter. ttk, Tkinter.messagebox, tkinter.commondialog, and so on. The importlib is another Python package in the standard library that has multiple modules (importlib. ABC, importlib. util, importlib. machinery, and so on.) You can build your own package having multiple but related modules under one folder. Let us see how it can be done.
In addition to Python modules, the folder should have a special file named as__init__.py for it to be recognized as a package by the Python interpreter. This file is also useful to build a packaging list of functions from various modules under the folder.
So, let us build a package named ‘MyPackage’. First of all, create a new folder at some suitable location in a filesystem (for example c:\testpackage) and a subfolder under it. Name the subfolder as ‘MyPackage’. Go on to save the following module scripts in it.
Example
#addfunctions.py def add ( num1 , num2 ) : result=num1 + num2 return result def adda11(*nums) : tt1 = 0 for num in nums: tt1=tt1+num return tt1
#mathfunctions.py def sum(x,y): return x+y def average(x,y): return (x+y)/2 def power(x,y): return x**y
#myfunctions.py def isprime(num): x=2 for x in range(2,num): if num%x==0: return False else: return True def'iseven(num): if num%2==0: return True else: return False def isleap(num): if num%4==0: return True else: return False
In addition to above, create an empty__init__. py in My package folder. The folder structure should appear as shown below (figure 3.17) :
C:\TestPackage | example.py | |_ __ _MyPackage addtunctions.py mathfunctions.py myfunctions.py __init___.py
Next, open the command prompt window while in the TestPackage directory and start Python. The child directory in it, MyPackage is recognized as a legitimate Python package because it does contain__init__.py, so you can import any module available in it. The usage is shown below:
C:\TestPackage> python >>>
Example
>>> from MyPackage import add functions >>> add functions.adda11(1,2,3,4) 10 >>> from MyPackage import functions >>> functions . is prime (67) True >>> from MyPackage import math functions >>> math functions.power(10,2) 100
What is more, it is possible to require a function by name from any module.
Example
>>> from MyPackage.math functions import power, average >>> power ( 5 , 4 ) 625 >>> average ( 20 , 30 ) 25.0
This is where__init__. py file plays an important role. It can provide package-level access to certain functions chosen from various modules. Let us modify the __init__. py script as shown below:
Example
#__init__.py from. mathfunctions import average from. addfunctions import adda11 from. myfunctions import isprime, iseven
Now, what does this__init__.py script do? It allows a function to be accessed by name of a package and not by name of a module which originally defined. To verify this behavior, save the following script as ‘example.py’ in the parent folder (‘testpackage’)
Example
#example.py import MyPackage ffcalling isprime function num=int ( input ( 1 enter a number . . ' ) ) retval=MyPackage . isprime ( num ) if retval==True: print ( " { } is a prime number " . format ( num ) ) else: print ( " { } is not a prime number " . format ( num) ) #adda11 function result=MyPackage.adda11( 10 , 20 ) print ( " Result of addition : " , result )
Output
C:\TestPackage>python example.py enter a number..23 23 is a prime number Result of addition: 30
In the above discussion, we learned how to create a package and use it from inside its parent folder. It is also possible to install the package for system-wide use. However, this book’s scope doesn’t cover its explanation. At this juncture, it is sufficient to state that it can be achieved by using a setup tools module from Python’s standard library. You can also upload your package to the Python Package Index repository (https://pypi.org//). The procedure is explained in detail on the website itself.
However, it is important to know how to install a new package in your computer’s Python library. The latest versions of Python come with pip utility.