Friday, January 10, 2014

Basic part-7 | Creating modules & packages

Hello... Friends, Today we are going to discus about creating modules & packages in python. as the topic module what actually a module is, a module is file containing Python definitions and statements of functions,variables,or classes. A module can contain executable statements as well as function definitions. if we make it more simpler then we can say that a module is a better way of organizing code

before using module in our scripts we need to import it by using word import module_name where what is module_name the module name is nothing but the python coded script in which you are calling classes, functions ,variables etc.
So let's understand it in better way practically

So here we discus about build-in modules as well how to create a module in python.
there are various build in module in python
if you want to see the list of built in modules then just open up your python shell & type

>> help ('modules')
& you get the list of modules in python


as you want to use any module then you just need to import them & then use them in your code let's more clear it as here i am calling the module math & then as i want to see about what are the functions inside this module then you can see list of all functions
like wise i am calling module email & same function list inside email module.


So can you understand something about modules in python if not then don't worry i'll clear more about it ....
Let's now we need to create our own module & work with it Let's moves on

let we have our previous coded script which we create in classes with calc.py
in this modules tut i want to take output from other script names calc-module.py & in this script we are importing calc script so here i am using calc script module to get the result . Suppose we have a list of modules in  a script & we want to call them again & again at different different places then how we work with them ? we just need to create a script with those modules & can call these modules simply in different different scripts.


suppose here i am calling module Quickmul() using calc script in calc-module.py script by simply importing calc script as a module in calc-module.py script


 now let's check the script is working fine or not run the script. after making it executable wow we get the result as we want Quick multiply of two number where a=24 & b= 35 we get the result 840 which is our right answer


So we learn how to create our own module & how to call it in your another script or we can say how to use functionality of one script in other one script.

Exercise : 
1.  Use the same script & try to call any other function in your script like Quickadd & scientific power function in calc-module.py .

2. Create your own file with multiple functions & try to call them from one file to another file .

package : 

packages are nothing just a file directory structure so that we can organize our codes mainly these both topics are used when we have a very lengthy code or we need to call a single code in different different scripts so we simply make the package of the code & use them in a directory file.

Let's understand it how to create a package in python so what we need to create a package of code :
we just simply need to add the script into a directory & then what we need is just create a file with neme __init__.py in the same directory in which we have our scripts so follows these steps

step 1: create a directory, here my directory is calcpackage copy the calc.py in it as
# cp calc.py /calcpackage 
step 2: change directory & create a file with name __init__.py & in write code what you want to imported from the calc.py , these Calculator , Scientific are classes which we want to import & use for our final script


Step 3: Next create an another script in the one directory up with the name calcpack.py this is our final script & what we do in this script let me explain
we are importing our calc script , now define a variable with name rb store the Calculator function in it & just print the result. save this script.


Step 4: now run this script to get the result as values are 10, 20 & there sum is 30

So this is all how to create our own module, bind them & calling modules in a package ... very simple scripts 

No comments:

Post a Comment