Advanced Python Material-converted
Advanced Python Material-converted
OOP’s
Part - 1
What is Class:
⚽ In Python every thing is an object. To create objects we required some
Model or Plan or Blue print, which is nothing but class.
⚽ We can write a class to represent properties (attributes) and actions
(behaviour) of object.
Syntax:
class className:
''' documenttation string '''
variables:instance variables,static and local
variables methods: instance methods,static
methods,class methods
1) print(classname. doc )
2) help(classname)
Example:
1) class Student:
2) ''''' This is student class with required data'''
3) print(Student. doc )
4) help(Student)
Within the Python class, we can represent operations by using methods. The
following are various types of allowed methods
1) Instance Methods
2) Class Methods
3) Static Methods
1) class Student:
2) '''''Developed by durga for python demo'''
3) def init (self):
4) self.name='dur
5) ga'
self.age=
6) 40
self.marks=
7) 80
8) def talk(self):
9) print("Hello I am
10 :",self.name)
print("My Age
)11 is:",self.age)
print("My Marks
) are:",self.marks)
What is Object:
Pysical existence of a class is nothing but object. We can create any number
of objects for a class.
Example: s = Student()
1) class Student:
2)
3) def init
(self,name,rollno,marks):
4) self.name=na
5) me
self.rollno=roll
6) no
self.marks=mar
7) ks
8) def talk(self):
9) print("Hello My Name is:",self.name)
10) print("My Rollno is:",self.rollno)
11) print("My Marks are:",self.marks)
12)
13) s1=Student("Durga",101,80)
14) s1.talk()
Output:
D:\durgaclasses>py
test.py Hello My Name
is: Durga My Rollno is:
101
My Marks are: 80
Self Variable:
self is the default variable which is always pointing to current object (like
this keyword in Java)
By using self we can access instance variables and instance methods of object.
Note:
1) self should be first parameter inside
constructor def init (self):
2) self should be first parameter inside instance
methods def talk(self):
Constructor Concept:
☕ Constructor is a special method in python.
☕ The name of the constructor should be init (self)
☕ Constructor will be executed automatically at the time of object creation.
☕ The main purpose of constructor is to declare and initialize instance variables.
☕ Per object constructor will be exeucted only once.
☕ Constructor can take atleast one argument(atleast self)
☕ Constructor is optional and if we are not providing any constructor then
python will provide default constructor.
Example:
1) def init (self,name,rollno,marks):
2) self.name=name
3) self.rollno=rollno
4) self.marks=marks
Output
Constructor
exeuction...
Constructor
exeuction...
Constructor
exeuction... Method
execution...
Program:
1) class
Student:
2)
3) ''''' This is student class with required data'''
4) def init (self,x,y,z):
5) self.name
6) =x
self.rollno
7) =y
self.marks
8) =z
9) def display(self):
10) print("Student Name:{}\nRollno:{}
\nMarks:{}".format(self.name,self.rollno,self
11).marks))
12) s1=Student("Durga",101,80)
13) s1.display()
14) s2=Student("Sunny",102,100)
15)
s2.display()
Output
Student
Name:Durga
Rollno:101
Marks:80
KESA PAVAN KUMAR Cell No: 9848461833
6
Student
Name:Sunny
Rollno:102
Marks:100
Types of Variables:
Inside Python class 3 types of variables are allowed.
1)Instance Variables:
If the value of a variable is varied from object to object, then such type of
variables are called instance variables.
For every object a separate copy of instance variables will be created.
1) class Employee:
2)
3) def init (self):
KESA PAVAN KUMAR Cell No: 9848461833
4) self.eno=1
5) 00
self.ename='Dur
7
6) self.esal=10000
7)
8) e=Employee()
9) print(e. dict )
1) class Test:
2)
3) def init (self):
4) self.a=10
5) self.b=20
6)
7) def m1(self):
8) self.c=30
9)
10) t=Test()
11) t.m1()
12) print(t. dict )
1) class
Test:
2)
3) def init (self):
4) self.a=1
5) 0
6) self.b=2
def m1(self):
7) 0
self.c=3
8) 0
9) t=Test()
10) t.m1()
11) t.d=40
12) print(t. dict )
1) class Test:
2)
3) def init (self):
4) self.a=1
5) self.b=20
0
6) def display(self):
7) print(self.a)
8) print(self.b)
9)
10) t=Test()
11)
12) t.display()
print(t.a,t.b)
Outpu
t 10
20
10 20
1) class
Test:def init (self):
2)
3) self.a=1
4) 0
self.b=2
5) 0
self.c=3
6) 0
self.d=4
7) 0 m1(self):
def
8) del
9) self.d
10) t=Test()
11) print(t. dict
)12) t.m1()
13)
14) print(t.
del t.c dict
)
Output
{'a': 10, 'b': 20, 'c': 30, 'd': 40}
{'a': 10, 'b': 20, 'c': 30}
{'a': 10, 'b': 20}
Note: The instance variables which are deleted from one object, will not be
deleted from other objects.
1) class
Test:
2) def init (self):
3) self.a=1
4) 0
self.b=2
5) 0
self.c=3
6) 0
7) self.d=4
0
8) t1=Test()
9) t2=Test()
10) del t1.a
11) print(t1. dict )
12) print(t2. dict )
Output
{'b': 20, 'c': 30, 'd': 40}
{'a': 10, 'b': 20, 'c': 30, 'd': 40}
1) class
Test:def init (self):
2)
3) self.a=1
4) 0
self.b=2
5) 0
6) t1=Test()
7) t1.a=888
8) t1.b=999
9) t2=Test()
10) print('t1:',t1.a,t1.b)
11)
print('t2:',t2.a,t2.b)
Output
t1: 888 999
t2: 10 20
2)Static Variables:
☕ If the value of a variable is not varied from object to object, such type of
variables we have to declare with in the class directly but outside of
methods. Such types of variables are called Static variables.
☕ For total class only one copy of static variable will be created and shared
by all objects of that class.
☕ We can access static variables either by class name or by object reference.
But recommended to use class name.
1) class Test:
2) x=10
3) def init
4) self.y=2
(self): 5)
0
6) t1=Test()
7) t2=Test()
8) print('t1:',t1.x,t1.y)
9) print('t2:',t2.x,t2.y)
10) Test.x=888
11) t1.y=999
12)
13) print('t1:',t1.x,t1.y)
print('t2:',t2.x,t2.y)
Output
t1: 10 20
t2: 10 20
t1: 888 999
t2: 888 20
1) class Test:
2) a=10
3) def init (self):
4) Test.b=20
5) def m1(self):
6) Test.c=30
7) @classmethod
8) def m2(cls):
9) cls.d1=40
10) Test.d2=400
11) @staticmethod
12) def m3():
13) Test.e=50
14) print(Test. dict )
15) t=Test()
16) print(Test. dict )
17) t.m1()
18) print(Test. dict )
19) Test.m2()
20) print(Test. dict )
21) Test.m3()
22) print(Test. dict )
23) Test.f=60
24) print(Test. dict )
1) class Test:
2) a=10
3) def init (self):
4) print(self.a)
5) print(Test.a)
6) def m1(self):
7) print(self.a)
8) print(Test.a)
9) @classmethod
10) def m2(cls):
11) print(cls.a)
12) print(Test.a)
13) @staticmethod
14) def m3():
15) print(Test.a)
16) t=Test()
17) print(Test.a)
18) print(t.a)
19) t.m1()
20) t.m2()
21) t.m3()
1) class Test:
2) a=777
3)
4) @classmeth
def m1(cls):
od
5) cls.a=88
6) 8
@staticmethod
7) def m2():
8) Test.a=99
9) 9
print(Test.a)
10) Test.m1()
11)
12) Test.m2()
print(Test.a)
13)
Outpu
print(Test.a)
t 777
888
999
*****
If we change the Value of Static Variable by using
either self OR Object Reference Variable:
If we change the value of static variable by using either self or object
reference variable, then the value of static variable won't be changed, just a
new instance variable with that name will be added to that particular object.
1) class Test:
2) a=10
3) def m1(self):
4) self.a=888
5) t1=Test()
6) t1.m1()
7) print(Test.a)
8) print(t1.a)
Outpu
t 10
888
1) class Test:
2) x=10
3) def init
4) self.y=2
(self): 5)
0
6) t1=Test()
7) t2=Test()
8) print('t1:',t1.x,t1.y)
9) print('t2:',t2.x,t2.y)
10) t1.x=888
11) t1.y=999
12)
13) print('t1:',t1.x,t1.y)
print('t2:',t2.x,t2.y)
Output
t1: 10
20
t2: 10 20
t1: 888 999
t2: 10 20
1) class Test:
2) a=10
3) def init (self):
4) self.b=20
5) t1=Test()
6) t2=Test()
7) Test.a=888
8) t1.b=999
9) print(t1.a,t1.b)
10) print(t2.a,t2.b)
Output
888 999
888 20
1) class Test:
2) a=10
3) def init (self):
4) self.b=2
5) def
0 m1(self):
6) self.a=88
7) 8
self.b=99
8) 9
9)
t1=Test()
10) t2=Test()
11)
12) t1.m1()
print(t1.a,t1.b)
13) print(t2.a,t2.
b)
Output
888
999
10 20
1) class Test:
2) a=10
3) def init (self):
4) self.b=2
5) @classmethod
0
6) def m1(cls):
7) cls.a=88
8) 8
cls.b=9
9) 99
10) t1=Test()
11) t2=Test()
12) t1.m1()
13) print(t1.a,t1.b)
14) print(t2.a,t2.b)
15)
print(Test.a,Test.b)
Outpu
t 888
20
888 20
888 999
1) class Test:
2) a=10
3) @classmethod
4) def m1(cls):
5) del cls.a
6) Test.m1()
7) print(Test. dict )
Example:
1) class Test:
2) a=10
3) def init (self):
4) Test.b=2
5) 0
del
6) Test.a
def m1(self):
7) Test.c=3
8) 0
del
9) Test.b
10) def @classmeth
m2(cls):
od
11 cls.d=4
)12 0
del
)13) Test.c
14) def @staticmeth
m3():
od
15 Test.e=5
)16 0
del
)17) print(Test.
Test.d dict
)18) t=Test()
19)
20) print(Test.
t.m1() dict
)
22) Test.m2()
21) print(Test. dict
)
24) Test.m3()
23) print(Test. dict
26)
) Test.f=60
****Note:
⚽ By using object reference variable/self we can read static variables, but
we cannot modify or delete.
⚽ If we are trying to modify, then a new instance variable will be added to
that particular object.
⚽ t1.a = 70
⚽ If we are trying to delete then we will get error.
Example:
1) class Test:
2) a=10
3)
4) t1=Test()
5) del t1.a ===>AttributeError: a
We can modify or delete static variables only by using classname or cls variable.
1) import sys
2) class Customer:
3) ''''' Customer class with bank
operations..
4) '''
bankname='DURGABANK'
5)
6) def init (self,name,balance=0.0):
self.name=na
7) me
self.balance=bala
8) defnce
deposit(self,amt):
9) self.balance=self.balance+
10 amt
print('Balance after
)11) def deposit:',self.balance)
withdraw(self,amt):
12 if
)13 amt>self.balance
print('Insufficient Funds..cannot perform this
)
14 : sys.exit
operation')
)15 ()
self.balance=self.balance-
)16 amt
print('Balance after
)17) withdraw:',self.balance)
18) print('Welcome to',Customer.bankname)
19) name=input('Enter Your Name:')
20) c=Customer(name)
21) while True:
22) print('d-Deposit \nw-Withdraw \ne-exit')
23) option=input('Choose your
option:')
Output:
D:\durga_classes>py
test.py Welcome to
DURGABANK Enter
Your Name:Durga
d-Deposit
w-
Withdraw
e-exit
Choose your
option:d Enter
amount:10000
Balance after deposit:
10000.0 d-Deposit
w-
Withdraw
e-exit
Choose your
option:d Enter
amount:20000
Balance after deposit:
30000.0 d-Deposit
w-
Withdraw
e-exit
Choose your
option:w Enter
amount:2000
Balance after withdraw:
28000.0 d-Deposit
w-
Withdraw
e-exit
e-exit
Choose your
option:e Thanks
for Banking
3)Local Variables:
⚽ Sometimes to meet temporary requirements of programmer,we can declare
variables inside a method directly,such type of variables are called local
variable or temporary variables.
⚽ Local variables will be created at the time of method execution and
destroyed once method completes.
⚽ Local variables of a method cannot be accessed from outside of method.
1) class Test:
2) def m1(self):
3) a=1000
4) print(a)
5) def m2(self):
6) b=2000
7) print(b)
8) t=Test()
9) t.m1()
10) t.m2()
Outpu
t 1000
2000
1) class
Test:
2) def m1(self):
3) a=100
4) 0
print(a
5) )
def
m2(self):
6) b=200
7) 0
print(a) #NameError: name 'a' is not
8) defined
print(b
9) )
t=Test()
10) t.m1()
11) t.m2()
Types of Methods:
Inside Python class 3 types of methods are allowed
1) Instance Methods
2) Class Methods
3) Static Methods
1)Instance Methods:
⚽ Inside method implementation if we are using instance variables then such
type of methods are called instance methods.
⚽ Inside instance method declaration, we have to pass self variable. def m1(self):
⚽ By using self variable inside method we can able to access instance variables.
⚽ Within the class we can call instance method by using self variable and from
outside of the class we can call by using object reference.
1) class Student:
2) def init (self,name,marks):
3) self.name=name
4) self.marks=marks
5) def display(self):
6) print('Hi',self.name)
7) print('Your Marks are:',self.marks)
8) def grade(self):
9) if self.marks>=60:
10) print('You got First Grade')
11) elif self.marks>=50:
12) print('Yout got Second Grade')
13) elif self.marks>=35:
14) print('You got Third Grade')
15) else:
16) print('You are Failed')
17) n=int(input('Enter number of students:'))
18) for i in range(n):
19) name=input('Enter Name:')
20) marks=int(input('Enter Marks:'))
21) s= Student(name,marks)
22) s.display()
23) s.grade()
24) print()
Ouput:
D:\durga_classes>py
test.py Enter number of
students:2
Enter
Name:Durga
Enter Marks:90
Hi Durga
Your Marks are:
90 You got First
Grade
Enter
Name:Ravi
Enter
Marks:12 Hi
Ravi
Your Marks are:
12 You are
Failed
Setter Method:
setter methods can be used to set values to the instance variables. setter
methods also known as mutator methods.
Syntax:
def
setVariable(self,variab
le):
self.variable=variable
Example:
def
setName(self,name)
: self.name=name
Getter Method:
Getter methods can be used to get values of the instance variables. Getter
methods also known as accessor methods.
Syntax:
def
getVariable(self)
: return
self.variable
Example:
def
getName(self):
return
KESA PAVAN KUMAR Cell No: 9848461833
22
self.name
1) class Student:
2) def setName(self,name):
3) self.name=name
4)
5) def getName(self):
6) return self.name
7)
8) def setMarks(self,marks):
9) self.marks=marks
10)
11) def getMarks(self):
12) return self.marks
13)
14) n=int(input('Enter number of students:'))
15) for i in range(n):
16) s=Student()
17) name=input('Enter Name:')
18) s.setName(name)
19) marks=int(input('Enter Marks:'))
20) s.setMarks(marks)
21)
22) print('Hi',s.getName())
23) print('Your Marks are:',s.getMarks())
24) print()
Output:
D:\python_classes>py
test.py Enter number of
students:2
Enter
Name:Durga
Enter Marks:100
Hi Durga
Your Marks are: 100
Enter
Name:Ravi
Enter
Marks:80 Hi
Ravi
Your Marks are: 80
2)Class Methods:
⚽ Inside method implementation if we are using only class variables (static
variables), then such type of methods we should declare as class method.
⚽ We can declare class method explicitly by using @classmethod decorator.
⚽ For class method we should provide cls variable at the time of declaration
⚽ We can call classmethod by using classname or object reference variable.
1) class Animal:
2) lEgs=4
3) @classmethod
4) def walk(cls,name):
5) print('{} walks with {} lEgs...'.format(name,cls.lEgs))
6) Animal.walk('Dog')
7) Animal.walk('Cat')
Output
D:\python_classes>py
test.py Dog walks with 4
lEgs...
Cat walks with 4 lEgs...
13)
14) t4=Test()
t5=Test()
15)
Test.noOfObjects()
3)Static Methods:
⚽ In general these methods are general utility methods.
⚽ Inside these methods we won't use any instance or class variables.
⚽ Here we won't provide self or cls arguments at the time of declaration.
⚽ We can declare static method explicitly by using @staticmethod decorator
⚽ We can access static methods by using classname or object reference
1) class DurgaMath:
2)
3) @staticmethod
4) def add(x,y):
5) print('The Sum:',x+y)
6)
7)
8) @staticmeth
def product(x,y):
od
9) print('The
10 Product:',x*y)
)11)
12) @staticmeth
def average(x,y):
od
13 print('The
)
14 average:',(x+y)/2)
)15) DurgaMath.add(10,20)
16) DurgaMath.product(10,20)
17)
DurgaMath.average(10,20)
Output
The Sum: 30
The Product: 200
The average: 15.0
Note:
In general we can use only instance and static methods.Inside static
method we can access class level variables by using class name.
Class methods are most rarely used methods in python.
1) class
Employee:
2) def init (self,eno,ename,esal):
3) self.eno=e
4) no
self.ename=ena
5) me
self.esal=e
6) sal
def display(self):
7) print('Employee
8) Number:',self.eno)
print('Employee
9) Name:',self.ename)
print('Employee
10) classSalary:',self.esal)
Test:
11) def
modify(emp):
12 emp.esal=emp.esal+10
)13 000
emp.displa
)14) e=Employee(100,'Durga',10000)
y()
15) Test.modify(
e)
Output
D:\python_classes>py
test.py Employee
Number: 100 Employee
Name: Durga Employee
Salary: 20000
In the above application, Employee class members are available to Test class.
Inner Classes
Sometimes we can declare a class inside another class, such type of
classes are called inner classes.
class Car:
.....
class Engine:
......
class University:
.....
class Department:
......
class Human:
class Head:
Note: Without existing outer class object there is no chance of existing inner
class object. Hence inner class object is always associated with outer class
object.
Demo Program-1:
1) class
Outer:
2) def init (self):
3) print("outer class object
4) creation")
class Inner:
5) def init (self):
6) print("inner class object
7) creation")
def
8) m1(self):
print("inner class
9) method")
o=Outer()
10) i=o.Inner()
11) i.m1()
Output
outer class object
creation inner class
object creation inner
class method
Note: The following are various possible syntaxes for calling inner class method
1) o =
Outer() i =
o.Inner()
i.m1()
2) i =
Outer().Inner()
i.m1()
3) Outer().Inner().m1()
Demo Program-2:
1) class
Person:
2) def init (self):
3) self.name='dur
4) ga'
self.db=self.Do
5) b()
def
display(self):
6) print('Name:',self.na
me)
7)
8) class Dob:
def init (self):
9) self.dd=1
10 0
self.mm=
)11 5
self.yy=19
)12 47
def
)13 display(self):
print('Dob={}/{}/{}'.format(self.dd,self.mm,sel
) f.yy))
KESA PAVAN KUMAR Cell No: 9848461833
28
14) p=Person()
15) p.display()
16) x=p.db
17) x.display()
Output
Name: durga
Dob=10/5/194
7
Demo Program-3:
Inside a class we can declare any number of inner classes.
1) class Human:
2)
3) def init (self):
4) self.name =
5) 'Sunny'
self.head =
6) self.Head()
self.brain =
7) self.Brain()
def
display(self):
8) print("Hello..",self.na
me)
9)
10) class Head:
11 def
)12 talk(self):
print('Talking..
)13 .')
)
14) class Brain:
def
15
16 think(self):
print('Thinking..
)17) .')
18) h=Human()
19) h.display()
20) h.head.talk()
21)
h.brain.think()
Output
Hello..
Sunny
Talking...
Thinking...
Garbage Collection
⚽ In old languages like C++, programmer is responsible for both creation and
destruction of objects.Usually programmer taking very much care while
creating object, but nEglecting destruction of useless objects. Because of
his nEglectance, total memory can be filled with useless objects which
creates memory problems and total application will be down with Out of
memory error.
⚽ If an object does not have any reference variable then that object eligible
for Garbage Collection.
1) import gc
2) print(gc.isenabled())
3) gc.disable()
4) print(gc.isenabled())
5) gc.enable()
6) print(gc.isenabled())
Outpu
t True
False
True
Destructors:
⚽ Destructor is a special method and the name should be del
⚽ Just before destroying an object Garbage Collector always calls destructor
to perform clean up activities (Resource deallocation activities like close
database connection etc).
⚽ Once destructor execution completed then Garbage Collector automatically
destroys that object.
Note: The job of destructor is not to destroy object and it is just to perform
clean up activities.
1) import time
2) class Test:
3) def init (self):
4) print("Object
5) def del
Initialization...")
6) print("Fulfilling Last Wish and performing clean up
(self): 7)
activities...")
8)
9) t1=Test()
t1=None
10)
11) time.sleep(5)
print("End of application")
Output
Object Initialization...
Fulfilling Last Wish and performing clean up
activities... End of application
Note: If the object does not contain any reference variable then only it is
eligible fo GC. ie if the reference count is zero then only object eligible for GC.
1) import time
2) class Test:
3) def init (self):
4) print("Constructor Execution...")
5) def del (self):
6) print("Destructor Execution...")
7)
8) t1=Test()
9) t2=t1
10) t3=t2
11) del t1
12) time.sleep(5)
13) print("object not yet destroyed after deleting t1")
14) del t2
15) time.sleep(5)
16) print("object not yet destroyed even after deleting t2")
17) print("I am trying to delete last reference variable...")
18) del
t3
Example:
1) import time
2) class Test:
3) def init (self):
4) print("Constructor
5) def del
Execution...")
6) print("Destructor
(self): 7)
Execution...")
8) list=[Test(),Test(),Test()]
9) del list
10) time.sleep(5)
11) print("End of application")
Output
Constructor
Execution...
Constructor
Execution...
Constructor
Execution...
Destructor
Execution...
Destructor
Execution...
Destructor
Execution... End of
application
1) import sys
2) class Test:
3) pass
4) t1=Test()
5) t2=t1
6) t3=t1
7) t4=t1
8) print(sys.getrefcount(t1))
Output 5
KESA PAVAN KUMAR Cell No: 9848461833
32
Note: For every object, Python internally maintains one default reference variable self.
OOP’s
Part - 2
Agenda
Inheritance
Has-A Relationship
IS-A Relationship
IS-A vs HAS-A Relationship
Composition vs Aggregation
Types of Inheritance
Single Inheritance
Multi Level Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
Cyclic Inheritance
Demo Program-1:
1) class Engine:
2) a=10
3) def init (self):
4) self.b=2
5) def
0 m1(self):
Output:
Car using Engine Class
Functionality 10
20
Engine Specific Functionality
Demo Program-2:
1) class
Car: def init (self,name,model,color):
2)
3) self.name=na
4) me
self.model=mo
5) del
self.color=col
6) orgetinfo(self):
def
7) print("Car Name:{} , Model:{} and
Color:{}".format(self.name,self.model,self.c
8) olor))
9)
10)class def Employee:
init (self,ename,eno,car):
11 self.ename=ena
)12 me
self.eno=e
)13 no
self.car=c
)14) def arempinfo(self):
15 print("Employee
)
16 Name:",self.ename)
print("Employee
)17 Number:",self.eno)
print("Employee Car
)18 Info:")
self.car.getinf
)19) o()
c=Car("Innova","2.5V","Grey"
20) e=Employee('Durga',10000,c)
)21) e.empinfo
()
KESA PAVAN KUMAR Cell No: 9848461833
36
Output:
Employee Name:
Durga Employee
Number: 10000
Employee Car Info:
Car Name: Innova, Model: 2.5V and Color:Grey
In the above program Employee class Has-A Car reference and hence
Employee class can access all members of Car class.
Demo Program-3:
1) class
X: a=10
2)
3) def init (self):
4) self.b=2
5) 0 m1(self):
def
6) print("m1 method of X
7) class")
8) class Y:
9) c=30
10) def init (self):
11 self.d=4
)12) def 0 m2(self):
13 print("m2 method of Y
) class")
14
15) def m3(self):
)16 x1=X(
)17 )print(x1.
)18 a)
print(x1.
)19 b)
x1.m1(
)20 )print(Y.
)21 c)
print(self.
)22 d)
self.m2
)23 ()
print("m3 method of Y
)24) y1=Y()
class")
25) y1.m3(
)
Output:
10
20
m1 method of X
class 30
40
m2 method of Y
class m3 method
of Y class
Output:
10
10
Parent instance
method Parent class
method Parent static
method
1) class P:
2) 10 methods
3) class C(P):
4) 5 methods
In the above example Parent class contains 10 methods and these methods
automatically available to the child class and we are not required to rewrite
those methods(Code Reusability)
Hence child class contains 15 methods.
Note: What ever members present in Parent class are by default available to
the child class through inheritance.
1) class P:
2) def m1(self):
3) print("Parent class method")
4) class C(P):
5) def m2(self):
6) print("Child class method")
7)
8) c=C();
9) c.m1()
10) c.m2()
Output:
Parent class
method Child
class method
What ever methods present in Parent class are automatically available to the
child class and hence on the child class reference we can call both parent
class methods and child class methods.
1) class P:
2) a=10
3) def init (self):
4) self.b=20
5) class C(P):
6) c=30
7) def init (self):
8) super(). init ()===>Line-1
9) self.d=30
10)
11) c1=C()
12) print(c1.a,c1.b,c1.c,c1.d)
Output:
Eat Biryani and Drink Beer
Coding Python is very easy just like drinking Chilled
Beer Employee Name: Durga
Employee Age: 48
Employee Number: 100
Employee Salary: 10000
Person
IS - A
HAS - A
Employee Ca
r
1) class Car:
2) def init (self,name,model,color):
3) self.name=name
4) self.model=model
5) self.color=color
6) def getinfo(self):
7) print("\tCar Name:{} \n\t Model:{} \n\t
Color:{}".format(self.name,self.model,
self.color))
8)
9) class Person:
10) def init (self,name,age):
11) self.name=name
12) self.age=age
13) def eatndrink(self):
14) print('Eat Biryani and Drink Beer')
15)
16) class Employee(Person):
17) def init (self,name,age,eno,esal,car):
18) super(). init (name,age)
19) self.eno=eno
20) self.esal=esal
21) self.car=car
22) def work(self):
23) print("Coding Python is very easy just like drinking Chilled Beer")
24) def empinfo(self):
25) print("Employee Name:",self.name)
26) print("Employee Age:",self.age)
27) print("Employee Number:",self.eno)
28) print("Employee Salary:",self.esal)
29) print("Employee Car Info:")
30) self.car.getinfo()
31)
32) c=Car("Innova","2.5V","Grey")
33) e=Employee('Durga',48,100,10000,c)
34) e.eatndrink()
35) e.work()
36) e.empinfo()
Output:
Eat Biryani and Drink Beer
Coding Python is very easy just like drinking Chilled
Beer Employee Name: Durga
Employee Age: 48
Employee Number: 100
Employee Salary:
10000 Employee Car
Info:
Car
Name:Innova
Model:2.5V
Color:Grey
In the above example Employee class extends Person class functionality but
just uses Car class functionality.
Composition vs Aggregation:
Composition:
Without existing container object if there is no chance of existing contained
object then the container and contained objects are strongly associated and
that strong association is nothing but Composition.
Department
Object
(Contained
Object)
University
Object
(Container
KESA PAVAN KUMAR Cell No: 9848461833
42
Object)
Aggregation:
Without existing container object if there is a chance of existing contained
object then the container and contained objects are weakly associated and
that weak association is nothing but Aggregation.
(Contained
x
Professor
: :
Object)
Object
: :
: :
x
Department
Object
(Container
Object)
Coding Example:
1) class Student:
2) collegeName='DURGASOFT'
3) def init (self,name):
4) self.name=na
5) print(Student.collegeName)
me
6) s=Student('Durga')
7) print(s.name)
Output:
DURGASOFT
Durga
Conclusion:
The relation between object and its instance variables is always Composition
where as the relation between object and static variables is Aggregation.
Note: Whenever we are creating child class object then child class
constructor will be executed. If the child class does not contain constructor
then parent class constructor will be executed, but parent object won't be
created.
1) class P:
2) def init (self):
3) print(id(self))
4) class C(P):
5) pass
6) c=C()
7) print(id(c))
Output:
6207088
6207088
1) class
Person:
2) def init (self,name,age):
3) self.name=na
4) me
self.age=a
5) classge
Student(Person):
6) def init (self,name,age,rollno,marks):
7) super(). init (name,age)
8) self.rollno=roll
9) no
self.marks=mar
ks
10) def str (self):
11) return
'Name={}\nAge={}\nRollno={}\nMarks={}'.format(self.name,self.age,sel
12) f.rollno,self.marks)
s1=Student('durga',48,101,90)
13) print(s1)
Output:
Name=dur
ga Age=48
Rollno=101
Marks=90
Note: In the above example when ever we are creating child class object both
parent and child class constructors got executed to perform initialization of
child object.
Types of Inheritance:
1)Single Inheritance:
The concept of inheriting the properties from one class to another class is
known as single inheritance.
1) class
P:
2) def m1(self):
3) print("Parent
4) classMethod")
C(P):
5) def
m2(self):
6) print("Child
Method")
7) c=C()
8) c.m1()
9) c.m2()
Output:
Parent
Method Child
Method
Single Inheritance
1) class P:
2) def m1(self):
3) print("Parent Method")
4) class C(P):
5) def m2(self):
6) print("Child Method")
7) class CC(C):
8) def m3(self):
9) print("Sub Child Method")
10) c=CC()
11) c.m1()
12) c.m2()
13) c.m3()
Output:
Parent
Method Child
Method
Sub Child Method
C
C
3)Hierarchical Inheritance:
The concept of inheriting properties from one class into multiple classes
which are present at same level is known as Hierarchical Inheritance
C C
1 2
Hierarchical
Inheritance
1) class P:
2) def m1(self):
3) print("Parent Method")
4) class C1(P):
5) def
m2(self):
6) print("Child1
Method")
7) class
8) def C2(P):
m3(self):
9) print("Child2
Method")
10) c1=C1()
11)
c1.m1()
12) c1.m2()
13)
14) c2.m1()
c2=C2()
15)
Output:
c2.m3()
Parent
Method
Child1
Method
Parent
Method
Child2
Method
4)Multiple Inheritance:
The concept of inheriting the properties from multiple classes into a single
class at a time, is known as multiple inheritance.
P P
1 2
Multiple
Inheritanc
e
1) class
P1: def m1(self):
2)
3) print("Parent1
4) classMethod")
P2:
5) def
m2(self):
6) print("Parent2
Method")
7)
8) class
def m3(self):
KESA PAVAN KUMAR
C(P1,P2): Cell No: 9848461833
9) print("Child2
Method")
48
10) c=C()
11) c.m1()
12) c.m2()
13) c.m3()
Output:
Parent1
Method
Parent2
Method
Child2
Method
If the same method is inherited from both parent classes, then Python
will always consider the order of Parent classes in the declaration of
the child class.
1) class P1:
2) def m1(self):
3) print("Parent1 Method")
4) class P2:
5) def m1(self):
6) print("Parent2 Method")
7) class C(P1,P2):
8) def m2(self):
9) print("Child Method")
10) c=C()
11) c.m1()
12) c.m2()
Output:
Parent1
Method Child
Method
5)Hybrid Inheritance:
Combination of Single, Multi level, multiple and Hierarchical inheritance is
known as Hybrid Inheritance.
A B C
G H
6)Cyclic Inheritance:
The concept of inheriting properties from one class to another class in
cyclic way, is called Cyclic inheritance.Python won't support for Cyclic
Inheritance of course it is really not required.
Eg - 1: class A(A):pass
NameError: name 'A' is not defined
Eg - 2:
1) class A(B):
2) pass
3) class B(A):
4) pass
B C
D
mro(A) = A, object
mro(B) = B, A, object
mro(C) = C, A, object
mro(D) = D, B, C, A,
object
test.py
1) class A:pass
2) class B(A):pass
3) class C(A):pass
4) class D(B,C):pass
5) print(A.mro())
6) print(B.mro())
7) print(C.mro())
8) print(D.mro())
Output:
[<class ' main .A'>, <class 'object'>]
[<class ' main .B'>, <class ' main .A'>, <class
'object'>] [<class ' main .C'>, <class ' main .A'>,
<class 'object'>]
[<class ' main .D'>, <class ' main .B'>, <class ' main .C'>, <class ' main .A'>,
<class 'object'>]
Objec
t
A B C
X Y
mro(A)=A,object P
mro(B)=B,object
mro(C)=C,object
mro(X)=X,A,B,object
mro(Y)=Y,B,C,object
mro(P)=P,X,A,Y,B,C,ob
ject
test.py
1) class A:pass
2) class B:pass
3) class C:pass
4) class X(A,B):pass
5) class Y(B,C):pass
6) class P(X,Y,C):pass
7) print(A.mro())#AO
8) print(X.mro())#XABO
9) print(Y.mro())#YBCO
10) print(P.mro())#PXAYBCO
Output:
[<class ' main .A'>, <class 'object'>]
[<class ' main .X'>, <class ' main .A'>, <class ' main .B'>, <class
'object'>] [<class ' main .Y'>, <class ' main .B'>, <class ' main
.C'>, <class 'object'>] [<class ' main .P'>, <class ' main .X'>, <class ' main
.A'>, <class ' main .Y'>,
<class ' main .B'>,
<class ' main .C'>, <class 'object'>]
test.py
1) class A:
2) def m1(self):
3) print('A class Method')
4) class B:
5) def m1(self):
6) print('B class Method')
7) class C:
8) def m1(self):
9) print('C class Method')
10) class X(A,B):
11) def m1(self):
12 print('X class Method')
)
13) class Y(B,C):
14) def m1(self):
15 print('Y class Method')
)
16) class P(X,Y,C):
17) def m1(self):
18 print('P class Method')
)
19) p=P()
20) p.m1()
Objec
t
D E F
B C
mro(o) = object
mro(D) = D,object
mro(E) = E,object
mro(F) = F,object
mro(B) =
B,D,E,object
mro(C) =
C,D,F,object
mro(A) = A+Merge(mro(B),mro(C),BC)
= A+Merge(BDEO,CDFO,BC)
= A+B+Merge(DEO,CDFO,C)
= A+B+C+Merge(DEO,DFO)
= A+B+C+D+Merge(EO,FO)
= A+B+C+D+E+Merge(O,FO)
= A+B+C+D+E+F+Merge(O,O)
= A+B+C+D+E+F+O
test.py
1) class D:pass
2) class E:pass
3) class F:pass
4) class B(D,E):pass
5) class C(D,F):pass
6) class A(B,C):pass
7) print(D.mro())
8) print(B.mro())
9) print(C.mro())
10) print(A.mro())
Output:
[<class ' main .D'>, <class 'object'>]
[<class ' main .B'>, <class ' main .D'>, <class ' main .E'>, <class
'object'>] [<class ' main .C'>, <class ' main .D'>, <class ' main
.F'>, <class 'object'>] [<class ' main .A'>, <class ' main .B'>, <class ' main
.C'>, <class ' main .D'>,
<class ' main .E'>,
<class ' main .F'>, <class 'object'>]
super() Method:
super() is a built-in method which is useful to call the super class
constructors,variables and methods from the child class.
In the above program we are using super() method to call parent class
constructor and display() method
Output:
10
Parent instance
method Parent class
method Parent static
method
In the above example we are using super() to call various members of Parent class.
1) super(D, self).m1()
It will call m1() method of super class of D.
2) A.m1(self)
It will call A class m1() method
1) class A:
2) def m1(self):
3) print('A class Method')
4) class B(A):
5) def m1(self):
6) print('B class Method')
7) class C(B):
8) def m1(self):
9) print('C class Method')
10) class D(C):
11) def m1(self):
12) print('D class Method')
13) class E(D):
14) def m1(self):
15) A.m1(self)
16)
17) e=E()
18) e.m1()
1) class P:
2) a=10
3) def init
4) self.b=2
(self): 5)
0
6) class C(P):
7) def m1(self):
8) print(super().a)#valid
9) print(self.b)#valid
10) print(super().b)#invalid
11) c=C()
12) c.m1()
Output:
10
20
AttributeError: 'super' object has no attribute 'b'
Case-2: From child class constructor and instance method, we can access
parent class instance method, static method and class method by using
super()
1) class
P:
2) def init (self):
3) print('Parent
4) Constructor')
def m1(self):
5) print('Parent instance
6) method')
@classmethod
7) def m2(cls):
8) print('Parent class
9) method')
10) def @staticmeth
m3():
od
11 print('Parent static
)
12 method')
)13) class
C(P):
14) def init (self):
15 super(). init ()
)16 super().m1
)17 ()
super().m2
)
18 ()
super().m3
)19 ()
)20) def m1(self):
super(). init ()
21
22 super().m1
)23 ()
super().m2
)
24 ()
super().m3
)25) ()
26) c=C()
27)
c.m1()
Output:
Parent Constructor
Parent instance
method Parent class
method Parent static
method Parent
Constructor Parent
instance method
Parent class method
Parent static method
Case-3: From child class, class method we cannot access parent class
instance methods and constructors by using super() directly(but indirectly
possible). But we can access parent class static and class methods.
1) class
P:
2) def init (self):
3) print('Parent
4) defConstructor')
m1(self):
5) print('Parent instance
6) method')
@classmethod
7) def m2(cls):
8) print('Parent class
9) method')
10) def @staticmeth
m3():
od
11 print('Parent static
)12 method')
)13) class C(P):
14) @classmethod
15) def
m1(cls):
16 #super(). init ()--->invalid
)17 #super().m1()---
)18 >invalid
super().m2
)19 ()
super().m3
)20 ()
)21)
C.m1()
Output:
Parent class
method Parent
static method
Output:
Parent constructor
Parent instance
method
Case-4: In child class static method we are not allowed to use super()
generally (But in special way we can use)
1) class P:
2) def init (self):
3) print('Parent Constructor')
4) def m1(self):
5) print('Parent instance method')
6) @classmethod
7) def m2(cls):
8) print('Parent class method')
9) @staticmethod
10) def m3():
11) print('Parent static method')
12)
13) class C(P):
14) @staticmethod
15) def m1():
16) super().m1()-->invalid
17) super().m2()--->invalid
18) super().m3()--->invalid
19)
20) C.m1()
OOP’s
Part - 3
POLYMORPHISM
poly means many. Morphs means
forms. Polymorphism means 'Many
Forms'.
repetition operator
Eg4: The Same method with different implementations in Parent class and
child classes.(overriding)
2) Overloading
1) Operator Overloading
2) Method Overloading
3) Constructor Overloading
3) Overriding
1) Method Overriding
2) Constructor Overriding
def
f1(obj):
obj.talk()
1) class Duck:
2) def talk(self):
3) print('Quack.. Quack..')
4)
5) class Dog:
6) def talk(self):
7) print('Bow Bow..')
8)
9) class Cat:
10) def talk(self):
11) print('Moew Moew ..')
12)
13) class Goat:
14) def talk(self):
15) print('Myaah Myaah ..')
16)
17) def f1(obj):
18) obj.talk()
19)
20) l=[Duck(),Cat(),Dog(),Goat()]
21) for obj in l:
22) f1(obj)
Output:
Quack..
Quack..
Moew Moew
.. Bow Bow..
Myaah Myaah ..
The problem in this approach is if obj does not contain talk() method then we
will get AttributeError.
1) class
Duck:
2) def talk(self):
3) print('Quack..
4) Quack..')
5) class
Dog:
6) def bark(self):
7) print('Bow
Bow..')
8) def f1(obj):
9)
10 obj.talk(
)
12) f1(d)
11) d=Duck()
13)
KESA PAVAN KUMAR Cell No: 9848461833
66
14) d=Dog()
15) f1(d)
Output:
D:\durga_classes>py
test.py Quack.. Quack..
Traceback (most recent call last):
File "test.py", line 22, in
<module> f1(d)
File "test.py", line 13,
in f1 obj.talk()
AttributeError: 'Dog' object has no attribute 'talk'
21)
22) h=Human()
23)
24 f1(h)
)
25)
26) f1(d)
d=Dog()
2) Overloading
We can use same operator or methods for different purposes.
1)Operator Overloading:
We can use the same operator for multiple purposes, which is nothing
but operator overloading.
Python supports operator overloading.
6) b2=Book(200)
7) print(b1+b2)
D:\durga_classes>py test.py
Traceback (most recent call
last):
File "test.py", line 7, in
<module> print(b1+b2)
TypeError: unsupported operand type(s) for +: 'Book' and 'Book'
⚽ We can overload + operator to work with Book objects also. i.e Python
supports Operator Overloading.
⚽ For every operator Magic Methods are available. To overload any operator
we have to override that Method in our class.
⚽ Internally + operator is implemented by using add () method.This method is
called magic method for + operator. We have to override this method in
our class.
16) print("s1>=s2=",s1>=s2)
Output
10>20 =
False
s1>s2=
False
s1<s2=
True
s1<=s2=
True
s1>=s2=
False
1) class
Employee:
2) def init (self,name,salary):
3) self.name=na
4) me
self.salary=sal
5) ary
def mul (self,other):
6) return
7) self.salary*other.days
8) class TimeSheet:
9) def init (self,name,days):
10) self.name=name
11) self.days=days
12)
13) e=Employee('Durga',500)
14) t=TimeSheet('Durga',25)
15) print('This Month Salary:',e*t)
2)Method Overloading:
If 2 methods having same name but different type of arguments then
those methods are said to be overloaded methods.
Eg: m1(int a)
m1(double
d)
Demo Program:
1) class Test:
2) def m1(self):
3) print('no-arg method')
4) def m1(self,a):
5) print('one-arg method')
6) def m1(self,a,b):
7) print('two-arg method')
8)
9) t=Test()
10) #t.m1()
11) #t.m1(10)
12) t.m1(10,20)
12) t.sum()
3)Constructor Overloading:
⚽ Constructor overloading is not possible in Python.
⚽ If we define multiple constructors then the last constructor will be considered.
1) class Test:
2) def init (self):
3) print('No-Arg Constructor')
4)
Output
Constructor with 0|1|2|3 number of
arguments Constructor with 0|1|2|3
number of arguments Constructor with
0|1|2|3 number of arguments Constructor
with 0|1|2|3 number of arguments
Output:
Constructor with variable number of
arguments Constructor with variable
number of arguments Constructor with
variable number of arguments
Constructor with variable number of
arguments Constructor with variable
number of arguments
3) Overriding
Method Overriding
⚽ What ever members available in the parent class are bydefault available to
the child class through inheritance. If the child class not satisfied with
parent class implementation then child class is allowed to redefine that
method in the child class based on its requirement. This concept is called
overriding.
⚽ Overriding concept applicable for both methods and constructors.
Output
Gold+Land+Cash+Po
wer Katrina Kaif
From Overriding method of child class,we can call parent class method also
by using super() method.
1) class P:
2) def property(self):
3) print('Gold+Land+Cash+Power')
4) def marry(self):
5) print('Appalamma')
6) class C(P):
7) def marry(self):
8) super().marry()
9) print('Katrina Kaif')
10)
11) c=C()
12) c.property()
13) c.marry()
Output
Gold+Land+Cash+Po
wer Appalamma
Katrina Kaif
From child class constuctor we can call parent class constructor by using super()
method.
9) self.eno=e
10 no
self.esal=e
)11 sal
)
12) def display(self):
print('Employee
13
14 Name:',self.name)
print('Employee
)15 Age:',self.age)
print('Employee
)
16 Number:',self.eno)
print('Employee
)17) Salary:',self.esal)
18) e1=Employee('Durga',48,872425,26000)
19)
e1.display()
20) e2=Employee('Sunny',39,872426,36000)
21)
e2.display()
Output
Employee Name:
Durga Employee
Age: 48
Employee Number: 872425
Employee Salary: 26000
Employee Name:
Sunny Employee
Age: 39
Employee Number: 872426
Employee Salary: 36000
OOP’s
Part - 4
Agenda
1) Abstract Method
2) Abstract class
3) Interface
4) Public,Private and Protected Members
5) str () Method
6) Difference between str() and repr() functions
7) Small Banking Application
Abstract Method:
Sometimes we don't know about implementation, still we can declare a
method. Such types of methods are called abstract methods.i.e abstract
method has only declaration but not implementation.
In python we can declare abstract method by using @abstractmethod
decorator as follows.
@abstractmethod
def m1(self): pass
1) class Test:
2) @abstractmethod
3) def m1(self):
4) pas
s
NameError: name 'abstractmethod' is not defined
Eg:
1) from abc import *
2) class Test:
3) @abstractmethod
4) def m1(self):
5) pass
Eg:
1) from abc import *
2) class Fruit:
3) @abstractmethod
4) def taste(self):
5) pass
Child classes are responsible to provide implemention for parent class abstract
methods.
Abstract class:
Some times implementation of a class is not complete,such type of partially
implementation classes are called abstract classes. Every abstract class in
Python should be derived from ABC class which is present in abc module.
Case-1:
In the above code we can create object for Test class b'z it is concrete class
and it does not conatin any abstract method.
Case-2:
In the above code we can create object, even it is derived from ABC
class,b'z it does not contain any abstract method.
Case-3:
1) from abc import *
2) class Test(ABC):
3) @abstractmethod
4) def m1(self):
5) pass
6)
7) t=Test()
Case-4:
Case-5:
Output: Hello
Case-1:
1) from abc import *
2) class Vehicle(ABC):
3) @abstractmethod
4) def noofwheels(self):
5) pass
6)
7) class Bus(Vehicle): pass
Case-2:
TypeError: Can't instantiate abstract class Bus with abstract methods noofwheels
Note: If we are extending abstract class and does not override its abstract
method then child class is also abstract and instantiation is not possible.
Note: Abstract class can contain both abstract and non-abstract methods also.
Interfaces In Python:
In general if an abstract class contains only abstract methods such type of
abstract class is considered as interface.
D:\durga_classes>py test.py
Enter Database
Name:Oracle Connecting to
Oracle Database...
Disconnecting to Oracle Database...
D:\durga_classes>py test.py
Enter Database
Name:Sybase Connecting to
Sybase Database...
Disconnecting to Sybase Database...
Note: The inbuilt function globals()[str] converts the string 'str' into a class
name and returns the classname.
config.txt
EPSON
test.py
1) from abc import *
2) class Printer(ABC):
3) @abstractmethod
4) def
printit(self,text):pass 5)
6) @abstractmethod
7) def
disconnect(self):pass 8)
9) class EPSON(Printer):
10) def printit(self,text):
11) print('Printing from EPSON Printer...')
12) print(text)
13) def disconnect(self):
14) print('Printing completed on EPSON
Printer...') 15)
16) class HP(Printer):
17) def printit(self,text):
18) print('Printing from HP Printer...')
19) print(text)
20) def disconnect(self):
21) print('Printing completed on HP
Printer...') 22)
23) with open('config.txt','r') as f:
24) pname=f.readlin
e() 25)
26) classname=globals()[pname]
27) x=classname()
28) x.printit('This data has to print...')
29) x.disconnect()
Output:
Printing from EPSON
Printer... This data has to
print...
Printing completed on EPSON Printer...
Protected attributes can be accessed within the class anywhere but from
outside of the class only in child classes. We can specify an attribute as
protected by prefexing with _ symbol.
But is is just convention and in reality does not exists protected attributes.
private attributes can be accessed only within the class.i.e from outside of the
class we cannot access. We can declare a variable as private explicitly by
prefexing with 2 underscore symbols.
syntax:
variablename=value Eg:
name='durga'
1) class Test:
2) x=10
3) _y=20
4) z=30
5) def m1(self):
6) print(Test.x)
7) print(Test._y)
8) print(Test. z)
9)
10) t=Test()
11) t.m1()
12) print(Test.x)
13) print(Test._y)
14) print(Test. z)
Output:
10
20
30
10
20
Traceback (most recent call last):
File "test.py", line 14, in
<module> print(Test. z)
AttributeError: type object 'Test' has no attribute ' z'
1) class
Test:def init (self):
2)
3) self. x=10
4)
KESA PAVAN KUMAR Cell No: 9848461833
87
5) t=Test()
6) print(t._Test x)#10
str () method:
Whenever we are printing any object reference internally str () method
will be called which is returns string in the following format
< main .classname object at 0x022144B0>
1) class Student:
2) def init (self,name,rollno):
3) self.name=name
4) self.rollno=roll
5) no
6) def str (self):
7) return 'This is Student with Name:{} and
Rollno:{}'.format(self.name,self.rollno)
8)
9) s1=Student('Durga',101)
10) s2=Student('Ravi',102)
11) print(s1)
12) print(s2)
Output without Overriding str():
< main .Student object at 0x022144B0>
< main .Student object at 0x022144D0>
1) import datetime
2) today=datetime.datetime.now()
3) s=str(today)#converting datetime object to str
4) print(s)
5) d=eval(s)#converting str object to datetime
D:\durgaclasses>py
test.py 2018-05-18
22:48:19.890888
Traceback (most recent call last):
File "test.py", line 5, in <module>
d=eval(s)#converting str object to
datetime File "<string>", line 1
2018-05-18 22:48:19.890888
^
SyntaxError: invalid token
1) import datetime
2) today=datetime.datetime.now()
3) s=repr(today)#converting datetime object to str
4) print(s)
5) d=eval(s)#converting str object to datetime
6) print(d)
Output:
datetime.datetime(2018, 5, 18, 22, 51, 10, 875838)
2018-05-18 22:51:10.875838
Output:
D:\durgaclasses>py test.py
Durga's Savings Account with Balance
:10000 Account Balance: 15000
Sorry, Insufficient Funds
Durga's Savings Account with
Balance :0 Ravi's Current Account
with Balance :26000 Ravi's Current
Account with Balance :-1000
EXCEPTION
HANDLING
1) Syntax Errors
2) Runtime Errors
1)Syntax Errors:
The errors which occur because of invalid syntax are called syntax errors.
Eg 1:
x = 10
if x == 10
print("Hello
")
Eg 2:
print "Hello"
SyntaxError: Missing parentheses in call to 'print'
2)Runtime Errors:
Also known as exceptions.
While executing the program if something goes wrong because of end
user input or programming logic or memory problems etc then we will
get Runtime Errors.
Eg:
1) print(10/0) ZeroDivisionError: division by zero
2) print(10/"ten") TypeError: unsupported operand type(s) for /: 'int' and 'str'
3) x = int(input("Enter
Number:")) print(x)
D:\Python_classes>py
test.py Enter Number:ten
ValueError: invalid literal for int() with base 10: 'ten'
Note: Exception Handling concept applicable for Runtime Errors but not for syntax
errors
What is Exception?
An unwanted and unexpected event that disturbs normal flow of program is
called exception.
Eg:
ZeroDivisionError
TypeError
ValueError
FileNotFoundError
EOFError
SleepingError
TyrePuncturedError
Eg: For example our programming requirement is reading data from remote
file locating at London. At runtime if London file is not available then the
program should not be terminated abnormally. We have to provide local file
to continue rest of the program normally. This way of defining alternative is
nothing but exception handling.
try:
Read Data from Remote File locating at
London. except FileNotFoundError:
use local file and continue rest of the program normally
Q. What is an Exception?
Q. What is the purpose of Exception Handling?
Q. What is the meaning of Exception Handling?
1) print("Hello")
2) print(10/0)
3) print("Hi")
D:\Python_classes>py
test.py Hello
Traceback (most recent call last):
File "test.py", line 2, in
<module> print(10/0)
ZeroDivisionError: division by zero
Python's Exception
Hierarchy
BaseExceptio
n
TimeO
ut
Every Exception in Python is a Error
class.
All exception classes are child classes of BaseException.i.e every
exception class extends BaseException either directly or indirectly.
KESA PAVAN KUMAR Cell No: 9848461833
94
Without try-except:
1) print("stmt-1")
2) print(10/0)
3) print("stmt-3")
Output
stmt-1
ZeroDivisionError: division by zero
Abnormal termination/Non-Graceful Termination
With try-except:
1) print("stmt-1")
2) try:
3) print(10/0)
4) except ZeroDivisionError:
5) print(10/2)
6) print("stmt-3")
Outpu
t stmt-1
5.0
stmt-3
Normal termination/Graceful Termination
stmt-
4 stmt-5
Conclusions:
1) Within the try block if anywhere exception raised then rest of the try
block won’t be executed eventhough we handled that exception. Hence
we have to take only risky code inside try block and length of the try
block should be as less as possible.
2) In addition to try block, there may be a chance of raising exceptions inside
except and finally blocks also.
3) If any statement which is not part of try block raises an exception then
it is always abnormal termination.
1) print(10/0)
2) except ZeroDivisionError as msg:
3) print("exception raised and its description is:",msg)
Eg:
try:
-------
-------
-------
except ZeroDivisionError:
perform alternative arithmetic operations
except FileNotFoundError:
use local file instead of remote file
1) try:
2) x=int(input("Enter First Number: "))
3) y=int(input("Enter Second Number: "))
4) print(x/y)
5) except ZeroDivisionError :
6) print("Can't Divide with Zero")
7) except ValueError:
8) print("please provide int value only")
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second
Number: 2 5.0
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second
Number: 0 Can't
Divide with Zero
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second Number:
ten please provide int
value only
If try with multiple except blocks available then the order of these except
blocks is important .Python interpreter will always consider from top to
bottom until matched except block identified.
1) try:
2) x=int(input("Enter First Number: "))
3) y=int(input("Enter Second Number: "))
4) print(x/y)
5) except ArithmeticError :
6) print("ArithmeticError")
7) except ZeroDivisionError:
8) print("ZeroDivisionError")
D:\Python_classes>py
test.py Enter First
KESA PAVAN KUMAR Cell No: 9848461833
99
Number: 10
Enter Second
Number: 0
ArithmeticError
except (Exception1,Exception2,exception3,..):
OR
except (Exception1,Exception2,exception3,..)
as msg :
1) try:
2) x=int(input("Enter First Number: "))
3) y=int(input("Enter Second Number: "))
4) print(x/y)
5) except (ZeroDivisionError,ValueError) as msg:
6) print("Plz Provide valid numbers only and problem is: ",msg)
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second Number: 0
Plz Provide valid numbers only and problem is: division by zero
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second Number: ten
Plz Provide valid numbers only and problem is: invalid literal for int() with b are 10: 'ten'
Syntax:
excep
t:
statements
1) try:
2) x=int(input("Enter First Number: "))
3) y=int(input("Enter Second Number: "))
4) print(x/y)
5) except ZeroDivisionError:
KESA PAVAN KUMAR Cell No: 9848461833
101
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second Number: 0
ZeroDivisionError:Can't divide
with zero
D:\Python_classes>py
test.py Enter First
Number: 10
Enter Second Number: ten
Default Except:Plz provide valid input only
***Note: If try with multiple except blocks available then default except block
should be last, otherwise we will get SyntaxError.
1) try:
2) print(10/0)
3) except:
4) print("Default Except")
5) except ZeroDivisionError:
6) print("ZeroDivisionError")
finally Block:
☕ It is not recommended to maintain clean up code(Resource
Deallocating Code or Resource Releasing code) inside try block
because there is no guarentee for the execution of every statement
inside try block always.
☕ It is not recommended to maintain clean up code inside except block,
because if there is no exception then except block won't be executed.
☕ Hence we required some place to maintain clean up code which should be
executed always irrespective of whether exception raised or not raised
and whether exception handled or not handled. Such type of best place is
nothing but finally block.
☕ Hence the main purpose of finally block is to maintain clean up code.
try:
Risky Code
except:
Handling Code
KESA PAVAN KUMAR Cell No: 9848461833
103
finally:
Cleanup code
Outpu
t try
finally
Outpu
t try
except
finally
Outpu
t try
finally
ZeroDivisionError: division by zero(Abnormal Termination)
*** Note: There is only one situation where finally block won't be executed ie
whenever we are using os._exit(0) function.
1) imports
2) try:
3) print("try")
4) os._exit(0)
5) except NameError:
6) print("except")
7) finally:
8) print("finally")
Output: try
Note:
os._exit(0)
Where 0 represents status code and it indicates normal
termination There are multiple status codes are possible.
Case-1: If there is no
exception 1,2,3,5,6 Normal
Termination
--------------
--------------
excep --------------
t: ------------
except:
-----------
-----------
-----------
General Risky code we have to take inside outer try block and too much risky
code we have to take inside inner try block. Inside Inner try block if an
exception raised then inner except block is responsible to handle. If it is
unable to handle then outer except block is responsible to handle.
1) try:
2) print("outer try block")
3)
4) tryprint("Inner try
:5) block")
print(10/0
6) )
except ZeroDivisionError:
7) print("Inner except
8) block")
finally:
9) print("Inner finally
block")
10) except:
11) print("outer except block")
12) finally:
13) print("outer finally block")
Output
outer try
block Inner
try block
Inner except
block Inner
finally block
outer finally
block
Case-3: If an exceptiion raised at stmt-2 and the corresponding except block not
matched 1,11,Abnormal Termination
Case-6:If an exception raised at stmt-5 and both inner and outer except
blocks are not matched 1,2,3,4,8,11,Abnormal Termination
Case-8: If an exception raised at stmt-7 and corresponding except block not matched
1,2,3,.,.,.,8,11,Abnormal Termination
Note: If the control entered into try block then compulsary finally block will be
executed. If the control not entered into try block then finally block won't be
executed.
try:
Risky Code
except:
will be executed if exception inside try
else:
will be executed if there is no exception inside try
finally
will be executed whether exception raised or not raised and handled or not
:
handled
E
g:
try print("try")
: print(10/0)
1
except:
print("except")
else:
print("else")
finally
print("finally
:
")
If we are not commenting line-1 then else block won't be executed b'z there is
exception inside try block. In this case output is:
try
excep
t
finall
y
try:
1
print("try")
except:
2
print("Hello")
else:
3
print("Hello")
finally:
4
print("Hello")
try:
print("try"
5
) except: √
print("except")
try:
print("try")
6
finally: √
print("finally")
try:
print("try"
) except:
7
print("except") √
else:
print("else")
try:
print("try")
8
else:
print("else")
try:
print("try")
else:
9
print("else")
finally:
print("finally")
try:
print("try"
) except XXX:
10
print("except-1") √
except YYY:
print("except-2")
try:
print("try")
11
except :
print("except-1")
else:
print("else")
else:
print("else")
try:
print("try"
) except :
print("except-1")
12
finally:
print("finally")
finally:
print("finally")
try:
print("try"
13 ) print("Hello")
except:
print("except")
try:
print("try"
) except:
14 print("except
") print("Hello")
except:
print("except")
try:
print("try"
) except:
15 print("except
") print("Hello")
finally:
print("finally")
try:
print("try"
) except:
16 print("except
") print("Hello")
else:
print("else")
try:
print("try"
) except:
17
print("except") √
try:
print("try")
except:
print("except")
try:
print("try"
) except:
print("except")
18
try: √
print("try")
finally:
print("finally")
try:
print("try"
) except:
print("except
19
") if 10>20: √
print("if")
else:
print("else")
try:
print("try"
) try:
print("inner
try") except:
20
print("inner except √
block") finally:
print("inner finally
block")
except:
print("except")
try:
print("try")
except:
print("except
21 ") try:
print("inner
√
try") except:
print("inner except
block") finally:
print("inner finally
block")
try:
print("try"
22 ) except:
print("except")
√
finally:
try:
print("inner
try") except:
print("inner except
block") finally:
print("inner finally
block")
try:
print("try"
) except:
23
try:
print("except")
print("try")
else:
print("else")
try:
print("try"
) try:
24
print("inner try")
except:
print("except")
try:
print("try")
else:
print("else
25
") except:
print("except")
finally:
print("finally")
Types of Exceptions:
In Python there are 2 types of exceptions are possible.
1) Predefined Exceptions
2) User Definded Exceptions
1)Predefined Exceptions:
Also known as inbuilt exceptions.
The exceptions which are raised automatically by Python virtual machine
whenver a particular event occurs are called pre defined exceptions.
Eg 2: Whenever we are trying to convert input value to int type and if input
value is not int value then Python will raise ValueError automatically
x=int("ten") ValueError
Syntax:
class classname(predefined exception class
name): def init (self,arg):
self.msg=arg
1) class TooYoungException(Exception):
2) def init (self,arg):
3) self.msg=arg
1) class
TooYoungException(Exception):
2) def init (self,arg):
3) self.msg=a
4) rg
5) class
TooOldException(Exception):
6) def init (self,arg):
7) self.msg=a
8) rg
9) age=int(input("Enter Age:"))
10) if age>60:
11) raise TooYoungException("Plz wait some more time you will get best match
soon!!!")
12) elif age<18:
13) raise TooOldException("Your age already crossed marriage age...no chance of
getting marriage")
14) else:
15) print("You will get match details soon by email!!!")
D:\Python_classes>py
test.py Enter Age:90
main .TooYoungException: Plz wait some more time you will get best match soon!!!
D:\Python_classes>py
test.py Enter Age:12
main .TooOldException: Your age already crossed marriage age...no
chance of g etting marriage
D:\Python_classes>py
test.py Enter Age:27
You will get match details soon by email!!!
Note: raise keyword is best suitable for customized exceptions but not for
pre defined exceptions
FILE
HANDLING
Types of Files:
There are 2 types of files
1) Text Files:
Usually we can use text files to store
character data Eg: abc.txt
2) Binary Files:
Usually we can use binary files to store binary data like images,video files,
audio files etc...
Opening a File:
Before performing any operation (like read or write) on the file,first we
have to open that file.For this we should use Python's inbuilt function
open()
But at the time of open, we have to specify mode,which represents the
purpose of opening file.
f = open(filename, mode)
The allowed modes in Python are
2) w open an existing file for write operation. If the file already contains
some data then it will be overridden. If the specified file is not already
avaialble then this mode will create that file.
4) r+ To read and write data into the file. The previous data in the file
will not be deleted.The file pointer is placed at the beginning of the file.
5) w+ To write and read data. It will override existing data.
6) a+ To append and read data from the file.It wont override existing data.
Note: All the above modes are applicable for text files. If the above modes
suffixed with 'b' then these represents for binary files.
Eg: rb,wb,ab,r+b,w+b,a+b,xb
f = open("abc.txt","w")
We are opening abc.txt file for writing data.
Closing a File:
After completing our operations on the file, it is highly recommended to
close the file. For this we have to use close() function.
f.c lose()
1) f=open("abc.txt",'w')
2) print("File Name: ",f.name)
3) print("File Mode: ",f.mode)
4) print("Is File Readable: ",f.readable())
5) print("Is File Writable: ",f.writable())
6) print("Is File Closed : ",f.closed)
7) f.close()
8) print("Is File Closed : ",f.closed)
Output
D:\Python_classes>py
test.py File Name: abc.txt
File Mode: w
Is File Readable:
False Is File
Writable: True Is
File Closed: False
Is File Closed: True
1) f=open("abcd.txt",'w')
2) f.write("Durga\n")
3) f.write("Software\n")
4) f.write("Solutions\n")
5) print("Data written to the file successfully")
6) f.close()
abcd.txt:
Durga
Softwar
e
Solutio
ns
Note: In the above program, data present in the file will be overridden
everytime if we run the program. Instead of overriding if we want append
operation then we should open the file as follows.
f = open("abcd.txt","a")
Eg 2:
1) f=open("abcd.txt",'w')
2) list=["sunny\n","bunny\n","vinny\n","chinny"]
3) f.writelines(list)
4) print("List of lines written to the file successfully")
5) f.close()
abcd.txt:
sunn
y
bunn
y
vinny
chinn
y
Note: While writing data by using write() methods, compulsory we have to provide line
seperator(\n), otherwise total data should be written to a single line.
Outpu
t sunny
bunny
chinny
vinny
Outpu
t sunny
bunn
Outpu
t sunny
bunny
chinny
Outpu
t sunny
bunny
chinny
vinny
Eg 5:
1) f=open("abc.txt","r")
2) print(f.read(3))
3) print(f.readline())
4) print(f.read(4))
5) print("Remaining data")
6) print(f.read())
Output
su
n
ny
bunn
Remaining
data y
chinn
y
vinny
1) with open("abc.txt","w") as f:
2) f.write("Durga\n")
3) f.write("Software\n")
4) f.write("Solutions\n")
5) print("Is File Closed: ",f.closed)
6) print("Is File Closed: ",f.closed)
Output
Is File Closed:
False Is File
Closed: True
1) f=open("abc.txt","r")
2) print(f.tell())
3) print(f.read(2))
4) print(f.tell())
5) print(f.read(3))
6) print(f.tell())
abc.txt:
sunn
y
bunn
y
chinn
y
vinny
Output:
0
s
u
2
nn
y5
seek():
We can use seek() method to move cursor (file pointer) to
specified location. [Can you please seek the cursor to a particular
KESA PAVAN KUMAR Cell No: 9848461833
125
location]
f.seek(offset, fromwhere) offset represents the number of positions
Note: Python 2 supports all 3 values but Python 3 supports only zero.
1) data="All Students are STUPIDS"
2) f=open("abc.txt","w")
3) f.write(data)
4) with open("abc.txt","r+") as f:
5) text=f.read()
6) print(text)
7) print("The Current Cursor Position: ",f.tell())
8) f.seek(17)
9) print("The Current Cursor Position: ",f.tell())
10) f.write("GEMS!!!")
11) f.seek(0)
12) text=f.read()
13) print("Data After Modification:")
14) print(text)
Output
All Students are STUPIDS
The Current Cursor
Position: 24 The Current
Cursor Position: 17 Data
After Modification:
All Students are GEMS!!!
Output
D:\Python_classes>py
test.py Enter File Name:
durga.txt File does not
exist: durga.txt
D:\Python_classes>py
test.py Enter File Name:
abc.txt
File exists: abc.txt
The content of file
is:
All Students are
GEMS!!! All Students
are GEMS!!! All
Students are
GEMS!!! All Students
are GEMS!!! All
Students are
GEMS!!! All Students
are GEMS!!!
Note:
sys.exit(0) To exit system without executing rest of the program.
argument represents status code. 0 means normal termination and it is the default
value.
Output
D:\Python_classes>py
test.py Enter File Name:
durga.txt File does not
exist: durga.txt
D:\Python_classes>py
test.py Enter File Name:
abc.txt
File exists: abc.txt
The number of Lines:
6 The number of
Words: 24
The number of Characters: 149
abc.txt
All Students are
GEMS!!! All Students
are GEMS!!! All
Students are
GEMS!!! All Students
are GEMS!!! All
Students are
GEMS!!! All Students
are GEMS!!!
1) f1=open("rossum.jpg","rb")
2) f2=open("newpic.jpg","wb")
3) bytes=f1.read()
4) f2.write(bytes)
5) print("New Image is available with the name: newpic.jpg")
Note: If we are not using newline attribute then in the csv file blank lines will
be included between data. To prevent these blank lines, newline attribute is
required in Python-3,but in Python-2 just we can specify mode as 'wb' and we
are not required to use newline attribute.
Output
D:\Python_classes>py test.py
import os
os.mkdir("mysub/mysu
b2")
print("mysub2 created inside mysub")
1) import os
2) print(os.listdir("."))
Output
D:\Python_classes>py test.py
['abc.py', 'abc.txt', 'abcd.txt', 'com', 'demo.py', 'durgamath.py', 'emp.csv', '
file1.txt', 'file2.txt', 'file3.txt', 'files.zip', 'log.txt', 'module1.py', 'myl
og.txt', 'newdir', 'newpic.jpg', 'pack1', 'rossum.jpg', 'test.py', ' pycache ']
The above program display contents of current working directory but not
contents of sub directories.
If we want the contents of a directory including sub directories then we
should go for walk() function.
Eg: To display all contents of Current working directory including sub directories:
1) import os
2) for dirpath,dirnames,filenames in os.walk('.'):
3) print("Current Directory Path:",dirpath)
4) print("Directories:",dirnames)
5) print("Files:",filenames)
6) print()
Output
Current Directory Path: .
Directories: ['com', 'newdir', 'pack1', ' pycache ']
Files: ['abc.txt', 'abcd.txt', 'demo.py', 'durgamath.py', 'emp.csv', 'file1.txt', 'file2.txt',
'file3.
txt', 'files.zip', 'log.txt', 'module1.py', 'mylog.txt', 'newpic.jpg', 'rossum.jpg', 'test.py']
os.system("commad string")
The argument is any command which is executing from DOS.
Eg:
import os
os.system("dir
*.py")
os.system("py
abc.py")
KESA PAVAN KUMAR Cell No: 9848461833
135
Note: st_atime, st_mtime and st_ctime returns the time as number of milli
seconds since Jan 1st 1970, 12:00 AM. By using datetime module from
timestamp() function, we can get exact date and time.
Output
os.stat_result(st_mode=33206, st_ino=844424930132788,
st_dev=2657980798, st_nlin k=1, st_uid=0, st_gid=0, st_size=22410,
st_atime=1505451446, st_mtime=1505538999
, st_ctime=1505451446)
Output
File Size in Bytes: 22410
File Last Accessed Time: 2017-09-15 10:27:26.599490
File Last Modified Time: 2017-09-16 10:46:39.245394
12) pickle.dump(e,f)
13) print("Pickling of Employee Object completed...")
14)
15) with open("emp.dat","rb") as f:
16) obj=pickle.load(f)
17) print("Printing Employee Information after unpickling")
18) obj.display()
pick.py:
1) import emp,pickle
2) f=open("emp.dat","wb")
3) n=int(input("Enter The number of Employees:"))
4) for i in range(n):
5) eno=int(input("Enter Employee Number:"))
6) ename=input("Enter Employee Name:")
7) esal=float(input("Enter Employee Salary:"))
8) eaddr=input("Enter Employee Address:")
9) e=emp.Employee(eno,ename,esal,eaddr)
10) pickle.dump(e,f)
11) print("Employee Objects pickled successfully")
unpick.py:
1) import emp,pickle
2) f=open("emp.dat","rb")
3) print("Employee Details:")
4) while True:
5) try:
6) obj=pickle.load(f)
7) obj.display
8) ()
except EOFError:
9) print("All employees
10 Completed")
brea
)11) k
f.close()
MULTI
THREADING
Multi Tasking:
Executing several tasks simultaneously is the concept of multitasking.
Note: Where ever a group of independent jobs are available, then it is highly
recommended to execute simultaneously instead of executing one by one. For
such type of cases we should go for Multi Threading.
Python provides one inbuilt module "threading" to provide support for
developing threads. Hence developing multi threaded Programs is very
easy in python.
Every Python Program by default contains one thread which is
nothing but MainThread.
Note: Thread is a pre defined class present in threading module which can
be used to create our own Threads.
3)
4) defforrun(self):
i in
5) range(10):
print("Child Thread-
1")
6) t=MyThread()
7) t.start()
8) for i in range(10):
9) print("Main Thread-
1")
3)Creating a Thread without extending Thread Class
1) from threading import *
2) class Test:
3) def display(self):
4) for i in range(10):
5) print("Child Thread-2")
6) obj=Test()
7) t=Thread(target=obj.display)
8) t.start()
9) for i in range(10):
10) print("Main Thread-2")
3)
4) deffor n in numbers:
doubles(numbers):
5) time.sleep(
6) 1)
print("Double:",2*
7) def n)
squares(numbers):
8) for n in numbers:
9) time.sleep(
10 1)
print("Square:",n*
)11) numbers=[1,2,3,4,5,6]
n)
12) begintime=time.time()
13) doubles(numbers)
14) squares(numbers)
15) print("The total time taken:",time.time()-
begintime)
We can get and set name of thread by using the following Thread class
methods. t.getName() Returns Name of Thread
t.setName(newName) To set our own name
Note: Every Thread has implicit variable "name" to represent name of Thread.
1) from threading import *
2) print(current_thread().getName())
3) current_thread().setName("Pawan Kalyan")
4) print(current_thread().getName())
5) print(current_thread().name)
Output
MainThread
Pawan
Kalyan
Pawan
Kalyan
Output:
Child Thread
Main Thread Identification Number:
2492 Child Thread Identification
Number: 2768
active_count():
This function returns the number of active threads currently running.
Output:
D:\python_classes>py
test.py The Number of
active Threads: 1
ChildThread1 ...started
ChildThread2 ...started
ChildThread3 ...started
The Number of active Threads: 4
ChildThread1
...ended
ChildThread2
...ended
ChildThread3
...ended
The Number of active Threads: 1
enumerate() Function:
This function returns a list of all active threads currently running.
Output:
D:\python_classes>py
test.py ChildThread1
...started ChildThread2
...started ChildThread3
...started Thread Name:
MainThread Thread
Name: ChildThread1
Thread Name:
ChildThread2 Thread
Name: ChildThread3
ChildThread1 ...ended
ChildThread2 ...ended
ChildThread3 ...ended
Thread Name:
MainThread
isAlive() Method:
isAlive() method checks whether a thread is still executing or not.
Output:
D:\python_classes>py
test.py ChildThread1
...started ChildThread2
...started ChildThread1 is
Alive : True ChildThread2
is Alive : True
ChildThread1 ...ended
ChildThread2 ...ended
ChildThread1 is Alive :
False ChildThread2 is
Alive : False
join() Method:
If a thread wants to wait until completing some other thread then we should
go for join() method.
3)
4) deffor
display():
i in range(10):
5) print("Seetha
6) Thread")
time.sleep(
7) 2)
8) t=Thread(target=display)
9)
t.start()
KESA PAVAN KUMAR Cell No: 9848461833
147
In the above example Main Thread waited until completing child thread.
In this case output is:
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Output
Seetha
Thread
Seetha
Thread
Seetha
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Rama
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Seetha
Thread
Daemon Threads:
The threads which are running in the background are called Daemon Threads.
The main objective of Daemon Threads is to provide support for
Non Daemon Threads( like main thread)
KESA PAVAN KUMAR Cell No: 9848461833
150
But we can use this method before starting of Thread.i.e once thread
started,we cannot change its Daemon nature,otherwise we will get
RuntimeException:cannot set daemon status of active thread.
Default Nature:
By default Main Thread is always non-daemon.But for the remaining threads
Daemon nature will be inherited from parent to child.i.e if the Parent Thread is
Daemon then child thread is also Daemon and if the Parent Thread is Non
Daemon then ChildThread is also Non Daemon.
Note: Main Thread is always Non-Daemon and we cannot change its Daemon
Nature b'z it is already started at the beginning only.
3)
4) deffor
job():
i in range(10):
5) print("Lazy
6) Thread")
time.sleep(
7) 2)
8) t=Thread(target=job)
9)
#t.setDaemon(True)===>Line-
10) t.start()
1
12) print("End Of Main Thread")
11) time.sleep(5)
In the above program if we comment Line-1 then both Main Thread and Child
Threads are Non Daemon and hence both will be executed until their
completion.
In this case output is:
Lazy
Thread
Lazy
Thread
Lazy
Thread
End Of Main
Thread Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
Lazy
Thread
End of Main Thread
Synchronization:
If multiple threads are executing simultaneously then there may be a chance
of data inconsistency problems.
4) deffor
3) wish(name):
i in range(10):
5) print("Good
6) Evening:",end='')
time.sleep(
7) 2)
print(nam
e)
8) t1=Thread(target=wish,args=("Dhoni",))
9)
t2=Thread(target=wish,args=("Yuvraj"
10) t1.start()
,))
11) t2.start()
Output
Good Evening:Good
Evening:Yuvraj Dhoni
Good Evening:Good
Evening:Yuvraj Dhoni
....
If we are commenting line-1 then we will get RuntimeError: release unlocked lock
3) l=Lock()
4) def wish(name):
5)
6) l.acquire
for i in range(10):
()
7) print("Good
8) Evening:",end='')
time.sleep(
9) 2)
print(nam
e)
10) l.release()
11)
12) t1=Thread(target=wish,args=("Dhoni",))
13)
t2=Thread(target=wish,args=("Yuvraj"
14) t3=Thread(target=wish,args=("Kohli",))
,))
16) t2.start()
15)
17) t1.start()
t3.start(
)
In the above program at a time only one thread is allowed to execute wish()
method and hence we will get regular output.
Output
D:\python_classes>py test.py
Main Thread trying to acquire
Lock
Main Thread trying to acquire Lock Again
--
In the above Program main thread will be blocked b'z it is trying to acquire the
lock second time.
Note: To kill the blocking thread from windows command prompt we have to
use ctrl+break. Here ctrl+C won't work.
Hence Traditional Locking mechanism won't work for executing recursive functions.
Reentrant facility is available only for owner thread but not for other threads.
In this case Main Thread won't be Locked b'z thread can acquire the lock any
number of times.
This RLock keeps track of recursion level and hence for every acquire() call
compulsory release() call should be available. i.e the number of acquire()
calls and release() calls should be matched then only lock will be released.
Eg:
l=RLock(
)
l.acquire
()
l.acquire
()
l.release
()
l.release
()
Note:
1) Only owner thread can acquire the lock multiple times
2) The number of acquire() calls and release() calls should be matched.
8) else:
9) result=n*factorial(n-1)
10) l.release()
11) return result
12)
13) def results(n):
14) print("The Factorial of",n,"is:",factorial(n))
15)
16) t1=Thread(target=results,args=(5,))
17) t2=Thread(target=results,args=(9,))
18) t1.start()
19) t2.start()
Output:
The Factorial of 5 is: 120
The Factorial of 9 is: 362880
In the above program instead of RLock if we use normal Lock then the thread
will be blocked.
Case-1: s = Semaphore()
In this case counter value is 1 and at a time only one thread is allowed to
access. It is exactly same as Lock concept.
Case-2: s = Semaphore(3)
In this case Semaphore object can be accessed by 3 threads at a time.The
remaining threads have to wait until releasing the semaphore.
3) s=Semaphore(2)
4) def wish(name):
5)
6) s.acquire()
for i in range(10):
7) print("Good
8) Evening:",end='')
time.sleep(
9) 2)
print(nam
e)
10) s.release()
11)
12) t1=Thread(target=wish,args=("Dhoni",))
13)
t2=Thread(target=wish,args=("Yuvraj"
14) t3=Thread(target=wish,args=("Kohli",))
,))
16) t5=Thread(target=wish,args=("Pandya",))
15)
t4=Thread(target=wish,args=("Rohit",)
18) t2.start()
)
20) t4.start()
17) t1.start()
19) t3.start()
In the above program at a time 2 threads are allowed to access semaphore
and hence 2 threads are allowed to execute wish() function.
21) t5.start()
Bounded Semaphore:
Normal Semaphore is an unlimited semaphore which allows us to call
release() method any number of times to increment counter.The number of
release() calls can exceed the number of acquire() calls also.
☕ It is valid because in normal semaphore we can call release() any number of times.
☕ BounedSemaphore is exactly same as Semaphore except that the number
of release() calls should not exceed the number of acquire()
calls,otherwise we will get ValueError: Semaphore released too many
times
Conclusion:
The main advantage of synchronization is we can overcome data
inconsistency problems.But the main disadvantage of synchronization is it
increases waiting time of threads and creates performance problems. Hence if
there is no specific requirement then it is not recommended to use
synchronization.
Pseudo Code:
event = threading.Event()
#consumer thread has to wait until event
is set event.wait()
#producer thread can set or clear
event event.set()
event.clear()
Demo Program-1:
17) t1.start()
16) t2.start()
Output:
Consumer thread is waiting for
updation Producer thread
producing items
Producer thread giving notification by setting
event Consumer thread got notification and
consuming items
Demo Program-2:
1) from threading
import * time
2) import
3)
4) defwhile
trafficpolice():
True:
5) time.sleep(1
6) 0)
print("Traffic Police Giving GREEN
7) Signal")
event.set
8) ()
time.sleep(2
9) 0)
print("Traffic Police Giving RED
10 Signal")
event.clear
)11) def ()
driver():
12) num=0
13)
14 while
print("Drivers waiting for GREEN
True:
)
15 Signal")
event.wait
)16 ()
print("Traffic Signal is GREEN...Vehicles can
)17 move")
while
) event.isSet():
KESA PAVAN KUMAR Cell No: 9848461833
162
18) num=num+1
19) print("Vehicle No:",num,"Crossing the Signal")
20) time.sleep(2)
21) print("Traffic Signal is RED...Drivers have to wait")
22) event=Event()
23) t1=Thread(target=trafficpolice)
24) t2=Thread(target=driver)
25) t1.start()
26) t2.start()
In the above program driver thread has to wait until Trafficpolice thread sets
event.ie until giving GREEN signal.Once Traffic police thread sets event(giving
GREEN signal),vehicles can cross the signal. Once traffic police thread clears
event (giving RED Signal)then the driver thread has to wait.
Methods of Condition:
1) acquire() To acquire Condition object before producing or
consuming items.i.e thread acquiring internal lock.
Case Study:
The producing thread needs to acquire the Condition before producing item
to the resource and notifying the consumers.
#Producer Thread
...generate item..
condition.acquir
e()
...add item to the resource...
condition.notify()#signal that a new item is
available(notifyAll()) condition.release()
The Consumer must acquire the Condition and then it can consume items
from the resource
#Consumer
Thread
condition.acquir
condition.wait()
e()
consume item
condition.releas
e()
Demo Program-1:
1) from threading import *
2) def consume(c):
3) c.acquire()
4) print("Consumer waiting for updation")
5) c.wait()
6) print("Consumer got notification & consuming the item")
7) c.release()
8)
9) def produce(c):
10) c.acquire()
11) print("Producer Producing Items")
12) print("Producer giving Notification")
13) c.notify()
14) c.release()
15)
16) c=Condition()
17) t1=Thread(target=consume,args=(c,))
18) t2=Thread(target=produce,args=(c,))
19) t1.start()
20) t2.start()
Output
Consumer waiting for
updation Producer
Producing Items Producer
giving Notification
Consumer got notification & consuming the item
Demo Program-2:
3) import random
4) items=[]
5)
6) def produce(c):
while True:
7) c.acquire
8) ()
item=random.randint(1,1
9) 00)
print("Producer Producing
10 Item:",item)
items.append(ite
)11 m)
print("Producer giving
)
12 Notification")
c.notify
)13 ()
c.release
)14 ()
time.sleep(
)15) 5)
16) def consume(c):
17) while
True: c.acquire
18
)19 ()
print("Consumer waiting for
)20 updation")
c.wait(
)21 )print("Consumer consumed the
)22 item",items.pop())
c.release
)23 ()
time.sleep(
)
24 5)
)25) c=Condition()
26) t1=Thread(target=consume,args=(c,))
27)
t2=Thread(target=produce,args=(c,
28) t1.start()
))
29) t2.start()
Output
Consumer waiting for
updation Producer
Producing Item: 49 Producer
giving Notification
Consumer consumed the
item 49
.....
Producer Thread uses put() method to insert data in the queue. Internally this
method has logic to acquire the lock before inserting data into queue. After
inserting data lock will be released automatically.
put() method also checks whether the queue is full or not and if queue is full
then the Producer thread will entered in to waiting state by calling wait()
method internally.
Consumer Thread uses get() method to remove and get data from the queue.
Internall
y this method has logic to acquire the lock before removing data from the
queue.Once removal completed then the lock will be released automatically.
If the queue is empty then consumer thread will entered into waiting state by
calling wait() method internally.Once queue updated with data then the thread
will be notified automatically.
Note: The Queue Module takes care of locking for us which is a great Advantage.
Output
Consumer waiting for
updation Producer
Producing Item: 58 Producer
giving Notification Consumer
consumed the item: 58
Types of Queues:
Python Supports 3 Types of Queues.
1) FIFO Queue:
q = queue.Queue()
This is Default Behaviour. In which order we put items in the queue, in the
same order the items will come out (FIFO-First In First Out).
1) import queue
2) q=queue.Queue()
3) q.put(10)
4) q.put(5)
5) q.put(20)
6) q.put(15)
7) while not
q.empty():
8) print(q.get(),end ')
='
Output: 10 5 20 15
2) LIFO Queue:
The removal will be happend in the reverse order of Insertion (Last In First Out)
1) import queue
2) q=queue.LifoQueue()
3) q.put(10)
4) q.put(5)
5) q.put(20)
6) q.put(15)
7) while not
q.empty():
8) print(q.get(),end ')
='
Output: 15 20 5 10
3) Priority Queue:
The elements will be inserted based on some priority order.
1) import queue
2) q=queue.PriorityQueue()
3) q.put(10)
4) q.put(5)
5) q.put(20)
6) q.put(15)
7) while not
q.empty():
8) print(q.get(),end ')
='
Output: 5 10 15 20
Eg 2: If the data is non-numeric, then we have to provide our data in the form
of tuple. (x,y)
x is priority
y is our element
1) import queue
2) q=queue.PriorityQueue()
3) q.put((1,"AAA"))
4) q.put((3,"CCC"))
5) q.put((2,"BBB"))
6) q.put((4,"DDD"))
7) while not q.empty():
8) print(q.get()[1],end ')
='
l=
threading.Lock()
l.acquire()
try:
perform required safe
operations finally:
l.release()
Demo Program:
1) from threading import
*2) import time
3) l=Lock()
4) def wish(name):
5)
6) l.acquire()
try:
7) for i in
8) range(10):
print("Good
9) Evening:",end='')
time.sleep(
10 2)
print(nam
)11) finally: e)
12 l.release
)13) ()
14) t1=Thread(target=wish,args=("Dhoni",))
15)
t2=Thread(target=wish,args=("Yuvraj"
16) t3=Thread(target=wish,args=("Kohli",))
,))
18) t2.start()
17)
19) t1.start()
t3.start(
)
Case-2:
It is highly recommended to acquire lock by using with statement. The main
advantage of with statement is the lock will be released automatically once
control reaches end of with block and we are not required to release
explicitly.
This is exactly same as usage of with statement for files.
Demo Program:
1) from threading import
*2) import time
3) lock=Lock()
4) def wish(name):
5)
6) with
for lock:
i in
7) range(10):
print("Good
8) Evening:",end='')
time.sleep(
9) 2)
print(nam
e)
10) t1=Thread(target=wish,args=("Dhoni",))
11)
t2=Thread(target=wish,args=("Yuvraj"
12) t3=Thread(target=wish,args=("Kohli",))
,))
14) t2.start()
13) t1.start()
15) t3.start()
Q) What is the Advantage of using with Statement to acquire a Lock in
Threading?
Lock will be released automatically once control reaches end of with block
and we are not required to release explicitly.
Note: We can use with statement in multithreading for the following cases:
1) Lock
2) RLock
3) Semaphore
4) Condition
PYTHON
DATABASE
PROGRAMMING
Storage Areas
As the Part of our Applications, we required to store our Data like Customers
Information, Billing Information, Calls Information etc.
To store this Data, we required Storage Areas. There are 2 types of Storage Areas.
File Systems:
File Systems can be provided by Local operating System. File Systems are
best suitable to store very less Amount of Information.
Limitations:
1) We cannot store huge Amount of Information.
2) There is no Query Language support and hence operations will become very
complex.
3) There is no Security for Data.
4) There is no Mechanism to prevent duplicate Data. Hence there may be a
chance of Data Inconsistency Problems.
Databases:
1) We can store Huge Amount of Information in the Databases.
2) Query Language Support is available for every Database and hence we
can perform Database Operations very easily.
3) To access Data present in the Database, compulsory username and
pwd must be required. Hence Data is secured.
4) Inside Database Data will be stored in the form of Tables. While developing
Database Table Schemas, Database Admin follow various Normalization
Techniques and can implement various Constraints like Unique Key
Constrains, Primary Key Constraints etc which prevent Data Duplication.
Hence there is no chance of Data Inconsistency Problems.
Limitations of Databases:
1) Database cannot hold very Huge Amount of Information like Terabytes of Data.
2) Database can provide support only for Structured Data (Tabular Data OR
Relational Data) and cannot provide support for Semi Structured Data (like
XML Files) and Unstructured Data (like Video Files, Audio Files, Images
etc)
4) Execute SQL Queries By using Cursor object. For this we can use the following
methods
⚽ execute(sqlquery) To execute a Single SQL Query
⚽ executescript(sqlqueries) To execute a String of SQL Queries
seperated by semi- colon ';'
⚽ executemany() To execute a Parameterized Query.
Eg: cursor.execute("select * from employees")
Eg 1: data =
cursor.fetchone()
print(data)
Eg 2: data =
cursor.fetchall() for
row in data:
print(row)
Note: The following is the list of all important methods which can be used for
python database programming.
⚽ connect()
⚽ cursor()
⚽ execute()
⚽ executescript()
⚽ executemany()
⚽ commit()
⚽ rollback()
⚽ fetchone()
⚽ fetchall()
⚽ fetchmany(n)
⚽ fetch
⚽ close()
These methods won't be changed from database to database and same for all
databases.
Diagram
Installing cx_Oracle:
From Normal Command Prompt (But not from Python console) execute
the following command
D:\python_classes>pip install
Output
D:\python_classes>py
db1.py 11.2.0.2.0
1) import cx_Oracle
2) try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5) cursor.execute("create table employees(eno number,ename
varchar2(10),esal n umber(10,2),eaddr varchar2(10))")
6) print("Table created successfully")
7) except cx_Oracle.DatabaseError as e:
8) if con:
9) con.rollbac
10 k()
print("There is a problem with
)11) sql",e)
finally:
12) if cursor:
13 cursor.clos
)14) if con:
e()
15 con.clos
) e()
17) except
cx_Oracle.DatabaseError
18) if con: as e:
19 con.rollbac
)20 k()
print("There is a problem with sql
)21) :",e)
finally:
22) if cursor:
23 cursor.clos
)
24) e()
if con:
25 con.clos
) e()
App 6) Write a Program to Update Employee Salaries with
Increment for the certain Range with Dynamic Input
Eg: Increment all employee salaries by 500 whose salary < 5000
1) import cx_Oracle
2) try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5) increment=float(input("Enter Increment Salary:"))
6) salrange=float(input("Enter Salary Range:"))
7) sql="update employees set esal=esal+%f where
esal<%f"
8) cursor.execute(sql %(increment,salrange))
9) print("Records Updated Successfully")
10) con.commit()
11) except cx_Oracle.DatabaseError as e:
12) if con:
13 con.rollbac
)
14 k()
print("There is a problem with sql
)15) :",e)
finally:
16) if cursor:
17 cursor.clos
)18) if con:
e()
19 con.clos
) e()
App 7) Write a Program to Delete Employees
whose Salary Greater provided Salary as
Dynamic Input?
Eg: delete all employees whose salary > 5000
1) import cx_Oracle
2) try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5) cutoffsalary=float(input("Enter CutOff Salary:"))
Output
D:\python_classes>py test.py
Enter the number of required
rows:3 (100, 'Durga', 1500.0,
'Hyd')
(200, 'Sunny', 2500.0, 'Mumbai')
(300, 'Chinny', 3500.0, 'Hyd')
D:\python_classes>py test.py
Enter the number of required
rows:4 (100, 'Durga', 1500.0,
'Hyd')
(200, 'Sunny', 2500.0, 'Mumbai')
(300, 'Chinny', 3500.0, 'Hyd')
(400, 'Bunny', 4500.0, 'Hyd')
Note: In MySQL, everything we have to work with our own databases, which
are also known as Logical Databases.
In the above diagram only one physical database is available and 4 logical
databases are available.
5) To Create a Table
create table employees(eno int(5) primary key,ename varchar(10),esal
double(10,2), eaddr varchar(10));
6) To Insert Data
insert into employees
values(100,'Durga',1000,'Hyd'); insert into
employees values(200,'Ravi',2000,'Mumbai');
Driver/Connector Information:
From Python program if we want to communicates with MySql database,
compulsory some translator is required to convert python specific calls into
mysql database specific calls and mysql database specific calls into python
specific calls. This translator is nothing but Driver or Connector.
explicitly PATH=C:\Python34
PYTHONPATH=C:\Python34\Lib\site-packages
6) print("Table Created...")
7)
8) sql = "insert into employees(eno, ename, esal, eaddr) VALUES(%s, %s,
%s, %s)"
9) records=[(100,'Sachin',1000,'Mumbai'),
10) (200,'Dhoni',2000,'Ranchi'),
11) (300,'Kohli',3000,'Delhi')]
12) cursor.executemany(sql,records)
13) con.commit()
14) print("Records Inserted Successfully...")
15) cursor.execute("select * from employees")
16) data=cursor.fetchall()
17) for row in data:
18) print("Employee Number:",row[0])
19) print("Employee Name:",row[1])
20) print("Employee Salary:",row[2])
21) print("Employee Address:",row[3])
22) print()
23) print()
24) except mysql.connector.DatabaseError as e:
25) if con:
26) con.rollback()
27) print("There is a problem with sql :",e)
28) finally:
29) if cursor:
30) cursor.close()
31) if con:
32) con.close()
13) if con:
14) con.rollback()
15) print("There is a problem with MySql :",e)
16) finally:
17) if cursor:
18) cursor.close()
19) if con:
20) con.clos
e() 21)
22) try:
23) con=cx_Oracle.connect('scott/tiger@localhost')
24) cursor=con.cursor()
25) sql="insert into employees values(:eno,:ename,:esal,:eaddr)"
26) cursor.executemany(sql,list)
27) con.commit()
28) print("Records Copied from MySQL Database to Oracle Database
Successfully")
https://dev.mysql.com/downloads/connector/python/2.1.html
REGULAR
EXPRESSIONS
&
WEB SCRAPING
1) compile()
Returns Module contains compile() Function to compile a Pattern into
RegexObject. pattern = re.compile("ab")
2) finditer():
Returns an Iterator object which yields Match object for every
Match matcher = pattern.finditer("abaababa")
1) import re count=0
2) pattern=re.compile("ab")
3) matcher=pattern.finditer("abaababa")
4) for match in matcher:
5) count+=1
6) print(match.start(),"...",match.end(),"...",match.group())
7) print("The number of occurrences: ",count)
Output:
0 ... 2 ... ab
3 ... 5 ... ab
5 ... 7 ... ab
Output:
0 ... 2 ... ab
3 ... 5 ... ab
5 ... 7 ... ab
The number of occurrences: 3
Character Classes:
We can use character classes to search a group of characters
1) [abc] Either a OR b OR c
2) [^abc] Except a and b and c
3) [a-z] Any Lower case alphabet symbol
4) [A-Z] Any upper case alphabet symbol
5) [a-zA-Z] Any alphabet symbol
6) [0-9] Any digit from 0 to 9
7) [a-zA-Z0-9] Any alphanumeric character
8) [^a-zA-Z0-9] Except alphanumeric characters(Special Characters)
1) import re
2) matcher=re.finditer("x","a7b@k9z")
3) for match in matcher:
4) print(match.start(),". . ",match.group())
1) import re
2) matcher=re.finditer("x","a7b k@9z")
3) for match in matcher:
4) print(match.start(),". . ",match.group())
x = \s: x = \S: x = \d: x = \D: x = \w: x = \W: x=.
3 ...... 0 ...... a 1 ...... 7 0 ...... a 0 ...... a 3 ...... 0 ...... a
1 ...... 7 6 ...... 9 2 ...... b 1 ...... 7 5 ...... @ 1 ...... 7
2 ...... b 3 ...... 2.......b 2 ...... b
4 ...... k 4 ...... k 4.......k 3 ......
5 ...... @ 5 ...... @ 6 ...... 9 4 ...... k
6 ...... 9 7 ...... z 7.......z 5 ......
@
7 ...... z 6 ...... 9
7 ...... z
Qunatifiers:
We can use quantifiers to specify the number of occurrences to match.
1) a Exactly one 'a'
2) a+ Atleast one 'a'
3) a* Any number of a's including zero number
4) a? Atmost one 'a' ie either zero number or one number
5) a{m} Exactly m number of a's
6) a{m,n} Minimum m number of a's and Maximum n number of a's.
1) import re
2) matcher=re.finditer("x","abaabaaab")
3) for match in
4) print(match.start(),". . ",match.group())
matcher:
x=
a?: x= x = a{2,4}:
0.......a a{3}: 2 ...... aa
x = a: x = a+: x = a*:
1 ...... 5 5
0 ...... a 0 ...... a 0 ...... a
2a ........ aa
2 ...... a 2 ...... aa 1 ...... ........ aa
3a a
3 ...... a 5 ...... aaa 2 ...... aa a
4 ......
5 ...... a 4 ...... 5a
6 ...... a 5 ...... aaa 6a
7 ...... a 8 ...... 7a
9 ...... 8 ......
9 ......
Note:
1) ^x It will check whether target string starts with x OR not.
2) x$ It will check whether target string ends with x OR not.
1) match():
We can use match function to check the given pattern at beginning of target string.
If the match is available then we will get Match object, otherwise we will get None.
1) import re
2) s=input("Enter pattern to check: ")
3) m=re.match(s,"abcabdefg")
4) if m!= None:
5) print("Match is available at the beginning of the String")
6) print("Start Index:",m.start(), "and End Index:",m.end())
7) else:
8) print("Match is not available at the beginning of the String")
Output:
D:\python_classes>py
test.py Enter pattern to
check: abc
Match is available at the beginning of the
String Start Index: 0 and End Index: 3
D:\python_classes>py
test.py Enter pattern to
check: bde
Match is not available at the beginning of the String
2) fullmatch():
We can use fullmatch() function to match a pattern to all of target string.
i.e complete string should be matched according to given pattern.
If complete string matched then this function returns Match object
otherwise it returns None.
1) import re
2) s=input("Enter pattern to check: ")
3) m=re.fullmatch(s,"ababab")
4) if m!= None:
5) print("Full String Matched")
6) else:
7) print("Full String not Matched")
Output:
D:\python_classes>py
test.py Enter pattern to
check: ab Full String not
Matched
D:\python_classes>py
test.py Enter pattern to
check: ababab Full String
Matched
3) search():
We can use search() function to search the given pattern in the target string.
If the match is available then it returns the Match object which
represents first occurrence of the match.
If the match is not available then it returns None
1) import re
2) s=input("Enter pattern to check: ")
3) m=re.search(s,"abaaaba")
4) if m!= None:
5) print("Match is available")
6) print("First Occurrence of match with start index:",m.start(),"and end
index:",m.
end())
7) else:
8) print("Match is not available")
Output:
D:\python_classes>py
test.py Enter pattern to
check: aaa Match is
available
First Occurrence of match with start index: 2 and end index: 5
D:\python_classes>py
test.py Enter pattern to
check: bbb Match is not
available
4) findall():
To find all occurrences of the match.
This function returns a list object which contains all occurrences.
1) import re
2) l=re.findall("[0-9]","a7b9c5kz")
3) print(l)
5) finditer():
Returns the iterator yielding a match object for each match.
On each match object we can call start(), end() and group() functions.
1) import re
2) itr=re.finditer("[a-z]","a7b9c5k8z")
3) for m in itr:
4) print(m.start(),"...",m.end(),"...",m.group())
6) sub():
sub means substitution or replacement.
re.sub(regex,replacement,targetstring)
In the target string every matched pattern will be replaced with provided
replacement.
1) import re
2) s=re.sub("[a-z]","#","a7b9c5k8z")
3) print(s)
Output: #7#9#5#8#
Every alphabet symbol is replaced with # symbol
7) subn():
It is exactly same as sub except it can also returns the number of replacements.
This function returns a tuple where first element is result string and
second element is number of replacements.
(resultstring, number of replacements)
1) import re
2) t=re.subn("[a-z]","#","a7b9c5k8z")
3) print(t)
4) print("The Result String:",t[0])
5) print("The number of replacements:",t[1])
Output:
D:\python_classes>py
test.py ('#7#9#5#8#', 5)
The Result String:
#7#9#5#8# The number of
replacements: 5
8) split():
If we want to split the given target string according to a particular
pattern then we should go for split() function.
This function returns list of all tokens.
1) import re
2) l=re.split(",","sunny,bunny,chinny,vinny,pinny")
3) print(l)
4) for t in l:
5) print(t)
Output:
D:\python_classes>py test.py
['sunny', 'bunny', 'chinny', 'vinny',
'pinny'] sunny
bunn
y
chinn
y
vinny
pinny
1) import re
2) l=re.split("\.","www.durgasoft.com")
3) for t in l:
4) print(t)
Output:
D:\python_classes>py
test.py www
durgaso
ft com
9) ^ symbol:
We can use ^ symbol to check whether the given target string starts with
our provided pattern or not.
Eg: res = re.search("^Learn",s)
If the target string starts with learn then it will return Match
object,otherwise returns None.
test.py
1) import re
2) s="Learning Python is Very Easy"
3) res=re.search("^Learn",s)
4) if res != None:
5) print("Target String starts with Learn")
6) else:
7) print("Target String Not starts with Learn")
10) $ symbol:
We can use $ symbol to check whether the given target string
ends with our provided pattern or not.
Eg: res = re.search("Easy$",s)
If the target string ends with Easy then it will return Match
object,otherwise returns None.
test.py
1) import re
2) s="Learning Python is Very Easy"
3) res=re.search("Easy$",s)
4) if res != None:
5) print("Target String ends with Easy")
6) else:
7) print("Target String Not ends with Easy")
test.py
1) import re
2) s="Learning Python is Very Easy"
3) res=re.search("easy$",s,re.IGNORECASE)
4) if res != None:
5) print("Target String ends with Easy by ignoring case")
6) else:
7) print("Target String Not ends with Easy by ignoring case")
Output
D:\python_classes>py
test.py Enter
Identifier:a6kk9z##
a6kk9z## is valid Yava Identifier
D:\python_classes>py
test.py Enter
Identifier:k9b876 k9b876
is valid Yava Identifier
D:\python_classes>py
test.py Enter
Identifier:k7b9
k7b9 KESA
is invalid Yava Identifier
PAVAN KUMAR Cell No: 9848461833
196
[7-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-
9][0-9] OR
[7-9][0-
9]{9} OR
[7-9]\d{9}
Output
D:\python_classes>py
test.py Enter
number:9898989898 Valid
Mobile Number
D:\python_classes>py
test.py Enter
number:6786786787
Invalid Mobile Number
D:\python_classes>py
test.py Enter
number:898989
Invalid Mobile Number
1) import re,urllib
2) import urllib.request
3) sites="google rediff".split()
4) print(sites)
5) for s in sites:
6) print("Searching...",s)
7) u=urllib.request.urlopen("http://"+s+".com")
8) text=u.read()
9) title=re.findall("<title>.*</title>",str(text),re.I)
10) print(title[0])
6) else:
7) print("Invalid Mail id")
Output:
D:\python_classes>py test.py
Enter Mail
id:[email protected] Valid
Mail Id
D:\python_classes>py
test.py Enter Mail
id:durgatoc Invalid Mail
id
D:\python_classes>py test.py
Enter Mail
id:[email protected]
Invalid Mail id
Output:
D:\python_classes>py test.py
Enter Vehicle Registration
Number:TS07EA7777 Valid Vehicle
Registration Number
D:\python_classes>py test.py
Enter Vehicle Registration
Number:TS07KF0786 Valid Vehicle
Registration Number
D:\python_classes>py test.py
Enter Vehicle Registration
Number:AP07EA7898 Invalid Vehicle
Registration Number
DECORATOR
FUNCTIONS
Decorator is a function which can take a function as argument and extend its
functionality and returns modified function with extended functionality.
1) def wish(name):
2) print("Hello",name,"Good Morning")
This function can always print same output for any name
1) def decor(func):
2) def inner(name):
3) if name=="Sunny":
4) print("Hello Sunny Bad Morning")
5) else:
6) func(name)
7) return inner
8)
9) @decor
10) def wish(name):
11) print("Hello",name,"Good Morning")
12)
13) wish("Durga")
14) wish("Ravi")
15) wish("Sunny")
Output
Hello Durga Good
Morning Hello Ravi
Good Morning
1) def decor(func):
2) def inner(name):
3) if name=="Sunny":
4) print("Hello Sunny Bad Morning")
5) else:
6) func(name)
7) return inner
8)
9) def wish(name):
10) print("Hello",name,"Good Morning")
11)
12) decorfunction=decor(wish)
13)
14) wish("Durga") #decorator wont be executed
15) wish("Sunny") #decorator wont be executed
16)
17) decorfunction("Durga")#decorator will be executed
18) decorfunction("Sunny")#decorator will be executed
Output
Hello Durga Good
Morning Hello Sunny
Good Morning Hello
Durga Good Morning
Hello Sunny Bad
Morning
1) def smart_division(func):
2) def inner(a,b):
3) print("We are dividing",a,"with",b)
4) if b==0:
5) print("OOPS...cannot divide")
6) return
7) else:
8) return func(a,b)
9) return inner
10)
11) @smart_division
12) def division(a,b):
13) return a/b
14) print(division(20,2))
15) print(division(20,0))
With Decorator we won't get any Error. In this Case Output is:
We are dividing 20 with 2
10.0
We are dividing 20
with 0 OOPS...cannot
divide None
1) def marriagedecor(func):
2) def inner():
3) print('Hair decoration...')
4) print('Face decoration with Platinum
5) package')
print('Fair and Lovely
6) etc..')
func(
7) )
return inner
8)
9) def getready():
10) print('Ready for the marriage')
11)
12) decorated_getready=marriagedecor(getready)
13)
14) decorated_getready()
Decorator Chaining
We can define multiple decorators for the same function and all these
decorators will form Decorator Chaining.
Eg:
@decor1
@decor
def
num():
1) def decor1(func):
2) def inner():
3) x=func()
4) return x*x
5) return inner
6)
7) def decor(func):
8) def inner():
9) x=func()
10) return 2*x
11) return inner
12)
13) @decor1
14) @decor
15) def num():
16) return 10
17)
18) print(num())
GENERATOR
FUNCTIONS
1) def mygen():
2) yield 'A'
3) yield 'B'
4) yield 'C'
5)
6) g=mygen()
7) print(type(g))
8)
9) print(next(g))
10) print(next(g))
11) print(next(g))
12) print(next(g))
Output
<class
'generator'> A
B
C
Traceback (most recent call
last): File "test.py", line 12, in
<module> print(next(g))
StopIteration
1) def countdown(num):
2) print("Start Countdown")
3) while(num>0):
4) yield num
5) num=num-1
6) values=countdown(5)
7) for x in values:
8) print(x)
Output
Start
Countdown 5
4
3
2
1
Output
1
2
3
4
5
Eg 4: To generate Fibonacci
Numbers... The next is the sum of
previous 2 numbers
Eg: 0,1,1,2,3,5,8,13,21,...
1) def fib():
2) a,b=0,1
3) while True:
4) yield a
5) a,b=b,a+b
6) for f in fib():
7) if f>100:
8) break
9) print(f)
Output
0
1
1
2
3
5
8
13
21
34
55
89
21 'id':i,
)
22 'name': random.choice(names),
)
23 'major':random.choice(subj
)
24 } ects)
)25 yield
)26 person
)27) '''''t1 = time.clock()
28) people = people_list(10000000)
29) t2 = time.clock()'''
30
)31) t1 = time.clock()
32) people = people_generator(10000000)
33) t2 = time.clock()
34
35)
) print('Took {}'.format(t2-
t1))
Note: In the above program observe the differnce wrt execution time by
using list and generators
We will get MemoryError in this case because all these values are required to
store in the memory.
Generators:
g=(x*x for x in
range(10000000000000000))
print(next(g))
Output: 0
We won't get any MemoryError because the values won't be stored at the beginning
ASSERTIONS
1) Simple Version:
assert conditional_expression
2) Augmented Version:
assert conditional_expression, message
conditional_expression will be evaluated and if it is true then the
program will be continued.
If it is false then the program will be terminated by raising AssertionError.
By seeing AssertionError, programmer can analyze the code and can fix the
problem.
1) def squareIt(x):
2) return x**x
3) assert squareIt(2)==4,"The square of 2 should be 4"
4) assert squareIt(3)==9,"The square of 3 should be 9"
5) assert squareIt(4)==16,"The square of 4 should be 16"
6) print(squareIt(2))
7) print(squareIt(3))
8) print(squareIt(4))
9)
10) D:\Python_classes>py test.py
11) Traceback (most recent call last):
12) File "test.py", line 4, in <module>
13) assert squareIt(3)==9,"The square of 3 should be 9"
Output
4
9
16
PYTHON
LOGGING
Logging Levels:
Depending on type of information, logging data is divided according to the
following 6 levels in python
1) CRITICAL 50
Represents a very serious problem that needs high attention
2) ERROR 40
Represents a serious error
3) WARNING 30
Represents a warning message, some caution needed. It is alert to the programmer.
4) INFO 20
Represents a message with some important information
5) DEBUG 10
Represents a message with debugging information
6) NOTSET 0
Represents that level is not set
By default while executing Python program only WARNING and higher level
messages will be displayed.
The above line will create a file log.txt and we can store either WARNING level
or higher level messages to that file.
After creating log file, we can write messages to that file by using the following
methods
☕ logging.debug(message)
☕ logging.info(message)
☕ logging.warning(message)
☕ logging.error(message)
☕ logging.critical(message)
log.txt:
WARNING:root:warning
Information ERROR:root:error
Information
CRITICAL:root:critical
Information
Note: In the above program only WARNING and higher level messages will
be written to the log file. If we set level as DEBUG then all messages will be
written to the log file.
test.py
1) import logging
2) logging.basicConfig(filename='log.txt',level=logging.DEBUG)
3) print('Logging Demo')
4) logging.debug('Debug Information')
5) logging.info('info Information')
6) logging.warning('warning Information')
7) logging.error('error Information')
8) logging.critical('critical Information')
log.txt
DEBUG:root:Debug Information
INFO:root:info Information
WARNING:root:warning
Information ERROR:root:error
Information
KESA PAVAN KUMAR Cell No: 9848461833
217
CRITICAL:root:critical
Information
logging.basicConfig(filename='log786.txt',level=logging.W
ARNING) Meant for appending
logging.basicConfig(filename='log786.txt',level=logging.WARNING,fil
emode='a') Explicitly we are specifying appending.
logging.basicConfig(filename='log786.txt',level=logging.WARNING,file
mode='w') Meant for over writing of previous data.
Note:
logging.basicConfig(filename='log.txt',level=logging.DEBUG)
If we are not specifying level then the default level is WARNING(30)
If we are not specifying file name then the messages will be printed to the console.
test.py
1) import logging
2) logging.basicConfig()
3) print('Logging Demo')
4) logging.debug('Debug Information')
5) logging.info('info Information')
6) logging.warning('warning Information')
7) logging.error('error Information')
8) logging.critical('critical Information')
D:\durgaclasses>py
test.py Logging Demo
WARNING:root:warning
Information ERROR:root:error
Information
CRITICAL:root:critical
Information
Output
2018-06-15 11:50:08,325:WARNING:warning Information
2018-06-15 11:50:08,372:ERROR:error Information
2018-06-15 11:50:08,372:CRITICAL:critical Information
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(
message)s', datefmt='%d/%m/%Y %I:%M:%S %p')
datefmt='%d/%m/%Y %I:%M:%S %p' Case is important
Output
15/06/2018 12:04:31 PM:WARNING:warning
Information 15/06/2018 12:04:31 PM:ERROR:error
Information 15/06/2018 12:04:31 PM:CRITICAL:critical
Information
Note:
%I means 12 Hours time scale
%H means 24 Hours time scale
Eg: logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',
datefmt='%d/%m/%Y %H:%M:%S')
Output:
15/06/2018 12:06:28:WARNING:warning
Information 15/06/2018 12:06:28:ERROR:error
Information 15/06/2018 12:06:28:CRITICAL:critical
Information
https://docs.python.org/3/library/logging.html#logrecord-
attributes
https://docs.python.org/3/library/time.html#time.strftime
5)
6) x=int(input('Enter
y=int(input('Enter First
SecondNumber:'))
Number:'))
7) print('The Result:',x/y)
8)
9) except ZeroDivisionError as msg:
10) print('cannot divide with zero')
11) logging.exception(msg)
12
)13) except ValueError as msg:
14) print('Please provide int values only')
15) logging.exception(msg)
16
)17) logging.info('Request Processing Completed')
D:\durgaclasses>py
test.py Enter First
Number:10 Enter
Second Number:2 The
Result: 5.0
D:\durgaclasses>py
test.py Enter First
Number:20 Enter
Second Number:2 The
Result: 10.0
D:\durgaclasses>py
test.py Enter First
Number:10 Enter
Second Number:0
cannot divide with zero
D:\durgaclasses>py
test.py Enter First
Number:ten Please
provide int values only
mylog.txt
15/06/2018 12:30:51 PM:INFO:A new Request Came
15/06/2018 12:30:53 PM:INFO:Request Processing
Completed 15/06/2018 12:30:55 PM:INFO:A new
Request Came 15/06/2018 12:31:00 PM:INFO:Request
Processing Completed 15/06/2018 12:31:02 PM:INFO:A
new Request Came 15/06/2018 12:31:05
PM:ERROR:division by zero
Traceback (most recent call last):
File "test.py", line 7, in
<module> print('The
Result:',x/y)
ZeroDivisionError: division by zero
15/06/2018 12:31:05 PM:INFO:Request Processing
Completed 15/06/2018 12:31:06 PM:INFO:A new
Request Came
15/06/2018 12:31:10 PM:ERROR:invalid literal for int() with base 10:
'ten' Traceback (most recent call last):
File "test.py", line 5, in
<module> x=int(input('Enter
First Number:'))
ValueError: invalid literal for int() with base 10: 'ten'
15/06/2018 12:31:10 PM:INFO:Request Processing
Completed
Demo Application:
student.py:
1) import logging
2) logging.basicConfig(filename='student.log',level=logging.INFO)
3) logging.info('info message from student module')
test.py:
1) import logging
2) import student
3) logging.basicConfig(filename='test.log',level=logging.DEBUG)
4) logging.debug('debug message from test module')
student.log:
INFO: root:info message from student module
1) Once we set basic configuration then that configuration is final and we cannot
change.
2) It will always work for only one handler at a time, either console or file,
but not both simultaneously.
3) It is not possible to configure logger with different configurations at different levels.
4) We cannot specify multiple log files for multiple modules/classes/methods.
logger.warn('warn
message')
logger.error('error
logger.critical('critical message')
message')
Note: Bydefault logger will set to WARNING level. But we can set our own
level based on our requirement.
logger = logging.getLogger('demologger')
logger.setLevel(logging.INFO)
logger log level by default available to console and file handlers. If we are
not satisfied with logger level, then we can set log level explicitly at
console level and file levels.
consoleHandler =
logging.StreamHandler()
consoleHandler.setLevel(logging.WAR
NING)
fileHandler =
logging.FileHandler('abc.log',mode='a')
fileHandler.setLevel(logging.ERROR)
Note: console and file log levels should be supported by logger. i.e logger log
level should be lower than console and file levels. Otherwise only logger log
level will be considered.
Eg:
logger DEBUG console INFO Valid and INFO will be considered
logger INFO console DEBUG Invalid and only INFO will be
considered to the console.
3)
4) def testLog(self):
5) logger =
6) logging.getLogger('demologger')
logger.setLevel(logging.I
7) NFO)
8) consoleHandler =
9) logging.StreamHandler()
consoleHandler.setLevel(logging.
10 INFO)
)11) formatter = logging.Formatter('%(asctime)s -
%(name)s -
12 %(levelname)s: %(message)s',%I:%M:%S
datefmt='%m/%d/%Y
) %p')
13)
14 consoleHandler.setFormatter(form
)15 atter)
logger.addHandler(consoleHan
) dler)
KESA PAVAN KUMAR Cell No: 9848461833
224
D:\durgaclasses>py loggingdemo3.py
06/18/2018 12:14:15 PM - demologger - INFO: info
message 06/18/2018 12:14:15 PM - demologger -
WARNING: warn message 06/18/2018 12:14:15 PM -
demologger - ERROR: error message 06/18/2018 12:14:15
PM - demologger - CRITICAL: critical message
Note: If we want to use class name as logger name then we have to create
logger object as follows logger = logging.getLogger(LoggerDemoConsole.
name )
D:\durgaclasses>py loggingdemo3.py
06/18/2018 12:21:00 PM - LoggerDemoConsole - INFO: info
message 06/18/2018 12:21:00 PM - LoggerDemoConsole -
WARNING: warn message 06/18/2018 12:21:00 PM -
LoggerDemoConsole - ERROR: error message 06/18/2018 12:21:00
PM - LoggerDemoConsole - CRITICAL: critical message
15 logger.addHandler(fileHandler)
)
16)
17 logger.debug('debug
)18 message')
logger.info('info
)19 message')
logger.warn('warn
)
20 message')
logger.error('error
)21 message')
logger.critical('critical
)22 message')
)23) demo =
LoggerDemoConsole()
24) demo.testLog()
abc.log:
07/05/2018 08:58:04 AM - demologger - INFO: info
message 07/05/2018 08:58:04 AM - demologger -
WARNING: warn message 07/05/2018 08:58:04 AM -
demologger - ERROR: error message 07/05/2018 08:58:04
AM - demologger - CRITICAL: critical message
logging.config.fileConfig('logging.conf')
logger = logging.getLogger(LoggerDemoConf. name )
Note: The extension of the file need not be conf. We can use any extension
like txt or durga etc.
logging.conf
[loggers]
keys=root,LoggerDemo
Conf
[handlers]
keys=fileHandl
er
[formatters]
keys=simpleFormat
ter
[logger_root]
level=DEBUG
handlers=fileHand
ler
[logger_LoggerDemoC
onf] level=DEBUG
handlers=fileHandler
qualname=demoLogge
r
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=simpleFormat
ter args=('test.log', 'w')
[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s -
%(message)s datefmt=%m/%d/%Y %I:%M:%S %p
test.py
1) import logging
2) import logging.config
3) class
LoggerDemoConf():
4)
5) def testLog(self):
logging.config.fileConfig('logging.
6) conf')
logger = logging.getLogger(LoggerDemoConf.
7) name )
8) logger.debug('debug
9) message')
logger.info('info
10 message')
logger.warn('warn
)11 message')
logger.error('error
)12 message')
logger.critical('critical
)13 message')
)14) demo =
LoggerDemoConf()
15) demo.testLog()
test.log
06/18/2018 12:40:05 PM - LoggerDemoConf - DEBUG - debug
message 06/18/2018 12:40:05 PM - LoggerDemoConf - INFO -
info message 06/18/2018 12:40:05 PM - LoggerDemoConf -
WARNING - warn message 06/18/2018 12:40:05 PM -
LoggerDemoConf - ERROR - error message 06/18/2018
12:40:05 PM - LoggerDemoConf - CRITICAL - critical message
[handler_fileHandler]
class=FileHandler
level=INFO
formatter=simpleFormat
ter args=('test.log', 'a')
test.py
1) import logging
2) from customlogger import getCustomLogger
3) class LoggingDemo:
4) def m1(self):
5) logger=getCustomLogger(logging.DEBUG)
6) logger.debug('m1:debug message')
7) logger.info('m1:info message')
8) logger.warn('m1:warn message')
9) logger.error('m1:error message')
10 logger.critical('m1:critical message')
)
11 def m2(self):
)
12 logger=getCustomLogger(logging.WARNING)
)
abc.log:
06/19/2018 12:17:19 PM - m1 - DEBUG: m1:debug
message 06/19/2018 12:17:19 PM - m1 - INFO: m1:info
message 06/19/2018 12:17:19 PM - m1 - WARNING:
m1:warn message 06/19/2018 12:17:19 PM - m1 -
ERROR: m1:error message 06/19/2018 12:17:19 PM -
m1 - CRITICAL: m1:critical message 06/19/2018
12:17:19 PM - m2 - WARNING: m2:warn message
06/19/2018 12:17:19 PM - m2 - ERROR: m2:error
message 06/19/2018 12:17:19 PM - m2 - CRITICAL:
m2:critical message 06/19/2018 12:17:19 PM - m3 -
ERROR: m3:error message 06/19/2018 12:17:19 PM -
m3 - CRITICAL: m3:critical message
test.py:
#Same as previous
1) import logging
2) from customlogger import getCustomLogger
3) class LoggingDemo:
4) def m1(self):
5) logger=getCustomLogger(logging.DEBUG)
6) logger.debug('m1:debug message')
7) logger.info('m1:info message')
8) logger.warn('m1:warn message')
9) logger.error('m1:error message')
10 logger.critical('m1:critical message')
)
11 def m2(self):
)
12 logger=getCustomLogger(logging.WARNING)
)
13 logger.debug('m2:debug message')
)
14 logger.info('m2:info message')
)
15 logger.warn('m2:warn message')
)
16 logger.error('m2:error message')
)
17 logger.critical('m2:critical message')
)
18 def m3(self):
)
19 logger=getCustomLogger(logging.ERROR)
)
20 logger.debug('m3:debug message')
)
21 logger.info('m3:info message')
)
22 logger.warn('m3:warn message')
)
23 logger.error('m3:error message')
)
24 logger.critical('m3:critical message')
)
25)
KESA PAVAN KUMAR Cell No: 9848461833
230
26) l=LoggingDemo()
27) print('Logging Demo with Seperate Log File')
28) l.m1()
29) l.m2()
30) l.m3()
m1.log
06/19/2018 12:26:04 PM - m1 - DEBUG: m1:debug
message 06/19/2018 12:26:04 PM - m1 - INFO: m1:info
message 06/19/2018 12:26:04 PM - m1 - WARNING:
m1:warn message
m2.log
06/19/2018 12:26:04 PM - m2 - WARNING: m2:warn
message 06/19/2018 12:26:04 PM - m2 - ERROR:
m2:error message 06/19/2018 12:26:04 PM - m2 -
CRITICAL: m2:critical message
m3.log
06/19/2018 12:26:04 PM - m3 - ERROR: m3:error
message 06/19/2018 12:26:04 PM - m3 - CRITICAL:
m3:critical message
test.py
1) import logging
2) from customlogger import getCustomLogger
3) class Test:
4) def logtest(self):
5) logger=getCustomLogger(logging.DEBUG)
6) logger.debug('debug message')
7) logger.info('info message')
8) logger.warning('warning message')
9) logger.error('error message')
10) logger.critical('critical message')
11) t=Test()
12) t.logtest()
student.py:
1) import logging
2) from customlogger import getCustomLogger
3) def studentfunction():
4) logger=getCustomLogger(logging.ERROR)
5) logger.debug('debug message')
6) logger.info('info message')
7) logger.warning('warning message')
8) logger.error('error message')
9) logger.critical('critical message')
10) studentfunction()