Tuesday, 24 January 2017

MODULES IN PYTHON

Every python file itself is known as module. A module can contain variables, functions and classes. In order to use the properties of one module into another module we use the ‘import’ statement

Example –

mod1.py
x=100
def f1():
    print 'in f1 of mod1'
def f2():
    print 'in f2 of mod1'

mod2.py
import mod1
print 'hi'
print mod1.x
mod1.f1()
mod1.f2()
print 'end'

Output –
hi
100
in f1 of mod1
in f2 of mod1
end

Note – Whenever we import one module into another module then imported module compiled file will be generated and stored that file into computer hard-disc permanently. After generating the compiled file for python module without having the .py file of that module we can import module into other module.


Renaming a module at the time of input – Whenever we import a module to access the properties of that module, compulsory we have to use modulename.propertyname

Example –

mod3.py
def add(a,b):
    return a+b
def sub(a,b):
    return a-b
def abs(a):
    if a>=0:
        return a
    else:
        return -a

mod4.py
import mod3 as m
sum=m.add(30,20)
print sum
sub=m.sub(30,20)
print sub
abs=m.abs(-10)
print abs

Output –
50
10
10


Math module – In-built module, all properties are available

Example –
import math
print 'The value of PI is : ',math.pi

Output –
The value of PI is :  3.14159265359


from…import – Inorder to import the specific properties of one module into another module we use from…import.

Example –
from math import pi,e
print 'The value of PI is : ',pi
print 'The value of e is : ',e

Output –
The value of PI is :  3.14159265359
The value of e is :  2.71828182846

Whenever we import the properties of one module into another module by using from import then we can access the properties of that module directly without using module name or alias name.


import – import is used to import all the properties of the module.

Example –
from math import *
print 'The value of PI is : ',pi
print 'The value of e is : ',e

Output –
The value of PI is :  3.14159265359
The value of e is :  2.71828182846

For every module by default some properties are added automatically.
__builtins__, __doc__, __file__, __package__, __name__

Example –
x=1000
def f1():
    print 'In f1 of if3'
print __builtins__
print __doc__
print __file__
print __package__
print __name__
f1()
print x

Output –
<module '__builtin__' (built-in)>
None
C:/Users/om/PycharmProjects/Pythonn/if3.py
None
__main__
In f1 of if3
1000


Dir function – Dir function is used to know the properties of the current module or specified module.

Example –
x=100
y=200
def 
f1():
    print 'In f1 of if3'
print dir()

Output –
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'f1', 'x', 'y']


Module Search Path – Whenever we import a module in python, python interpreter will search for that module in the following locations – current directory, python path, installation dependent default directory.

Note – We can find out the python path by using path variable of sys module.

from sys import *
print path
 
 
Reloading a module – Whenever we import one module more than one’s 
then by default that module will be imported only once.
 
Example –
 
mod5.py
print 'beginning of mod5'
print 'hello'
print 'bye'
print 'end of mod5'
 
mod6.py
import mod5
import mod5
import mod5
import mod5
import imp
imp.reload(mod5)
 
Output –
beginning of mod5
hello
bye
end of mod5
beginning of mod5
hello
bye
end of mod5
 
Note – Whenever we want then we can reload the module by calling reload 
function of imp module.
 
Example –
import os
# WHERE ARE WE ?
cwd = os.getcwd()
print '1',cwd
# GO FORWARD
os.chdir('abc')
print '2',os.getcwd()
# GO BACK
os.chdir(os.pardir)
print '3',os.getcwd()
 
Output –
1 C:\Users\om\PycharmProjects\Pythonn
2 C:\Users\om\PycharmProjects\Pythonn\abc
3 C:\Users\om\PycharmProjects\Pythonn
 
Example –
import os
samp=os.listdir("C:\Users\om\PycharmProjects\Pythonn")
for x in samp:
    print x
 
Output –
.idea
abc
if3.py
mod1.py
mod1.pyc
mod2.py
mod3.py
mod3.pyc
mod4.py
mod5.py
mod5.pyc
mod6.py
 
Example –
import os
os.makedirs("levels")
fp=open('levels/abc.txt','w')
fp.write("rakesh kumar")
fp.close()
# REMOVE THE FILE
os.remove('levels/abc.txt')
# ALL EMPTY DIRECTORIES ABOVE IT
os.removedirs('levels')
 
Example –
import os
if os.name=='om':
    cmd='ipconfig'
else:
    cmd='ping google.com'
os.system(cmd)
 
Output –
Pinging google.com [172.217.26.174] with 32 bytes of data:
Reply from 172.217.26.174: bytes=32 time=20ms TTL=57
Reply from 172.217.26.174: bytes=32 time=57ms TTL=57
Reply from 172.217.26.174: bytes=32 time=18ms TTL=57
Reply from 172.217.26.174: bytes=32 time=22ms TTL=57
 
Ping statistics for 172.217.26.174:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 18ms, Maximum = 57ms, Average = 29ms

No comments:

Post a Comment