KEMBAR78
Python Unit II.pptx
Types of Errors (exceptions)
Syntax Error or Compile Time Errors (addressed by programmer)
Logical Errors or Bugs (addressed by software testing team)
Run Time Errors – arises not due to mistake of programmer but due to:
Program receiving wrong input from the user,
Not being able to connect with database,
Experiencing network failure or facing server issues etc.
But as developer, we must have some mechanism to address these errors so that program
execution might not abruptly get stopped.
What is an Exception?
• An exception is an error that happens during execution of a program.
• When that error occurs, Python generate an exception that can be handled, which avoids
your program to crash.
• Whenever an exception occurs the program halts the execution and thus further code is not
executed.
• Thus exception is that error which python script is unable to tackle with.
Why use Exceptions?
• Exception in a code can also be handled.
• In case it is not handled, then the code is not executed further
and hence execution stops when exception occurs.
Hierarchy Of Exception
• ZeroDivisionError: Occurs when a number is divided by zero.
• NameError: It occurs when a name is not found. It may be local or
global.
• IndentationError: If incorrect indentation is given.
• IOError: It occurs when Input-Output operation fails.
• EOFError: It occurs when end of file is reached and yet
operations are being performed
Exception Handling
• Python provides two very important features to handle any
unexpected error in your Python programs and to add
debugging capabilities in them-
1. Exception Handling.
2. Assertions.
Standard Exceptions
EXCEPTION NAME DESCRIPTION
Exception Base class for all exceptions
ArithmeticError Base class for all errors that occur for
numeric calculation.
ZeroDivisonError Raised when division or modulo by zero takes place
for all numeric types.
EOFError Raised when there is no input from
either the raw_input() or input() function and the
end of file is reached.
ImportError Raised when an import statement fails.
KeyboardInterrupt Raised when the user interrupts program
execution, usually by pressing Ctrl + C.
NameError Raised when an identifier is not found in the local
or global namespace.
SyntaxError Raised when there is an error in Python syntax.
EXCEPTION NAME DESCRIPTION
IndentationError Raised when indentation is not specified properly.
TypeError Raised when an operation or function is attempted
that is invalid for the specified data type.
Standard Exceptions
Exception Handling
• The suspicious code can be handled by using the try block.
• Enclose the code which raises an exception inside the try block.
• The try block is followed except statement.
• It is then further followed by statements which are executed
during exception and in case if exception does not occur.
• Code in which exception may occur
Try
• Raise the exception
Raise
• Catch if excepetion occurs
Except
Declaring Multiple Exception
• Multiple Exceptions can be declared using the
same except statement:
try:
code
except (Exception1,Exception2,Exception3,..,ExceptionN):
execute this code in case any Exception of these occur.
else:
execute code in case no exception occurred.
Output
try:
malicious code
except (Exception1):
execute code
except (Exception2):
execute code
....
....
except (ExceptionN):
execute code
else:
In case of no exception, execute the else block code.
try:
a=10/0
print a
except (ArithmeticError):
print( "This statement is raising an exception" )
else:
print ("Welcome“)
def fun(a,b):
try:
c=(a+b)/(a-b)
except (ArithmeticError):
print("EXCEPTION OCCUR")
else:
print(c)
finally:
print("End of the world")
# Driver program to test above function
fun(2.0, 3.0) # -5.0 End of the world
fun(3.0, 3.0) # EXCEPTION OCCUR End of the world
Argument of an Exception
• An exception can have an argument, which is a value that
gives additional information about the problem.
• The contents of the argument vary by exception.
• You capture an exception's argument by supplying a variable
in the except clause as follows-
try:
You do your operations here
......................
except ExceptionType as Argument:
You can print value of Argument here...
Example 1
def fun(a):
try:
return int(a)
except ValueError as VE:
print("argument is not a number",VE)
fun(11) # 11
fun("string")
#argument is not a number invalid literal for int() with base 10: 'string’
Example 2
def fun(a,b):
c= a/b
return c
try:
fun("a","b")
except TypeError as VE:
print("Exception: ",VE)
Output:
Exception: unsupported operand type(s) for /: 'str' and 'str'
Raising Exception
• The raise statement allows the programmer to force a specific
exception to occur.
• The sole argument in raise indicates the exception to be raised.
• This must be either an exception instance or an exception class (a
class that derives from Exception).
# Program to depict Raising Exception
try:
raise NameError("Hey! are you sleeping!")
except NameError as NE:
print (NE)
# Raise Error
try:
raise Exception("How are you")
except Exception as E:
print(E)
How are you
Explanation:
i) Toraise an exception, raise statement is used.
It is followed by exception class name.
ii) Exception can be provided with a value(optional) that can be
given in the parenthesis. (here, Hey! are you sleeping!)
iii) To access the value "as" keyword is used.
“NE" is used as a reference variable which stores the value of
the exception.
User-defined Exceptions in Python
Creating User-defined Exception
• Programmers may name their own exceptions by creating a
new exception class.
• Exceptions need to be derived from the Exception class,
either directly or indirectly.
• Although not mandatory, most of the exceptions are named
as names that end in “Error”similar to naming of the standard
exceptions in python.
# A python program to create user-defined exception
# class MyError is derived from super class Exception
class MyError(Exception):
# Constructor or Initializer
def init (self, value):
self.value = value
# str is to print() the value
def str (self):
return(repr(self.value))
Introduction to PYTHON
(BCAC0009)
Module 2/ Lecture-4
Python Classes
Course: BCA II Year III Semester
By: Dr Aditya Saxena
Assistant Professor
Computer Engineering & Applications Department,
GLA University, Mathura
Python Objects and Classes
• Python is an object-oriented
programming language.
Unlike procedure-oriented
programming, where the main
emphasis is on functions,
object-oriented programming
stresses on objects.
• In OOP, Functions are called
Methods.
Python Objects and Classes
• An object is simply a
collection of Attributes
and Methods (functions)
that act on those data.
Python Objects and Classes
.
• In Python, Methods are
defined inside a class using
functions via def key-word
with one difference – the first
formal argument is ‘self’.
• In Python, Attributes are
initialized using a special
method _ _init_ _ via def key
work – again first argument
is ‘self’
• We can think of a class as a blueprint (prototype)
of a house which contains all the details about the
floors, doors, windows, etc. Based on these
descriptions we build the house.
• House is the object.
• As many houses can be made from a house's
blueprint, we can create many objects from a class.
• An object is also called an instance of a class and
the process of creating this object is
called instantiation.
Python Objects and Classes
In Python, objects are created
(instantiated) using syntax:
obj = Class_Name()
Any method defined inside a class
can be accessed only by the object
of that class using syntax:
obj.method_name(list of parameters)
Python Objects and Classes
Defining a Class in Python
Like function definitions begin with the def keyword in Python, class definitions
begin with a class keyword.
class Computer:
pass
Creating an Object in Python
To access different attributes and methods of class, we have to create an object of
that class
class Computer:
pass
com1 = Computer( )
No attribute or method in class – only
placeholder ‘pass’
Creation of an object 'com1' of class Computer
Special Method _ _init_ _ (also called constructor)
__init__ is a special method which is used to initialize
the values of attributes assigned to object
It does not need to be called unlike other defined methods
of a class.
It is called by itself as a new object is created of that class
This special method allows to impart attribute values to
objects at the same as they are created.
Assigning values to objects using instance variables
via. __init __ method
Class (static) variable, and Instance variables
Output
Static Variable
Instance Variables
Only Class (static) variable without Instance variables (No _ _init_ _)
Output
>>>class test:
def show(self):
print("SHOW METHOD")
>>>a=test()
>>>a.show() or test.show(a)
Output:
SHOW METHOD
In [1]: class test:
...: def show():
...: print("Message")
...:
In [2]: a=test()
In [3]: a.show() ## test.show(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-3-276f7237b96c> in <module>()
----> 1 a.show()
TypeError: show() takes 0 positional arguments but 1 was given
self in Python…???
class Point:
def __init__(self , x , y):
self.x = x
self.y = y
def distance(self):
return (self.x**2 + self.y**2) ** 0.5
p1 = Point(6,8)
print('The Distance between 6 and 8 is', p1.distance())
print('The Distance between 6 and 8 is', Point.distance(p1))
• In the above example, __init__() defines three parameters but we just
passed two (6 and 8).
• Similarly distance() requires one but zero arguments were passed.
• Why is Python not complaining about this argument number
mismatch?
• A peculiar thing about methods (in Python) is that the object itself is
passed on as the first argument to the corresponding
function……………IMPLICIT
• the method call p1.distance() is actually equivalent
to Point.distance(p1).
• Generally, when we call a method with some arguments, the
corresponding class function is called by placing the method's object
before the first argument.
• So, anything like obj.meth(args) becomes Class.meth(obj, args).
class student:
count=0 # class variable
def __init__(self, na, ma):
print("Constructor invoked")
self.name=na
self.marks=ma
student.count=student.count+1
def display(self):
print("NAME ",self.name)
print("MARKS ",self.marks)
def total(self):
print("Total students ",student.count)
a=student("abc",99) #Constructor invoked
b=student("xyz",88) #Constructor invoked
a.display() #NAME abc
#MARKS 99
b.display() #NAME xyz
#MARKS 88
student.count # 2
a.count # 2
b.Count # 2
• The variable count is a class variable whose value is shared among all the
instances of the class.
• This can be accessed as student.count from inside the class or outside the class.
• The first method __init__() is a special method, which is called initialization
method that Python calls when you create a new instance of this class.
• getattr(obj, name[, default]): to access the attribute of object.
• hasattr(obj,name): to check if an attribute exists or not.
• setattr(obj,name,value): to set an attribute. If attribute does not exist, then it
would be created.
• delattr(obj, name): to delete an attribute.
Built-In Class Attributes
• Every Python class keeps the following built-in attributes and they can be
accessed using dot operator like any other attribute −
__dict__: Dictionary containing the class's namespace.
__doc__: Class documentation string or none, if undefined.
__module__: Module name in which the class is defined. This attribute is
"__main__" in interactive mode.
__name__: Class name.
__bases__: A possibly empty tuple containing the base classes, in the order of
their occurrence in the base class list.
Garbage Collection in Python
• In any program, garbage is a collection of memory that is not being used by the
program or was being used at a given point but not anymore.
• As this memory space remains allocated to the program but is not being used by it
hence it is wasted memory which needs to be cleaned.
• Garbage collection is a process of freeing up memory that is not in use by a
specific program at a given instance of time.
• This process is helpful in memory management and minimizes the wastage of
memory. The memory freed by garbage collection can be used for storing other
important data or variables for the same program or by other programs.
Garbage Collection in Python
Python: Automatic Garbage collection
• Garbage collection in Python works automatically.
• When reference count of a variable/data/value reaches to 0, it is destroyed by
the garbage collection mechanism of python.
• Thus, python provides with good memory management and prevents the wastage
of memory.
class SomeObj:
def __init__(self):
print('The object is created.')
def __del__(self):
print('The object is destroyed.')
obj1 = SomeObj()
obj2 = obj1
obj3 = obj1
print("Set obj1 to None...")
obj1 = None
print("Set obj2 to None...")
obj2 = None
Output
The object is created.
Set obj1 to None...
Set obj2 to None...
Set obj3 to None...
The object is destroyed.
The following
example shows how
the python garbage
collection works:
when there is no
reference left to the
object then it is
automatically
destroyed by the
Garbage collector of
python
and __del__() method
is executed.
• You normally will not notice when the garbage collector destroys an orphaned
instance and reclaims its space.
• However, a class can implement the special method __del__(), called a destructor,
that is invoked when the instance is about to be destroyed.
• This method might be used to clean up any non-memory resources used by an
instance.
Python: Automatic Garbage collection
Inheritance in Python
• It refers to defining a new class with little or no modification to an existing class.
• The new class is called derived (or child) class and the one from which it inherits
is called the base (or parent) class.
class BaseClass:
Body of base class class
class DerivedClass(BaseClass):
Body of derived class
Single Inheritance
In single inheritance, a child class
inherits from a single-parent class.
Here is one child class and one
parent class.
# Base class
class Vehicle:
def Vehicle_info(self):
print('Inside Vehicle class')
# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')
# Create object of Car
car = Car()
# access Vehicle's info using car object
car.Vehicle_info()
car.car_info()
Output:
Inside Vehicle class
Inside Car class
Single Inheritance – example
Multilevel inheritance
In multilevel inheritance, a class
inherits from a child class or derived
class. Suppose three classes A, B, C.
A is the superclass, B is the child class
of A, C is the child class of B. In other
words, we can say a chain of
classes is called multilevel
inheritance.
Multilevel inheritance – example
Output
Inside Vehicle class
Inside Car class
Inside SportsCar class
Multiple Inheritance
In multiple inheritance,
one child class can inherit
from multiple parent
classes. So here is one
child class and multiple
parent classes.
Multiple Inheritance – example
Output
Python super() function
When a class inherits all properties and behavior from the parent
class – it is called inheritance. In such a case, the inherited class
is a subclass and the latter class is the parent class.
In child class, we can refer to parent class by using
the super() function.
The super function returns a temporary object of the parent class
that allows us to call a parent class method inside a child class
method.
Output
Jessa works at Google
Python super() function – example
Python super() function …contd.
Benefits of using the super() function.
1. We are not required to remember or specify the
parent class name to access its methods.
2. We can use the super() function in both single and multiple
inheritances.
3. The super() function support code reusability as there is no
need to write the entire function
Method Resolution Order in Python
• In Python, Method Resolution Order(MRO) is the order by which Python looks
for a method or attribute. First, the method or attribute is searched within a
class, and then it follows the order, we specified while inheriting.
• This order is also called the Linearization of a class, and a set of rules is called
MRO (Method Resolution Order). The MRO plays an essential role in multiple
inheritances as a single method may found in multiple parent classes.
In multiple inheritance, the following search order is followed.
1. First, it searches in the current parent class if not available, then searches in the
parents class specified while inheriting (that is left to right.)
2. We can get the MRO of a class. For this purpose, we can use either the mro
attribute or the .mro() method.
Method Resolution Order in Python
Output:
In class C
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
M.R.O in Python – example
In this example, we create three classes
named A, B and C.
Class B is inherited from A,
class C is inherits from B and A.
When we create an object of the C class and calling
the process() method, Python looks for
the process() method in the current class in
the C class itself.
Then search for parent classes, namely B and A,
because C class inherit from B and A. that is, C(B,
A) and always search in left to right manner.
ACCESSIBILITY
In Python, there are no keywords like public, protected or private. All attributes and methods are
public by default.
There is one way to define private in Python. The syntax to define private attribute and methods is:
_ _Attribute
_ _Methods_Name()
To make an attribute and a method private, we need to add two underscores, i.e. ‘_ _’ in front of the
attribute and the method’s name.
It helps in hiding these when accessed out of class.
ACCESSIBILITY – example
Output
Explanation In the above program, we have defined public and private attributes. The
private variable can be accessed from the method. We have created an instance of class
Person, i.e. P to access the attributes defined within the class.
However, it fails and shows an error when instance of class try to access private
attributes defined inside the class and are accessed from outside the class.
ACCESSIBILITY – example
Printing objects give us information about the objects we are working with.
In Python, this can be achieved by using __repr__ or __str__ methods.
__repr__ is used if we need a detailed information for debugging while __str__ is
used to print a string version for the users.
Printing Objects
Printing Objects
class Person:
def __init__(self, person_name, person_age):
self.name = person_name
self.age = person_age
def __str__(self):
return f'Person name is {self.name} and age is {self.age}'
def __repr__(self):
return f'Person(name={self.name}, age={self.age})'
p = Person('Pankaj', 34)
print(p.__str__())
print(p.__repr__()) Output
Polymorphism in Python
The word ‘Poly’ means many and ‘Morphs’ means forms. The process of
representing “one form in many forms” is called a polymorphism.
Types of Polymorphism in Python:
1. Duck Typing
2. Method overriding
3. Method Overloading
4. Operator Overloading
Method Overriding in Python
• Method overriding is an object-oriented programming feature that allows a
subclass to provide a different implementation of a method that is already
defined by its super class or by one of its super classes.
• The implementation in the subclass overrides the implementation of the super
class by providing a method with the same name, same parameters or signature,
and same return type as the method of the parent class.
class Dad:
def show(self):
print('I have Nokia 3310')
class Son(Dad):
def show(self):
print('I have iPhone')
ravi = Son()
ravi.show()
Method Overriding in Python-example
Method Overloading(does not work in PYTHON)
• Overloading is the ability to define the same method, with the same name but
with a different number of arguments and types.
• It's the ability of one function to perform different tasks, depending on the
number of parameters or the types of the parameters.
• If we need such a behavior, we can simulate it with default parameters.
Method Overloading(using trick)
Output
13
22
Python Operator Overloading
• You can change the meaning of an operator in Python depending upon the
operands used.
• This practice is known as operating overloading.
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
p1 = Point(2,3)
p2 = Point(-1,2)
print(p1) <__main__.Point object at 0x00000000031F8CC0>
p1 + p2
Traceback (most recent call last): ...
TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x,self.y)
>>> p1 = Point(2,3)
>>> print(p1)
(2,3)
>>> str(p1)
'(2,3)'
Overloading the + Operator in Python
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __str__(self):
return "({0},{1})".format(self.x,self.y) def __add__(self,
other):
x = self.x + other.x
y = self.y + other.y
return Point(x,y)
>>> p1 = Point(2,3)
>>> p2 = Point(-1,2)
>>> print(p1 + p2)
(1,5)
Operator Overloading Special Functions in Python
Operator Expression Internally
Addition p1 + p2 p1.__add__(p2)
Subtraction p1 - p2 p1.__sub__(p2)
Multiplication p1 * p2 p1.__mul__(p2)
Power p1 ** p2 p1.__pow__(p2)
Division p1 / p2 p1.__truediv__(p2)
Floor Division p1 // p2 p1.__floordiv__(p2)
Remainder (modulo) p1 % p2 p1.__mod__(p2)
Comparison Operator Overloading in Python
Operator Expression Internally
Less than p1 < p2 p1.__lt__(p2)
Less than or equal
to
p1 <= p2 p1.__le__(p2)
Equal to p1 == p2 p1.__eq__(p2)
Not equal to p1 != p2 p1.__ne__(p2)
Greater than p1 > p2 p1.__gt__(p2)
Greater than or
equal to
p1 >= p2 p1.__ge__(p2)
Overloading Comparison Operators in Python
class Point:
def __init__(self, x = 0, y = 0):
self.x = x
self.y = y
def __lt__(self,other):
smag = (self.x ** 2) + (self.y ** 2) omag =
(other.x ** 2) + (other.y ** 2)
return smag < omag
>>> Point(1,1) < Point(-2,-3)
True
>>> Point(1,1) < Point(0.5,-0.2)
False
>>> Point(1,1) < Point(1,1)
False
Composition
In this concept, we will describe a class that references to one or more
objects of other classes as an Instance variable. Here, by using the class
name or by creating the object we can access the members of one class
inside another class. It means that a class Composite(B) can contain an
object of another class Component(A). This type of relationship is known
as Has-A Relation.
class A :
# variables, methods of class A
... ...
class B :
# by using "obj" we can access member's of class A.
obj = A() # variables, methods of class B
... ...
class demo:
def __init__(self,a,b,c):
self.obj=test(a,b)
self.c=c
def show1(self):
self.obj.show()
print(self.c)
d=demo(1,2,3)
d.show1()
class test:
def __init__(self,x,y):
self.x=x
self.y=y
def show(self):
print(self.x,self.y)
Output:
1 2
3
What is regular expression?
• The term "regular expression", sometimes also
called regex or regexp, is originated in theoretical
computer science.
• In theoretical computer science they are used to
define a language-family with certain
characteristics.
• Regular Expressions are used in programming
languages to filter texts or strings.
• It's possible to check - if a text or a string
matches a regular expression.
• The syntax of regular expressions is the same
for all programming and script languages, e.g.
Python, Perl, Java, AWK and even X#.
• In Python, we have module “re” that
helps with regular expressions.
• So you need to import library re before you
can use regular expressions in Python.
import re
• Regular expressions use two types of
characters:
a)Meta characters: these characters have a
special meaning, similar to * in wild-card.
Ex. . ^ $ * + ? { } [ ]  | ( )
b) Literals: have usual meaning
Ex. A, B, a,b,text,1,2…….
The most common uses of regular expressions:
• Search a string (search and match) re.match(), re.search()
• Finding a string (findall) re.findall()
• Break string into a sub strings (split) re.split()
• Replace part of a string (sub) re.sub()
The following special-character sequences are recognized in regular expression
patterns:
Character Description
Matches any character except newline.
Matches the start of a string.
Matches the end of a string.
.
^
$
The following special-character sequences are recognized in regular expression
patterns:
Character Description
Matches zero or more repetitions of the preceding (before)
expression, matching as many repetitions as possible.
Matches one or more repetitions of the preceding expression,
matching as many repetitions as possible.
*
+
Difference between * and +
raw
* Matches zero or more repetitions of the preceding (before)
expression, matching as many repetitions as possible.
+ Matches one or more repetitions of the preceding expression,
matching as many repetitions as possible.
*? Matches zero or more repetitions of the preceding expression, matching as few
repetitions as possible.
? Matches zero or one repetition of the preceding expression.
neither 0 nor 1 repetitions so no match
{m} Matches exactly m repetitions of the preceding expression.
{m, n} Matches from m to n repetitions of the preceding expression, matching as many
repetitions as possible. If m is omitted, it defaults to 0. If n is omitted, it defaults to infinity.
Exactly 2 repetitions of 'l'
From 0 to 2 repetitions of 'l'
Expression String Matched ?
[abc]
a 1 match
ac 2 matches
Hey Jude No match
abc de ca 5 matches
[ ] - Square brackets
Square brackets specifies a set of characters we wish to match.
Here, [abc] will match if the string we are trying to match contains any of the a, b or c.
We can also specify a range of characters using - inside square brackets.
•[a-e] is the same as [abcde].
•[1-4] is the same as [1234].
•[0-39] is the same as [01239].
We can complement (invert) the character set by using caret ^ symbol at the start of a square-
bracket.
•[^abc] means any character except a or b or c.
•[^0-9] means any non-digit character.
Expression String Matched?
a|b
cde No match
ade 1 match (match at ade)
acdbea 3 matches (at acdbea)
| - Alternation
Vertical bar | is used for alternation (or operator). Here, a | b match any string that
contains either a or b
Here, a | b match any string that contains either a or b
Expression String Matched?
(a | b | c)xz
ab xz No match
abxz 1 match (match at abxz)
axz cabxz 2 matches (at axzbc cabxz)
() - Group
Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that
matches either axz or bxz or cxz.
EXAMPLES
• a="saghjz g hj sd hhjg“
• re.findall("hj", a)
• ['hj', 'hj', 'hj']
• re.findall("hj.", a)
• ['hjz', 'hj ', 'hjg']
• re.findall("hj?",a)
• ['hj', 'hj', 'h', 'hj']
? Matches zero or one repetition of the preceding expression.
*? Matches zero or more repetitions of the preceding expression, matching as few repetitions as possible.
• a="saghjz g hj sd hhjg“
• re.findall("^hj",a)
• []
• b="hj sa ghjz g hj sd hhjg"
• re.findall("^hj",b)
• ['hj']
• a="zz bbbb ccc“
• re.findall("n*",a)
• ['', '', '', '', '', '', '', '', '', '', '', '']
• b="zzbbbccccc“
• re.findall("b*",b)
• ['', '', 'bbb', '', '', '', '', '', '']
• In [1]: import re
• In [2]: a="a sgg ss“
• In [3]: b=re.findall("[sa]",a)
• In [4]: print(b)
• ['a', 's', 's', 's']
• In [5]: b=re.findall("[^sa]",a)
• In [6]: print(b)
• [' ', 'g', 'g', ' ']
• In [8]: a="a sgg ss“
• In [9]: b=re.findall("s{2}",a)
• In [10]: print(b)
• ['ss']
• In [11]: b=re.findall("s{3}",a)
• In [12]: print(b)
• []
• In [13]: b=re.findall("s{1}",a)
• In [14]: print(b)
• ['s', 's', 's']
• In [15]: b=re.findall("s{0}",a)
• In [16]: print(b)
• ['', '', '', '', '', '', '', '', '']
• In [17]: b=re.findall("s{1,3}",a)
• In [18]: print(b)
• ['s', 'ss']
• In [24]: a="BCA GLA UNIVERSITY“
• In [25]: b=re.findall("BCA|GLA",a)
• In [26]: print(b)
• ['BCA', 'GLA']
• In [27]: a="BCA GLA UNIVERSITY MBA CEA BBA“
• In [28]: b=re.findall("(.A)",a)
• In [29]: print(b)
• ['CA', 'LA', 'BA', 'EA', 'BA']
• In [39]: a="BCA 2001 GLA“
• In [40]: b=re.findall("([0-9])",a)
• In [41]: print(b)
• ['2', '0', '0', '1']
• (.*) means any character (.) repeated zero or more times (*).
• The fact that it is enclosed in parentheses tells Python to save away
the part of the string matched by that part of the pattern as a
group—a matched substring available after the match.
In [53]: b=re.findall("d{1,2}",a)
In [54]: print(b)
['8', '20', '17']
In [55]: b=re.findall("d{1,5}",a)
In [56]: print(b)
['8', '2017']
In [57]: b=re.findall("d{5,6}",a)
In [58]: print(b) []
In [48]: a="8 NOV 2017“
In [49]: b=re.findall("d{1,3}",a)
In [50]: print(b)
['8', '201', '7']
In [51]: b=re.findall("d{1,4}",a)
In [52]: print(b)
['8', '2017']
Predefined Character Classes
• You might have realized that it can be quite
cumbersome to construct certain character
classes.
• A good example is the character class, which
describes a valid word character.
• These are all lower case and uppercase
characters plus all the digits and the
underscore, corresponding to the following
regular expression: r"[a-zA-Z0-9_]“
• The special sequences consist of "" and a
character from the following list:
• d Matches any decimal digit; equivalent to the
set [0-9].
• D The complement of d. It matches any non-
digit character; equivalent to the set [^0-9].
• s Matches any whitespace character;
equivalent to [ tnrfv].
• S The complement of s. It matches any non-
whitespace character; equiv. to [^ tnrfv].
• w Matches any alphanumeric character;
equivalent to [a-zA-Z0-9_].
• WMatches the complement of w.
• b Matches the empty string, but only at
the start or end of a word.
• B Matches the empty string, but not at the
start or end of a word.
•  Matches a literal backslash.
Compilation flags
• Compilation flags let you modify some aspects of how
regular expressions work.
• Flags are available in the re module under two names, a
long name such as IGNORECASE and a short, one-letter
form such as I.
Flag
• ASCII, A
Meaning
Makes several escapes like w, b, s and d
match only on ASCII characters with the
property.
Make, match any character, including newlines
respective
• DOTALL, S
• IGNORECASE, I
• MULTILINE, M
Do case-insensitive matches
Multi-line matching, affecting ^ and $
• In [54]: a="GLA University“
• In [55]: b=re.search("gla",a)
• In [56]: print(b)
• None
• In [57]: b=re.search("gla",a, re.I)
• In [58]: print(b.group())
• GLA
Any Character .
• All three letter words, which end with "at".
• The syntax of regular expressions supplies a metacharacter ".",
which is used like a placeholder for "any character".
• The regular expression of our example can be written like this:
r" .at "
• This RE matches three letter words, isolated by blanks, which end in
"at".
• Now we get words like "rat", "cat", "bat", "eat", "sat" and many
others.
• But what, if the text contains "words" like "@at" or "3at"? These
words match as well and this means that we have created over
matching again. We will learn a solution in the following section:
Character Classes
• Square brackets, "[" and "]", are used to include a
character class.
• [xyz] means e.g. either an "x", an "y" or a "z".
Let's look at a more practical example:r"M[ae][iy]er"
• This is a regular expression, which matches a
surname which is quite common in German.
• A name with the same pronunciation and four
different spellings: Maier, Mayer, Meier, Meyer
• The only other special character inside square
brackets (character class choice) is the caret "^".
• If it is used directly after an opening square bracket, it
negates the choice. [^0-9] denotes the choice "any
character but a digit".
• The position of the caret within the square brackets is
crucial.
• If it is not positioned as the first character following the
opening square bracket, it has no special meaning.
• [^abc] means anything but an "a", "b" or "c"
• [a^bc] means an "a", "b", "c" or a "^
WHY WE NEED RE ?
>>>phone_number = "Home: (555) 265-2901“
>>>start = phone_number.find('(')
>>> print(start)
6
>>> start = start + 1
>>> end = start + 3
>>> area_code = phone_number[start : end]
>>> print(area_code)
555
Extract Area Code
>>> phone_number = "Cell (mobile): (555)-918-8271“
## Previous code can’t work here
What are various methods of Regular Expressions?
The ‘re’ package provides multiple methods to
perform queries on an input string.
• re.match()
• re.search()
• re.findall()
• re.split()
• re.sub()
• re.compile()
re.match(pattern, string):
• The match() method will only find matches if they occur at the start of the
string being searched.
• For example, calling match() on the string ‘AV Analytics AV’ and looking for a
pattern ‘AV’ will match.
• However, if we look for only Analytics, the pattern will not match.
Code
>>> import re
>>> result = re.match(r'AV', 'AV Analytics Vidhya AV')
>>> result
<re.Match object; span=(0, 2), match='AV'>
>>> result.start()
0
>>> result.end()
2
>>> result.group(0)
'AV'
The match Function
• This function attempts to match RE pattern to
string with optional flags.
• re.match(pattern, string, flags=0)
• pattern This is the regular expression to be
matched.
• string This is the string, which would be searched
to match the pattern at the beginning of string.
• flags You can specify different flags using bitwise
OR (|).
• The re.match function returns a match object on
success, None on failure.
• We use group(num) or groups() function of match
object to get matched expression.
Match Object Methods Description
• group(num=0)
• groups()
This method returns entire match (or specific subgroup num)
This method returns all matching subgroups in a tuple
(empty if there weren't any)
• So for example, calling match() on the string ‘dog
cat dog’, looking for the pattern ‘dog’ will match:
>>> re.match(r'dog', 'dog cat dog')
<re.Match object; span=(0, 3), match='dog'>
>>> match = re.match(r'dog', 'dog cat dog')
>>> match.group(0)
'dog'
• There are methods like start() and end() to know the
start and end position of matching pattern in the
string.
• result = re.match('AV', 'AV Analytics Vidhya AV')
• print (result.start())
• print (result.end())
Output: 0 2
Above you can see that start and end position of
matching pattern ‘AV’ in the string and sometime it
helps a lot while performing manipulation with the
string.
• But, if we call match() on the same string,
looking for the pattern ‘cat’, we won’t:
>>> a=re.match(r'cat', 'dog cat dog')
>>>print(a)
none
re.search(pattern, string)
• It is similar to match() but it doesn’t restrict us to find
matches at the beginning of the string only.
• Unlike previous method, here searching for pattern
‘Analytics’ will return a match.
Code
• result = re.search(r'Analytics', 'AV Analytics Vidhya AV')
• print (result.group(0))
Output: Analytics
Here you can see that, search() method is able to find a
pattern from any position of the string but it only
returns the first occurrence of the search pattern.
a = 'cats are smarter than dogs'
b=re.search(r"(w*) are (w*) (.*)",a)
b.group() ## or b.group(0)
'cats are smarter than dogs‘
b.group(1)
'cats'
b.group(2)
'smarter'
b.group(3)
'than dogs'
re.search(pattern, string)
re.findall (pattern, string):
• It helps to get a list of all matching patterns.
• It has no constraints of searching from start or end.
• If we will use method findall to search ‘AV’ in given
string it will return both occurrence of AV.
• re.findall() it can work like re.search() and re.match()
both.
• result = re.findall('AV', 'AV Analytics Vidhya AV')
• print (result)
Output: ['AV', 'AV']
re.split(pattern, string, [maxsplit=0]):
• This methods helps to split string by the
occurrences of given pattern.
Code
• result=re.split(‘m','Animal')
• result
Output: ['Ani', ‘al']
• Method split() has another argument
“maxsplit“. It has default value of zero.
• In this case it does the maximum splits that
can be done, but if we give value to maxsplit,
it will split the string.
• result=re.split('i','Analytics Vidhya')
• print (result)
Output: ['Analyt', 'cs V', 'dhya']
#It has performed all the splits that can be done by
pattern "i".
Code
• result=re.split(r'i','Analytics Vidhya', maxsplit=1)
• result
Output: ['Analyt', 'cs Vidhya']
re.sub(pattern, repl, string)
• It helps to search a pattern and replace with a
new sub string.
• If the pattern is not found, string is returned
unchanged.
Code
• result=re.sub('India','the World','AV is largest
Analytics community of India')
• result
Output: 'AV is largest Analytics community of the
World'
re.compile(pattern, repl, string)
• We can combine a regular expression pattern into
pattern objects, which can be used for pattern
matching.
• It also helps to search a pattern again without rewriting
it.
Code
• pattern=re.compile('AV')
• result=pattern.findall('AV Analytics Vidhya AV')
• print (result)
• ['AV', 'AV']
• result2=pattern.findall('AV is largest analytics community of India')
• print (result2)
• ['AV']
Some Examples of Regular Expressions
Problem 1: Return the first word of a given string
Solution-1 Extract each character (using “w“)
Code
import re
result=re.findall(r'.','AV is largest Analytics community of
India')
print (result)
Output: ['A', 'V', ' ', 'i', 's', ' ', 'l', 'a', 'r', 'g', 'e', 's', 't', ' ', 'A', 'n',
'a', 'l', 'y', 't', 'i', 'c', 's', ' ', 'c',
• Above, space is also extracted, now to avoid it use “w” instead of “.“.
• Code
• result=re.findall(r'w','AV is largest Analytics community of India')
• print( result)
Output: ['A', 'V', 'i', 's', 'l', 'a', 'r', 'g', 'e', 's', 't', 'A',
'n', 'a', 'l', 'y', 't', 'i', 'c', 's', 'c', 'o', 'm', 'm', 'u',
'n', 'i', 't', 'y', 'o', 'f', 'I', 'n', 'd', 'i', 'a']
• Solution-2 Extract each word (using “*” or “+“)
Code
• result=re.findall(r'w*','AV is largest Analytics
community of India')
• print(result)
Output: ['AV', '', 'is', '', 'largest', '', 'Analytics', '',
'community', '', 'of', '', 'India', '']
it is returning space as a word because “*”
returns zero or more matches of pattern to its
• Now to remove spaces we will go with “+“.
Code
• result=re.findall(r'w+','AV is largest Analytics
community of India')
• print (result)
Output: ['AV', 'is', 'largest', 'Analytics',
'community', 'of', 'India']
Problem 2: Return the first two character of each word
• Solution-1 Extract consecutive two characters of each
word, excluding spaces (using “w“)
• Code
• result=re.findall(r'ww','AV is largest Analytics
community of India')
• print (result)
Output: ['AV', 'is', 'la', 'rg', 'es', 'An', 'al', 'yt', 'ic',
'co', 'mm', 'un', 'it', 'of', 'In', 'di']
• Solution-2 Extract consecutive two characters those available at start of word
boundary (using “b“)
• Code
• result=re.findall(r'bw.','AV is largest Analytics community of India')
• print (result)
Output: ['AV', 'is', 'la', 'An', 'co', 'of', 'In']
Problem 3: Return the domain type of given email-ids
• Solution-1 Extract all characters after “@”
• Code
• result=re.findall(r'@w+',' abc.test@gmail.com , xyz@test.in,
test.first@analyticsvidhya.com , first.test@rest.biz ')
• print (result)
Output: ['@gmail', '@test', '@analyticsvidhya', '@rest']
• Above, you can see that “.com”, “.in” part is
not extracted.
• To add it, we will go with below code.
• result=re.findall(r'@w+.w+',
'abc.test@gmail.com, xyz@test.in,
test.first@analyticsvidhya.com,
first.test@rest.biz')
• print (result)
Output: ['@gmail.com', '@test.in',
• Solution – 2 Extract only domain name using
“( )”
Code
• result=re.findall(r'@w+.(w+)','abc.test@gm
ail.com, xyz@test.in,
test.first@analyticsvidhya.com,
first.test@rest.biz')
• print (result)
Output: ['com', 'in', 'com', 'biz']
Problem 4: Return date from given string
• Here we will use “d” to extract digit.
• Solution:
Code
• result=re.findall(r'd{2}-d{2}-d{4}','Amit 34- 3456 12-05-2007,
XYZ 56-4532 11-11-2011, ABC 67-8945 12-01-2009')
• print (result)
Output: ['12-05-2007', '11-11-2011', '12-01- 2009']
• result=re.findall(r'd{2}-d{2}-(d{4})','Amit 34- 3456 12-05-2007,
XYZ 56-4532 11-11-2011, ABC 67-8945 12-01-2009')
• print (result)
Output: ['2007', '2011', '2009']
Problem 5: Return all words of a string
Solution-1
Code
>>> result=re.findall(r'w+','AV is largest Analytics community of India')
>>> print( result)
['AV', 'is', 'largest', 'Analytics', 'community', 'of', 'India']
• Solution-2 Return words starts with vowel (using [])
Code
• result=re.findall(r'[aeiouAEIOU]w+','AV is largest Analytics community of
India')
• print (result)
Output: ['AV', 'is', 'argest', 'Analytics', 'ommunity', 'of', 'India']
Above you can see that it has returned “argest” and “ommunity”
from the mid of word
• Solution- 3
• Code
• result=re.findall(r'b[aeiouAEIOU]w+','AV is
largest Analytics community of India')
• print (result)
Output: ['AV', 'is', 'Analytics', 'of', 'India']
• In similar ways, we can extract words those starts with consonant using
“^” within square bracket.
• Code
• result=re.findall(r'b[^aeiouAEIOU]w+','AV is largest Analytics community
of India')
• print (result)
Output: [' is', ' largest', ' Analytics', ' community', ' of', ' India']
• Above you can see that it has returned words starting with space.
• To drop it from output, include space in square bracket[].
Code
• result=re.findall(r'b[^aeiouAEIOU ]w+','AV is largest Analytics community
of India')
• print (result)
Output: ['largest', 'community']
Problem 6: Validate a phone number (phone number must be
of 10 digits and starts with 8 or 9)
• We have a list phone numbers in list “li” and
here we will validate phone numbers using
regular
Solution
• import re
• li=['9999999999','999999-999','99999x9999']
• for val in li:
if re.match(r'[8-9]{1}[0-9]{9}',val) and len(val) == 10:
print ('yes' )
else:
print (‘No' )
Problem 7: Split a string with multiple delimiters
• Solution
• import re
• line = 'asdf fjdk;afed,fjek,asdf,foo'
# String has multiple delimiters (";",","," ").
• result= re.split(r'[;,s]', line)
• print (result)
Output: ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
• The groups contain the matched values.
• In particular: # match.group(0) always returns the fully matched string
• # match.group(1) match.group(2), ... will return the capture # groups in
order from left to right in the input string
• # match.group() is equivalent to match.group(0)
Introduction to Python
BCA3005
Module-III
Dr Aditya Saxena
Python Database Connectivity
• In this topic you will learn how to
– use the SQLite database management system with
Python
– use SQLite, SQL queries, RDBMS
Content
• Python Database Connection
–Introduction
–Connections
–Executing queries
–Transactions
–Handling error
Python Database
• Data is everywhere and software applications use that.
• Data is either in memory, files or databases.
• Python has bindings for many database systems
including MySQL, Postregsql, Oracle, Microsoft SQL Server
and Maria DB.
• One of these database management systems (DBMS) is called
SQLite.
• SQLite was created in the year 2000 by D. Richard Hipp.
Introduction
• The Python standard for database interfaces is the Python DB-
API, which is used by Python's database interfaces.
• The DB-API has been defined as a common interface, which
can be used to access relational databases.
• The code in Python for communicating with a database should
be the same, regardless of the database and the database
module used.
• The DB API provides a minimal standard for working
with databases using Python structures and syntax
wherever possible.
• This API includes the following:
– Importing the API module.
– Acquiring a connection with the database.
– Issuing SQL statements and stored procedures.
– Closing the connection
Connect
Create
Interact
Commit Rollback
Close
Establish a connection to your
chosen database backend
Create a cursor to communicate
through the connection to your data
Using the cursor manipulate your
Data using SQL
Tell your connection to apply
all of your SQL-manipulation
to your data and make them
permanent
Tell your connection to abort your SQL
manipulation, returning your data to
the state it was in before the
connection started
Destroy the connection to the
database backend
SQLite
• SQLite is a simple relational database system, which saves its
data in regular data files or even in the internal memory of
the computer, i.e. the RAM.
• It was developed for embedded applications, like Mozilla-
Firefox (Bookmarks), Symbian and Android OS.
• SQLITE is "quite" fast, even though it uses a simple file.
• It can be used for large databases as well.
• Python has an in-built support for SQLite.
Process of database connectivity in python
• If you want to use SQLite, you have to import the module
sqlite3.
• To use a database, you have to create first a Connection
object.
• The connection object will represent the database.
Establishing connection to a database
• It has to be a SQLite database file of course!
>>> import sqlite3
>>> connection =sqlite3.connect("company.db")
• We have now created a database with the name "company".
• If a file with this name exists, it will be opened.
• It's like having sent the command
• "CREATE DATABASE company;" to a SQL server.
• If you call "sqlite3.connect('company.db')" again, it will open the previously created
database.
Creating a table using SQL
• The SQL syntax for creating a table "employee" in the database "company"
looks like this:
CREATE TABLE employee (
staff_number INT NOT NULL,
fname
lname
gender
joining
birth_date
VARCHAR(20),
VARCHAR(30),
CHAR(1),
DATE,
DATE,
PRIMARY KEY (staff_number) );
Cursor object
• To be capable to send a command to "SQL", or SQLite, we
need a cursor object.
• Usually, a cursor in SQL and databases is a control structure to traverse
over the records in a database.
• So it's used for the fetching of the results.
• In SQLite (and other Python DB interfaces) it is more generally used.
• It's used for performing all SQL commands.
• We get the cursor object by calling the cursor() method of
connection.
• An arbitrary number of cursors can be created.
• The cursor is used to traverse the records from the result set.
• We can define a SQL command with a triple quoted string in
Python.
Syntax of cursor object
cursor = connection.cursor()
cursor.execute(sql_command)
• Where sql_command is a triple quoted string having sql
command.
sql_command = """
CREATE TABLE employee (
staff_number INTEGER PRIMARY KEY,
fname
lname
gender
joining
VARCHAR(20),
VARCHAR(30),
CHAR(1),
DATE,
birth_date DATE);"""
Manipulation of data
• Now we have a database with a table but no data included.
• To populate the table we will have to send the "INSERT"
command to SQLite.
• We will use again the execute method.
• To run the program you will either have to remove the file
company.db or uncomment the "DROP TABLE" line in the SQL
command:
Example:
import sqlite3
connection = sqlite3.connect("employee.db")
cursor = connection.cursor()
cursor.execute("DROP TABLE IF EXISTS manger")
sql = """CREATE TABLE manager(
ID INT PRIMARY KEY,
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SALARY INT)""“
cursor.execute(sql)
connection.close()
INSERT Operation
import sqlite3
db=sqlite3.connect("employee.db")
cursor=db.cursor()
sql="""INSERT INTO employee(ID, FIRST_NAME, LAST_NAME, AGE, SALARY)
VALUES(1007,'JAMES','BOND',50,1000),
(1009,'AGENT','VINOD',55,500 ),
(1010,'AGENT','TIGER',60,700)""“
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
finally:
db.close()
READ Operation
• You can use either fetchone() method to fetch a single record
• fetchall() method to fetch multiple values from a database table.
• fetchone(): It fetches the next row of a query result set. A result set is an object that is returned
when a cursor object is used to query a table.
• fetchall(): It fetches all the rows in a result set. If some rows have already been extracted from
the result set, then it retrieves the remaining rows from the result set.
• rowcount: This is a read-only attribute and returns the number of rows that were affected by
an execute() method.
import sqlite3
db=sqlite3.connect("employee.db")
cursor=db.cursor()
sql="SELECT *FROM employee"
try:
cursor.execute(sql)
results=cursor.fetchall()
for rows in results:
print(rows)
except:
print("some error")
db.close()
• (1007, 'JAMES', 'BOND', 50, 1000)
• (1009, 'AGENT', 'VINOD', 55, 500)
• (1010, 'AGENT', 'TIGER', 60, 700)
Update Operation
• UPDATE Operation on any database means to update one or
more records, which are already available in the database.
• The following procedure updates all the records.
• Here, we increase the SALARY of all the managers by 5000 if
their salary is less than 1000.
• import sqlite3
• db=sqlite3.connect("employee.db")
• cursor=db.cursor()
• sql = """UPDATE manager SET SALARY = SALARY + 5000
• WHERE SALARY < 1000"""
• try:
• cursor.execute(sql)
• db.commit()
• except:
• db.rollback()
• db.close()
DELETE Operation
• import sqlite3
• db=sqlite3.connect("employee.db")
• cursor=db.cursor()
• sql = """DELETE FROM manager
• WHERE SALARY < 5000"""
• try:
• cursor.execute(sql)
• db.commit()
• except:
• db.rollback()
• db.close()
Performing Transactions
• Transactions are a mechanism that ensure data consistency.
Transactions have the following four properties-
• Atomicity: Either a transaction completes or nothing happens
at all.
• Consistency: A transaction must start in a consistent state and
leave the system in a consistent state.
• Isolation: Intermediate results of a transaction are not visible
outside the current transaction.
• Durability: Once a transaction was committed, the effects are
persistent, even after a system failure.
• The Python DB API provides two methods to either commit
or rollback a transaction.
• # Prepare SQL query to DELETE required records
• sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
• try:
• # Execute the SQL command
• cursor.execute(sql)
• # Commit your changes in the database
• db.commit()
• except:
• # Rollback in case there is any error
• db.rollback()
COMMIT Operation
• Commit is an operation, which gives a green signal to the database to
finalize the changes, and after this operation, no change can be
reverted back.
• Here is a simple example to call the commit method.
• db.commit()
ROLLBACK Operation
• If you are not satisfied with one or more of the changes and
you want to revert back those changes completely, then use
the rollback() method.
• Here is a simple example to call the rollback() method.
• db.rollback()
Disconnecting Database
• To disconnect the Database connection, use the close()
method.
• db.close()
Points to remember
• The SQLite database system is included within Python as the sqlite3
standard library.
• The connect() method establishes a connection to your database file.
• The cursor() method lets you communicate with your database via an
existing connection.
• The execute() method lets you send an SQL query to your database via an
existing cursor.
• The commit() method makes changes to your database permanent.
• The rollback() method cancels any pending changes to your data.
• The close() method closes an existing connection to your database.
Handling Errors
• There are many sources of errors.
• A few examples are a syntax error in an executed SQL
statement, a connection failure, or calling the fetch method
for an already cancelled or finished statement handle.
• The DB API defines a number of errors that must exist in each
database module.
Multithreading in Python
• Multiprocessing(can be more than one processor)
• Just like multiprocessing, multithreading is a way of achieving
multitasking. In multithreading, the concept of threads is used.
Multiprocessing
Thread
• In computing, a process is an instance of a computer program that is being
executed.
• Any process has 3 basic components:
1. An executable program.
2. The associated data needed by the program (variables, work space, buffers, etc.)
3. The execution context of the program (State of process)
• A thread is
• an entity within a process that can be scheduled for execution.
• smallest unit of processing that can be performed in an OS (Operating
System).
• a sequence of such instructions within a program that can be executed
independently of other code.
• a subset of a process!
• Multithreading is defined as the ability of a processor to execute multiple threads
concurrently.
• In a simple, single-core CPU, it is achieved using frequent switching between
threads.
• This is termed as context switching.
• In context switching, the state of a thread is saved and state of another thread is
loaded whenever any interrupt (due to I/O or manually set) takes place.
• Context switching takes place so frequently that all the threads appear to be
running parallel (this is termed as multitasking).
Multithreading
The methods provided by the Thread class are as follows:
1. start(): The start() method starts a thread by calling the run method.
2. run(): The run() method is the entry point for a thread.
3. join([time]): The join() waits for threads to terminate.
4. isAlive(): The isAlive() method checks whether a thread is still executing.
5. getName(): The getName() method returns the name of a thread.
6. setName(): The setName() method sets the name of a thread.
• To import the threading module, we do:
import threading
• To create a new thread, we create an object of Thread class.
• It takes following arguments:
• target: the function to be executed by thread
• args: the arguments to be passed to the target function
• In above example, we created 2 threads with different target functions:
t1 = Thread(target = print_square, args=(10,))
t2 = Thread(target = print_cube, args=(10,))
• To start a thread, we use start method of Thread class.
t1.start()
t2.start()
• Once the threads start, the current program (you can think of it like a main
thread) also keeps on executing.
• In order to stop execution of current program until a thread is complete, we
use join method.
t1.join()
t2.join()
• As a result, the current program will first wait for the completion of t1 and
then t2.
• Once, they are finished, the remaining statements of current program are
executed.
Output
Synchronization between threads
• Thread synchronization is defined as a mechanism which ensures that
• two or more concurrent threads do not simultaneously execute some particular program
segment known as critical section.
• Critical section refers to the parts of the program where the shared resource is
accessed.
• For example, in the diagram below, 3 threads try to access shared resource or
critical section at the same time.
• Concurrent accesses to shared resource can lead to race condition.
• A race condition occurs when two or more threads can access shared data and
they try to change it at the same time.
• As a result, the values of variables may be unpredictable and vary depending on
the timings of context switches of the processes.
Output
import threading
# global variable x
x = 0
def increment():
global x
x += 1
def thread_task():
for i in range(100000):
increment()
def main_task():
global x
# setting global variable x as 0
x = 0
# creating threads
t1 = threading.Thread(target=thread_task)
t2 = threading.Thread(target=thread_task)
# start threads
t1.start()
t2.start()
# wait until threads finish their job
t1.join()
t2.join()
if __name__ == "__main__":
for i in range(10):
main_task()
print("Iteration {0}: x = {1}".format(i,x))
• Output:
• Iteration 0: x = 1477960
• Iteration 1: x = 1468219
• Iteration 2: x = 1497968
• Iteration 3: x = 1666032
• Iteration 4: x = 1581574
• Iteration 5: x = 1474136
• Iteration 6: x = 1539974
• Iteration 7: x = 1559577
• Iteration 8: x = 1575546
• Iteration 9: x = 1613644
• In above program:
• Two threads t1 and t2 are created in main_task function and global variable x is
set to 0.
• Each thread has a target function thread_task in which increment function is
called 100000 times.
• increment function will increment the global variable x by 1 in each call.
• The expected final value of x is 200000 but what we get in 10 iterations
of main_task function is some different values.
• This happens due to concurrent access of threads to the shared variable x. This
unpredictability in value of x is nothing but race condition.
Notice that expected value of x in above diagram is 12 but due to race condition, it
turns out to be 11!
Using Locks
• threading module provides a Lock class to deal with the race conditions.
• Lock is implemented using a Semaphore object provided by the Operating
System.
Semaphore
• A semaphore is a synchronization object that controls access by multiple
processes/threads to a common resource in a parallel programming
environment.
• It is simply a value in a designated place in operating system (or kernel)
storage that each process/thread can check and then change.
• Depending on the value that is found, the process/thread can use the
resource or will find that it is already in use and must wait for some period
before trying again.
• Semaphores can be binary (0 or 1) or can have additional values.
• Typically, a process/thread using semaphores checks the value and then, if
it using the resource, changes the value to reflect this so that subsequent
semaphore users will know to wait.
Lock()
• A new lock is created by calling the Lock() method, which returns the new lock.
• The acquire(blocking) method of the new lock object is used to force threads to
run synchronously.
• The optional blocking parameter enables you to control whether the thread waits
to acquire the lock.
• If blocking is set to 0,
• the thread returns immediately with a 0 value if the lock cannot be acquired and
• with a 1 if the lock was acquired.
• If blocking is set to 1, the thread blocks and wait for the lock to be released.
release() : To release a lock.
• The release() method of the new lock object is used to release the lock when it is
no longer required.
• If lock is already unlocked, a ThreadError is raised.
import threading
# global variable x
x = 0
def increment():
global x
x += 1
def thread_task(lock):
for i in range(100000):
lock.acquire()
increment()
lock.release()
def main_task():
global x
x = 0
# creating a lock
lock = threading.Lock()
# creating threads
t1 = threading.Thread(target=thread_task,
args=(lock,))
t2 = threading.Thread(target=thread_task,
args=(lock,))
# start threads
t1.start()
t2.start()
# wait until threads finish their job
t1.join()
t2.join()
if __name__ == "__main__":
for i in range(10):
main_task()
print("Iteration {0}: x = {1}".format(i,x))
Output:
• Iteration 0: x = 200000
• Iteration 1: x = 200000
• Iteration 2: x = 200000
• Iteration 3: x = 200000
• Iteration 4: x = 200000
• Iteration 5: x = 200000
• Iteration 6: x = 200000
• Iteration 7: x = 200000
• Iteration 8: x = 200000
• Iteration 9: x = 200000
• Firstly, a Lock object is created using:
lock = threading.Lock()
• Then, lock is passed as target function argument:
t1 = threading.Thread(target=thread_task, args=(lock,))
t2 = threading.Thread(target=thread_task, args=(lock,))
• In the critical section of target function, we apply lock
using lock.acquire()method.
• As soon as a lock is acquired, no other thread can access the critical section
(here, increment function) until the lock is released using lock.release()method.
lock.acquire()
increment()
lock.release()
• As you can see in the results, the final value of x comes out to be 200000 every
time (which is the expected final result).
Advantages of Mutithreading
• It doesn’t block the user. This is because threads are independent of each other.
• Better use of system resources is possible since threads execute tasks parallely.
• Enhanced performance on multi-processor machines.
• Multi-threaded servers and interactive GUIs use multithreading exclusively.
Disadvantages of multithreading
• As number of threads increase, complexity increases.
• Synchronization of shared resources (objects, data) is necessary.
• It is difficult to debug, result is sometimes unpredictable.
• Potential deadlocks which leads to starvation, i.e. some threads may not be
served with a bad design
• Constructing and synchronizing threads is CPU/memory intensive.
•courseera

Python Unit II.pptx

  • 1.
    Types of Errors(exceptions) Syntax Error or Compile Time Errors (addressed by programmer) Logical Errors or Bugs (addressed by software testing team) Run Time Errors – arises not due to mistake of programmer but due to: Program receiving wrong input from the user, Not being able to connect with database, Experiencing network failure or facing server issues etc. But as developer, we must have some mechanism to address these errors so that program execution might not abruptly get stopped.
  • 2.
    What is anException? • An exception is an error that happens during execution of a program. • When that error occurs, Python generate an exception that can be handled, which avoids your program to crash. • Whenever an exception occurs the program halts the execution and thus further code is not executed. • Thus exception is that error which python script is unable to tackle with.
  • 3.
    Why use Exceptions? •Exception in a code can also be handled. • In case it is not handled, then the code is not executed further and hence execution stops when exception occurs.
  • 4.
    Hierarchy Of Exception •ZeroDivisionError: Occurs when a number is divided by zero. • NameError: It occurs when a name is not found. It may be local or global. • IndentationError: If incorrect indentation is given. • IOError: It occurs when Input-Output operation fails. • EOFError: It occurs when end of file is reached and yet operations are being performed
  • 5.
    Exception Handling • Pythonprovides two very important features to handle any unexpected error in your Python programs and to add debugging capabilities in them- 1. Exception Handling. 2. Assertions.
  • 6.
    Standard Exceptions EXCEPTION NAMEDESCRIPTION Exception Base class for all exceptions ArithmeticError Base class for all errors that occur for numeric calculation. ZeroDivisonError Raised when division or modulo by zero takes place for all numeric types. EOFError Raised when there is no input from either the raw_input() or input() function and the end of file is reached. ImportError Raised when an import statement fails. KeyboardInterrupt Raised when the user interrupts program execution, usually by pressing Ctrl + C. NameError Raised when an identifier is not found in the local or global namespace. SyntaxError Raised when there is an error in Python syntax.
  • 7.
    EXCEPTION NAME DESCRIPTION IndentationErrorRaised when indentation is not specified properly. TypeError Raised when an operation or function is attempted that is invalid for the specified data type. Standard Exceptions
  • 8.
    Exception Handling • Thesuspicious code can be handled by using the try block. • Enclose the code which raises an exception inside the try block. • The try block is followed except statement. • It is then further followed by statements which are executed during exception and in case if exception does not occur.
  • 9.
    • Code inwhich exception may occur Try • Raise the exception Raise • Catch if excepetion occurs Except
  • 10.
    Declaring Multiple Exception •Multiple Exceptions can be declared using the same except statement: try: code except (Exception1,Exception2,Exception3,..,ExceptionN): execute this code in case any Exception of these occur. else: execute code in case no exception occurred.
  • 11.
  • 12.
    try: malicious code except (Exception1): executecode except (Exception2): execute code .... .... except (ExceptionN): execute code else: In case of no exception, execute the else block code.
  • 13.
    try: a=10/0 print a except (ArithmeticError): print("This statement is raising an exception" ) else: print ("Welcome“)
  • 14.
    def fun(a,b): try: c=(a+b)/(a-b) except (ArithmeticError): print("EXCEPTIONOCCUR") else: print(c) finally: print("End of the world") # Driver program to test above function fun(2.0, 3.0) # -5.0 End of the world fun(3.0, 3.0) # EXCEPTION OCCUR End of the world
  • 15.
    Argument of anException • An exception can have an argument, which is a value that gives additional information about the problem. • The contents of the argument vary by exception. • You capture an exception's argument by supplying a variable in the except clause as follows- try: You do your operations here ...................... except ExceptionType as Argument: You can print value of Argument here...
  • 16.
    Example 1 def fun(a): try: returnint(a) except ValueError as VE: print("argument is not a number",VE) fun(11) # 11 fun("string") #argument is not a number invalid literal for int() with base 10: 'string’
  • 17.
    Example 2 def fun(a,b): c=a/b return c try: fun("a","b") except TypeError as VE: print("Exception: ",VE) Output: Exception: unsupported operand type(s) for /: 'str' and 'str'
  • 18.
    Raising Exception • Theraise statement allows the programmer to force a specific exception to occur. • The sole argument in raise indicates the exception to be raised. • This must be either an exception instance or an exception class (a class that derives from Exception). # Program to depict Raising Exception try: raise NameError("Hey! are you sleeping!") except NameError as NE: print (NE) # Raise Error
  • 19.
    try: raise Exception("How areyou") except Exception as E: print(E) How are you
  • 20.
    Explanation: i) Toraise anexception, raise statement is used. It is followed by exception class name. ii) Exception can be provided with a value(optional) that can be given in the parenthesis. (here, Hey! are you sleeping!) iii) To access the value "as" keyword is used. “NE" is used as a reference variable which stores the value of the exception.
  • 21.
    User-defined Exceptions inPython Creating User-defined Exception • Programmers may name their own exceptions by creating a new exception class. • Exceptions need to be derived from the Exception class, either directly or indirectly. • Although not mandatory, most of the exceptions are named as names that end in “Error”similar to naming of the standard exceptions in python.
  • 22.
    # A pythonprogram to create user-defined exception # class MyError is derived from super class Exception class MyError(Exception): # Constructor or Initializer def init (self, value): self.value = value # str is to print() the value def str (self): return(repr(self.value))
  • 23.
    Introduction to PYTHON (BCAC0009) Module2/ Lecture-4 Python Classes Course: BCA II Year III Semester By: Dr Aditya Saxena Assistant Professor Computer Engineering & Applications Department, GLA University, Mathura
  • 24.
    Python Objects andClasses • Python is an object-oriented programming language. Unlike procedure-oriented programming, where the main emphasis is on functions, object-oriented programming stresses on objects. • In OOP, Functions are called Methods.
  • 25.
    Python Objects andClasses • An object is simply a collection of Attributes and Methods (functions) that act on those data.
  • 26.
    Python Objects andClasses . • In Python, Methods are defined inside a class using functions via def key-word with one difference – the first formal argument is ‘self’. • In Python, Attributes are initialized using a special method _ _init_ _ via def key work – again first argument is ‘self’
  • 27.
    • We canthink of a class as a blueprint (prototype) of a house which contains all the details about the floors, doors, windows, etc. Based on these descriptions we build the house. • House is the object. • As many houses can be made from a house's blueprint, we can create many objects from a class. • An object is also called an instance of a class and the process of creating this object is called instantiation. Python Objects and Classes
  • 28.
    In Python, objectsare created (instantiated) using syntax: obj = Class_Name() Any method defined inside a class can be accessed only by the object of that class using syntax: obj.method_name(list of parameters) Python Objects and Classes
  • 29.
    Defining a Classin Python Like function definitions begin with the def keyword in Python, class definitions begin with a class keyword. class Computer: pass
  • 30.
    Creating an Objectin Python To access different attributes and methods of class, we have to create an object of that class class Computer: pass com1 = Computer( ) No attribute or method in class – only placeholder ‘pass’ Creation of an object 'com1' of class Computer
  • 31.
    Special Method __init_ _ (also called constructor) __init__ is a special method which is used to initialize the values of attributes assigned to object It does not need to be called unlike other defined methods of a class. It is called by itself as a new object is created of that class This special method allows to impart attribute values to objects at the same as they are created.
  • 32.
    Assigning values toobjects using instance variables via. __init __ method
  • 33.
    Class (static) variable,and Instance variables Output Static Variable Instance Variables
  • 34.
    Only Class (static)variable without Instance variables (No _ _init_ _) Output
  • 35.
    >>>class test: def show(self): print("SHOWMETHOD") >>>a=test() >>>a.show() or test.show(a) Output: SHOW METHOD
  • 36.
    In [1]: classtest: ...: def show(): ...: print("Message") ...: In [2]: a=test() In [3]: a.show() ## test.show(a) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-3-276f7237b96c> in <module>() ----> 1 a.show() TypeError: show() takes 0 positional arguments but 1 was given
  • 37.
    self in Python…??? classPoint: def __init__(self , x , y): self.x = x self.y = y def distance(self): return (self.x**2 + self.y**2) ** 0.5 p1 = Point(6,8) print('The Distance between 6 and 8 is', p1.distance()) print('The Distance between 6 and 8 is', Point.distance(p1))
  • 38.
    • In theabove example, __init__() defines three parameters but we just passed two (6 and 8). • Similarly distance() requires one but zero arguments were passed. • Why is Python not complaining about this argument number mismatch?
  • 39.
    • A peculiarthing about methods (in Python) is that the object itself is passed on as the first argument to the corresponding function……………IMPLICIT • the method call p1.distance() is actually equivalent to Point.distance(p1). • Generally, when we call a method with some arguments, the corresponding class function is called by placing the method's object before the first argument. • So, anything like obj.meth(args) becomes Class.meth(obj, args).
  • 40.
    class student: count=0 #class variable def __init__(self, na, ma): print("Constructor invoked") self.name=na self.marks=ma student.count=student.count+1 def display(self): print("NAME ",self.name) print("MARKS ",self.marks) def total(self): print("Total students ",student.count)
  • 41.
    a=student("abc",99) #Constructor invoked b=student("xyz",88)#Constructor invoked a.display() #NAME abc #MARKS 99 b.display() #NAME xyz #MARKS 88 student.count # 2 a.count # 2 b.Count # 2
  • 42.
    • The variablecount is a class variable whose value is shared among all the instances of the class. • This can be accessed as student.count from inside the class or outside the class. • The first method __init__() is a special method, which is called initialization method that Python calls when you create a new instance of this class.
  • 44.
    • getattr(obj, name[,default]): to access the attribute of object. • hasattr(obj,name): to check if an attribute exists or not. • setattr(obj,name,value): to set an attribute. If attribute does not exist, then it would be created. • delattr(obj, name): to delete an attribute.
  • 46.
    Built-In Class Attributes •Every Python class keeps the following built-in attributes and they can be accessed using dot operator like any other attribute − __dict__: Dictionary containing the class's namespace. __doc__: Class documentation string or none, if undefined. __module__: Module name in which the class is defined. This attribute is "__main__" in interactive mode. __name__: Class name. __bases__: A possibly empty tuple containing the base classes, in the order of their occurrence in the base class list.
  • 47.
    Garbage Collection inPython • In any program, garbage is a collection of memory that is not being used by the program or was being used at a given point but not anymore. • As this memory space remains allocated to the program but is not being used by it hence it is wasted memory which needs to be cleaned. • Garbage collection is a process of freeing up memory that is not in use by a specific program at a given instance of time.
  • 48.
    • This processis helpful in memory management and minimizes the wastage of memory. The memory freed by garbage collection can be used for storing other important data or variables for the same program or by other programs. Garbage Collection in Python
  • 49.
    Python: Automatic Garbagecollection • Garbage collection in Python works automatically. • When reference count of a variable/data/value reaches to 0, it is destroyed by the garbage collection mechanism of python. • Thus, python provides with good memory management and prevents the wastage of memory.
  • 50.
    class SomeObj: def __init__(self): print('Theobject is created.') def __del__(self): print('The object is destroyed.') obj1 = SomeObj() obj2 = obj1 obj3 = obj1 print("Set obj1 to None...") obj1 = None print("Set obj2 to None...") obj2 = None Output The object is created. Set obj1 to None... Set obj2 to None... Set obj3 to None... The object is destroyed. The following example shows how the python garbage collection works: when there is no reference left to the object then it is automatically destroyed by the Garbage collector of python and __del__() method is executed.
  • 51.
    • You normallywill not notice when the garbage collector destroys an orphaned instance and reclaims its space. • However, a class can implement the special method __del__(), called a destructor, that is invoked when the instance is about to be destroyed. • This method might be used to clean up any non-memory resources used by an instance. Python: Automatic Garbage collection
  • 52.
    Inheritance in Python •It refers to defining a new class with little or no modification to an existing class. • The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class. class BaseClass: Body of base class class class DerivedClass(BaseClass): Body of derived class
  • 53.
    Single Inheritance In singleinheritance, a child class inherits from a single-parent class. Here is one child class and one parent class.
  • 54.
    # Base class classVehicle: def Vehicle_info(self): print('Inside Vehicle class') # Child class class Car(Vehicle): def car_info(self): print('Inside Car class') # Create object of Car car = Car() # access Vehicle's info using car object car.Vehicle_info() car.car_info() Output: Inside Vehicle class Inside Car class Single Inheritance – example
  • 55.
    Multilevel inheritance In multilevelinheritance, a class inherits from a child class or derived class. Suppose three classes A, B, C. A is the superclass, B is the child class of A, C is the child class of B. In other words, we can say a chain of classes is called multilevel inheritance.
  • 56.
    Multilevel inheritance –example Output Inside Vehicle class Inside Car class Inside SportsCar class
  • 57.
    Multiple Inheritance In multipleinheritance, one child class can inherit from multiple parent classes. So here is one child class and multiple parent classes.
  • 58.
  • 59.
    Python super() function Whena class inherits all properties and behavior from the parent class – it is called inheritance. In such a case, the inherited class is a subclass and the latter class is the parent class. In child class, we can refer to parent class by using the super() function. The super function returns a temporary object of the parent class that allows us to call a parent class method inside a child class method.
  • 60.
    Output Jessa works atGoogle Python super() function – example
  • 61.
    Python super() function…contd. Benefits of using the super() function. 1. We are not required to remember or specify the parent class name to access its methods. 2. We can use the super() function in both single and multiple inheritances. 3. The super() function support code reusability as there is no need to write the entire function
  • 62.
    Method Resolution Orderin Python • In Python, Method Resolution Order(MRO) is the order by which Python looks for a method or attribute. First, the method or attribute is searched within a class, and then it follows the order, we specified while inheriting. • This order is also called the Linearization of a class, and a set of rules is called MRO (Method Resolution Order). The MRO plays an essential role in multiple inheritances as a single method may found in multiple parent classes.
  • 63.
    In multiple inheritance,the following search order is followed. 1. First, it searches in the current parent class if not available, then searches in the parents class specified while inheriting (that is left to right.) 2. We can get the MRO of a class. For this purpose, we can use either the mro attribute or the .mro() method. Method Resolution Order in Python
  • 64.
    Output: In class C [<class'__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>] M.R.O in Python – example In this example, we create three classes named A, B and C. Class B is inherited from A, class C is inherits from B and A. When we create an object of the C class and calling the process() method, Python looks for the process() method in the current class in the C class itself. Then search for parent classes, namely B and A, because C class inherit from B and A. that is, C(B, A) and always search in left to right manner.
  • 65.
    ACCESSIBILITY In Python, thereare no keywords like public, protected or private. All attributes and methods are public by default. There is one way to define private in Python. The syntax to define private attribute and methods is: _ _Attribute _ _Methods_Name() To make an attribute and a method private, we need to add two underscores, i.e. ‘_ _’ in front of the attribute and the method’s name. It helps in hiding these when accessed out of class.
  • 66.
  • 67.
    Explanation In theabove program, we have defined public and private attributes. The private variable can be accessed from the method. We have created an instance of class Person, i.e. P to access the attributes defined within the class. However, it fails and shows an error when instance of class try to access private attributes defined inside the class and are accessed from outside the class. ACCESSIBILITY – example
  • 68.
    Printing objects giveus information about the objects we are working with. In Python, this can be achieved by using __repr__ or __str__ methods. __repr__ is used if we need a detailed information for debugging while __str__ is used to print a string version for the users. Printing Objects
  • 69.
    Printing Objects class Person: def__init__(self, person_name, person_age): self.name = person_name self.age = person_age def __str__(self): return f'Person name is {self.name} and age is {self.age}' def __repr__(self): return f'Person(name={self.name}, age={self.age})' p = Person('Pankaj', 34) print(p.__str__()) print(p.__repr__()) Output
  • 70.
    Polymorphism in Python Theword ‘Poly’ means many and ‘Morphs’ means forms. The process of representing “one form in many forms” is called a polymorphism. Types of Polymorphism in Python: 1. Duck Typing 2. Method overriding 3. Method Overloading 4. Operator Overloading
  • 71.
    Method Overriding inPython • Method overriding is an object-oriented programming feature that allows a subclass to provide a different implementation of a method that is already defined by its super class or by one of its super classes. • The implementation in the subclass overrides the implementation of the super class by providing a method with the same name, same parameters or signature, and same return type as the method of the parent class.
  • 72.
    class Dad: def show(self): print('Ihave Nokia 3310') class Son(Dad): def show(self): print('I have iPhone') ravi = Son() ravi.show() Method Overriding in Python-example
  • 73.
    Method Overloading(does notwork in PYTHON) • Overloading is the ability to define the same method, with the same name but with a different number of arguments and types. • It's the ability of one function to perform different tasks, depending on the number of parameters or the types of the parameters. • If we need such a behavior, we can simulate it with default parameters.
  • 74.
  • 75.
    Python Operator Overloading •You can change the meaning of an operator in Python depending upon the operands used. • This practice is known as operating overloading.
  • 76.
    class Point: def __init__(self,x = 0, y = 0): self.x = x self.y = y p1 = Point(2,3) p2 = Point(-1,2) print(p1) <__main__.Point object at 0x00000000031F8CC0> p1 + p2 Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'Point' and 'Point'
  • 77.
    class Point: def __init__(self,x = 0, y = 0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x,self.y) >>> p1 = Point(2,3) >>> print(p1) (2,3) >>> str(p1) '(2,3)'
  • 78.
    Overloading the +Operator in Python class Point: def __init__(self, x = 0, y = 0): self.x = x self.y = y def __str__(self): return "({0},{1})".format(self.x,self.y) def __add__(self, other): x = self.x + other.x y = self.y + other.y return Point(x,y)
  • 79.
    >>> p1 =Point(2,3) >>> p2 = Point(-1,2) >>> print(p1 + p2) (1,5)
  • 80.
    Operator Overloading SpecialFunctions in Python Operator Expression Internally Addition p1 + p2 p1.__add__(p2) Subtraction p1 - p2 p1.__sub__(p2) Multiplication p1 * p2 p1.__mul__(p2) Power p1 ** p2 p1.__pow__(p2) Division p1 / p2 p1.__truediv__(p2) Floor Division p1 // p2 p1.__floordiv__(p2) Remainder (modulo) p1 % p2 p1.__mod__(p2)
  • 81.
    Comparison Operator Overloadingin Python Operator Expression Internally Less than p1 < p2 p1.__lt__(p2) Less than or equal to p1 <= p2 p1.__le__(p2) Equal to p1 == p2 p1.__eq__(p2) Not equal to p1 != p2 p1.__ne__(p2) Greater than p1 > p2 p1.__gt__(p2) Greater than or equal to p1 >= p2 p1.__ge__(p2)
  • 82.
    Overloading Comparison Operatorsin Python class Point: def __init__(self, x = 0, y = 0): self.x = x self.y = y def __lt__(self,other): smag = (self.x ** 2) + (self.y ** 2) omag = (other.x ** 2) + (other.y ** 2) return smag < omag
  • 83.
    >>> Point(1,1) <Point(-2,-3) True >>> Point(1,1) < Point(0.5,-0.2) False >>> Point(1,1) < Point(1,1) False
  • 84.
    Composition In this concept,we will describe a class that references to one or more objects of other classes as an Instance variable. Here, by using the class name or by creating the object we can access the members of one class inside another class. It means that a class Composite(B) can contain an object of another class Component(A). This type of relationship is known as Has-A Relation. class A : # variables, methods of class A ... ... class B : # by using "obj" we can access member's of class A. obj = A() # variables, methods of class B ... ...
  • 85.
    class demo: def __init__(self,a,b,c): self.obj=test(a,b) self.c=c defshow1(self): self.obj.show() print(self.c) d=demo(1,2,3) d.show1() class test: def __init__(self,x,y): self.x=x self.y=y def show(self): print(self.x,self.y) Output: 1 2 3
  • 86.
    What is regularexpression? • The term "regular expression", sometimes also called regex or regexp, is originated in theoretical computer science. • In theoretical computer science they are used to define a language-family with certain characteristics. • Regular Expressions are used in programming languages to filter texts or strings. • It's possible to check - if a text or a string matches a regular expression.
  • 87.
    • The syntaxof regular expressions is the same for all programming and script languages, e.g. Python, Perl, Java, AWK and even X#. • In Python, we have module “re” that helps with regular expressions. • So you need to import library re before you can use regular expressions in Python. import re
  • 88.
    • Regular expressionsuse two types of characters: a)Meta characters: these characters have a special meaning, similar to * in wild-card. Ex. . ^ $ * + ? { } [ ] | ( ) b) Literals: have usual meaning Ex. A, B, a,b,text,1,2…….
  • 89.
    The most commonuses of regular expressions: • Search a string (search and match) re.match(), re.search() • Finding a string (findall) re.findall() • Break string into a sub strings (split) re.split() • Replace part of a string (sub) re.sub()
  • 90.
    The following special-charactersequences are recognized in regular expression patterns: Character Description Matches any character except newline. Matches the start of a string. Matches the end of a string. . ^ $
  • 91.
    The following special-charactersequences are recognized in regular expression patterns: Character Description Matches zero or more repetitions of the preceding (before) expression, matching as many repetitions as possible. Matches one or more repetitions of the preceding expression, matching as many repetitions as possible. * +
  • 92.
    Difference between *and + raw * Matches zero or more repetitions of the preceding (before) expression, matching as many repetitions as possible. + Matches one or more repetitions of the preceding expression, matching as many repetitions as possible.
  • 93.
    *? Matches zeroor more repetitions of the preceding expression, matching as few repetitions as possible. ? Matches zero or one repetition of the preceding expression. neither 0 nor 1 repetitions so no match
  • 94.
    {m} Matches exactlym repetitions of the preceding expression. {m, n} Matches from m to n repetitions of the preceding expression, matching as many repetitions as possible. If m is omitted, it defaults to 0. If n is omitted, it defaults to infinity. Exactly 2 repetitions of 'l' From 0 to 2 repetitions of 'l'
  • 95.
    Expression String Matched? [abc] a 1 match ac 2 matches Hey Jude No match abc de ca 5 matches [ ] - Square brackets Square brackets specifies a set of characters we wish to match. Here, [abc] will match if the string we are trying to match contains any of the a, b or c.
  • 96.
    We can alsospecify a range of characters using - inside square brackets. •[a-e] is the same as [abcde]. •[1-4] is the same as [1234]. •[0-39] is the same as [01239]. We can complement (invert) the character set by using caret ^ symbol at the start of a square- bracket. •[^abc] means any character except a or b or c. •[^0-9] means any non-digit character.
  • 97.
    Expression String Matched? a|b cdeNo match ade 1 match (match at ade) acdbea 3 matches (at acdbea) | - Alternation Vertical bar | is used for alternation (or operator). Here, a | b match any string that contains either a or b Here, a | b match any string that contains either a or b
  • 98.
    Expression String Matched? (a| b | c)xz ab xz No match abxz 1 match (match at abxz) axz cabxz 2 matches (at axzbc cabxz) () - Group Parentheses () is used to group sub-patterns. For example, (a|b|c)xz match any string that matches either axz or bxz or cxz.
  • 99.
    EXAMPLES • a="saghjz ghj sd hhjg“ • re.findall("hj", a) • ['hj', 'hj', 'hj'] • re.findall("hj.", a) • ['hjz', 'hj ', 'hjg'] • re.findall("hj?",a) • ['hj', 'hj', 'h', 'hj'] ? Matches zero or one repetition of the preceding expression. *? Matches zero or more repetitions of the preceding expression, matching as few repetitions as possible.
  • 100.
    • a="saghjz ghj sd hhjg“ • re.findall("^hj",a) • [] • b="hj sa ghjz g hj sd hhjg" • re.findall("^hj",b) • ['hj']
  • 101.
    • a="zz bbbbccc“ • re.findall("n*",a) • ['', '', '', '', '', '', '', '', '', '', '', ''] • b="zzbbbccccc“ • re.findall("b*",b) • ['', '', 'bbb', '', '', '', '', '', '']
  • 102.
    • In [1]:import re • In [2]: a="a sgg ss“ • In [3]: b=re.findall("[sa]",a) • In [4]: print(b) • ['a', 's', 's', 's'] • In [5]: b=re.findall("[^sa]",a) • In [6]: print(b) • [' ', 'g', 'g', ' ']
  • 103.
    • In [8]:a="a sgg ss“ • In [9]: b=re.findall("s{2}",a) • In [10]: print(b) • ['ss'] • In [11]: b=re.findall("s{3}",a) • In [12]: print(b) • [] • In [13]: b=re.findall("s{1}",a) • In [14]: print(b) • ['s', 's', 's'] • In [15]: b=re.findall("s{0}",a) • In [16]: print(b) • ['', '', '', '', '', '', '', '', '']
  • 104.
    • In [17]:b=re.findall("s{1,3}",a) • In [18]: print(b) • ['s', 'ss'] • In [24]: a="BCA GLA UNIVERSITY“ • In [25]: b=re.findall("BCA|GLA",a) • In [26]: print(b) • ['BCA', 'GLA']
  • 105.
    • In [27]:a="BCA GLA UNIVERSITY MBA CEA BBA“ • In [28]: b=re.findall("(.A)",a) • In [29]: print(b) • ['CA', 'LA', 'BA', 'EA', 'BA'] • In [39]: a="BCA 2001 GLA“ • In [40]: b=re.findall("([0-9])",a) • In [41]: print(b) • ['2', '0', '0', '1']
  • 106.
    • (.*) meansany character (.) repeated zero or more times (*). • The fact that it is enclosed in parentheses tells Python to save away the part of the string matched by that part of the pattern as a group—a matched substring available after the match.
  • 107.
    In [53]: b=re.findall("d{1,2}",a) In[54]: print(b) ['8', '20', '17'] In [55]: b=re.findall("d{1,5}",a) In [56]: print(b) ['8', '2017'] In [57]: b=re.findall("d{5,6}",a) In [58]: print(b) [] In [48]: a="8 NOV 2017“ In [49]: b=re.findall("d{1,3}",a) In [50]: print(b) ['8', '201', '7'] In [51]: b=re.findall("d{1,4}",a) In [52]: print(b) ['8', '2017']
  • 108.
    Predefined Character Classes •You might have realized that it can be quite cumbersome to construct certain character classes. • A good example is the character class, which describes a valid word character. • These are all lower case and uppercase characters plus all the digits and the underscore, corresponding to the following regular expression: r"[a-zA-Z0-9_]“
  • 109.
    • The specialsequences consist of "" and a character from the following list: • d Matches any decimal digit; equivalent to the set [0-9]. • D The complement of d. It matches any non- digit character; equivalent to the set [^0-9]. • s Matches any whitespace character; equivalent to [ tnrfv]. • S The complement of s. It matches any non- whitespace character; equiv. to [^ tnrfv].
  • 110.
    • w Matchesany alphanumeric character; equivalent to [a-zA-Z0-9_]. • WMatches the complement of w. • b Matches the empty string, but only at the start or end of a word. • B Matches the empty string, but not at the start or end of a word. • Matches a literal backslash.
  • 111.
    Compilation flags • Compilationflags let you modify some aspects of how regular expressions work. • Flags are available in the re module under two names, a long name such as IGNORECASE and a short, one-letter form such as I. Flag • ASCII, A Meaning Makes several escapes like w, b, s and d match only on ASCII characters with the property. Make, match any character, including newlines respective • DOTALL, S • IGNORECASE, I • MULTILINE, M Do case-insensitive matches Multi-line matching, affecting ^ and $
  • 112.
    • In [54]:a="GLA University“ • In [55]: b=re.search("gla",a) • In [56]: print(b) • None • In [57]: b=re.search("gla",a, re.I) • In [58]: print(b.group()) • GLA
  • 113.
    Any Character . •All three letter words, which end with "at". • The syntax of regular expressions supplies a metacharacter ".", which is used like a placeholder for "any character". • The regular expression of our example can be written like this: r" .at " • This RE matches three letter words, isolated by blanks, which end in "at". • Now we get words like "rat", "cat", "bat", "eat", "sat" and many others. • But what, if the text contains "words" like "@at" or "3at"? These words match as well and this means that we have created over matching again. We will learn a solution in the following section:
  • 114.
    Character Classes • Squarebrackets, "[" and "]", are used to include a character class. • [xyz] means e.g. either an "x", an "y" or a "z". Let's look at a more practical example:r"M[ae][iy]er" • This is a regular expression, which matches a surname which is quite common in German. • A name with the same pronunciation and four different spellings: Maier, Mayer, Meier, Meyer
  • 116.
    • The onlyother special character inside square brackets (character class choice) is the caret "^". • If it is used directly after an opening square bracket, it negates the choice. [^0-9] denotes the choice "any character but a digit". • The position of the caret within the square brackets is crucial. • If it is not positioned as the first character following the opening square bracket, it has no special meaning. • [^abc] means anything but an "a", "b" or "c" • [a^bc] means an "a", "b", "c" or a "^
  • 117.
    WHY WE NEEDRE ? >>>phone_number = "Home: (555) 265-2901“ >>>start = phone_number.find('(') >>> print(start) 6 >>> start = start + 1 >>> end = start + 3 >>> area_code = phone_number[start : end] >>> print(area_code) 555 Extract Area Code >>> phone_number = "Cell (mobile): (555)-918-8271“ ## Previous code can’t work here
  • 120.
    What are variousmethods of Regular Expressions? The ‘re’ package provides multiple methods to perform queries on an input string. • re.match() • re.search() • re.findall() • re.split() • re.sub() • re.compile()
  • 121.
    re.match(pattern, string): • Thematch() method will only find matches if they occur at the start of the string being searched. • For example, calling match() on the string ‘AV Analytics AV’ and looking for a pattern ‘AV’ will match. • However, if we look for only Analytics, the pattern will not match. Code >>> import re >>> result = re.match(r'AV', 'AV Analytics Vidhya AV') >>> result <re.Match object; span=(0, 2), match='AV'> >>> result.start() 0 >>> result.end() 2 >>> result.group(0) 'AV'
  • 122.
    The match Function •This function attempts to match RE pattern to string with optional flags. • re.match(pattern, string, flags=0) • pattern This is the regular expression to be matched. • string This is the string, which would be searched to match the pattern at the beginning of string. • flags You can specify different flags using bitwise OR (|).
  • 123.
    • The re.matchfunction returns a match object on success, None on failure. • We use group(num) or groups() function of match object to get matched expression. Match Object Methods Description • group(num=0) • groups() This method returns entire match (or specific subgroup num) This method returns all matching subgroups in a tuple (empty if there weren't any)
  • 124.
    • So forexample, calling match() on the string ‘dog cat dog’, looking for the pattern ‘dog’ will match: >>> re.match(r'dog', 'dog cat dog') <re.Match object; span=(0, 3), match='dog'> >>> match = re.match(r'dog', 'dog cat dog') >>> match.group(0) 'dog'
  • 125.
    • There aremethods like start() and end() to know the start and end position of matching pattern in the string. • result = re.match('AV', 'AV Analytics Vidhya AV') • print (result.start()) • print (result.end()) Output: 0 2 Above you can see that start and end position of matching pattern ‘AV’ in the string and sometime it helps a lot while performing manipulation with the string.
  • 126.
    • But, ifwe call match() on the same string, looking for the pattern ‘cat’, we won’t: >>> a=re.match(r'cat', 'dog cat dog') >>>print(a) none
  • 127.
    re.search(pattern, string) • Itis similar to match() but it doesn’t restrict us to find matches at the beginning of the string only. • Unlike previous method, here searching for pattern ‘Analytics’ will return a match. Code • result = re.search(r'Analytics', 'AV Analytics Vidhya AV') • print (result.group(0)) Output: Analytics Here you can see that, search() method is able to find a pattern from any position of the string but it only returns the first occurrence of the search pattern.
  • 128.
    a = 'catsare smarter than dogs' b=re.search(r"(w*) are (w*) (.*)",a) b.group() ## or b.group(0) 'cats are smarter than dogs‘ b.group(1) 'cats' b.group(2) 'smarter' b.group(3) 'than dogs' re.search(pattern, string)
  • 129.
    re.findall (pattern, string): •It helps to get a list of all matching patterns. • It has no constraints of searching from start or end. • If we will use method findall to search ‘AV’ in given string it will return both occurrence of AV. • re.findall() it can work like re.search() and re.match() both. • result = re.findall('AV', 'AV Analytics Vidhya AV') • print (result) Output: ['AV', 'AV']
  • 130.
    re.split(pattern, string, [maxsplit=0]): •This methods helps to split string by the occurrences of given pattern. Code • result=re.split(‘m','Animal') • result Output: ['Ani', ‘al']
  • 131.
    • Method split()has another argument “maxsplit“. It has default value of zero. • In this case it does the maximum splits that can be done, but if we give value to maxsplit, it will split the string.
  • 132.
    • result=re.split('i','Analytics Vidhya') •print (result) Output: ['Analyt', 'cs V', 'dhya'] #It has performed all the splits that can be done by pattern "i". Code • result=re.split(r'i','Analytics Vidhya', maxsplit=1) • result Output: ['Analyt', 'cs Vidhya']
  • 133.
    re.sub(pattern, repl, string) •It helps to search a pattern and replace with a new sub string. • If the pattern is not found, string is returned unchanged. Code • result=re.sub('India','the World','AV is largest Analytics community of India') • result Output: 'AV is largest Analytics community of the World'
  • 134.
    re.compile(pattern, repl, string) •We can combine a regular expression pattern into pattern objects, which can be used for pattern matching. • It also helps to search a pattern again without rewriting it. Code • pattern=re.compile('AV') • result=pattern.findall('AV Analytics Vidhya AV') • print (result) • ['AV', 'AV'] • result2=pattern.findall('AV is largest analytics community of India') • print (result2) • ['AV']
  • 135.
    Some Examples ofRegular Expressions Problem 1: Return the first word of a given string Solution-1 Extract each character (using “w“) Code import re result=re.findall(r'.','AV is largest Analytics community of India') print (result) Output: ['A', 'V', ' ', 'i', 's', ' ', 'l', 'a', 'r', 'g', 'e', 's', 't', ' ', 'A', 'n', 'a', 'l', 'y', 't', 'i', 'c', 's', ' ', 'c',
  • 136.
    • Above, spaceis also extracted, now to avoid it use “w” instead of “.“. • Code • result=re.findall(r'w','AV is largest Analytics community of India') • print( result) Output: ['A', 'V', 'i', 's', 'l', 'a', 'r', 'g', 'e', 's', 't', 'A', 'n', 'a', 'l', 'y', 't', 'i', 'c', 's', 'c', 'o', 'm', 'm', 'u', 'n', 'i', 't', 'y', 'o', 'f', 'I', 'n', 'd', 'i', 'a']
  • 137.
    • Solution-2 Extracteach word (using “*” or “+“) Code • result=re.findall(r'w*','AV is largest Analytics community of India') • print(result) Output: ['AV', '', 'is', '', 'largest', '', 'Analytics', '', 'community', '', 'of', '', 'India', ''] it is returning space as a word because “*” returns zero or more matches of pattern to its
  • 138.
    • Now toremove spaces we will go with “+“. Code • result=re.findall(r'w+','AV is largest Analytics community of India') • print (result) Output: ['AV', 'is', 'largest', 'Analytics', 'community', 'of', 'India']
  • 139.
    Problem 2: Returnthe first two character of each word • Solution-1 Extract consecutive two characters of each word, excluding spaces (using “w“) • Code • result=re.findall(r'ww','AV is largest Analytics community of India') • print (result) Output: ['AV', 'is', 'la', 'rg', 'es', 'An', 'al', 'yt', 'ic', 'co', 'mm', 'un', 'it', 'of', 'In', 'di']
  • 140.
    • Solution-2 Extractconsecutive two characters those available at start of word boundary (using “b“) • Code • result=re.findall(r'bw.','AV is largest Analytics community of India') • print (result) Output: ['AV', 'is', 'la', 'An', 'co', 'of', 'In']
  • 141.
    Problem 3: Returnthe domain type of given email-ids • Solution-1 Extract all characters after “@” • Code • result=re.findall(r'@w+',' abc.test@gmail.com , xyz@test.in, test.first@analyticsvidhya.com , first.test@rest.biz ') • print (result) Output: ['@gmail', '@test', '@analyticsvidhya', '@rest']
  • 142.
    • Above, youcan see that “.com”, “.in” part is not extracted. • To add it, we will go with below code. • result=re.findall(r'@w+.w+', 'abc.test@gmail.com, xyz@test.in, test.first@analyticsvidhya.com, first.test@rest.biz') • print (result) Output: ['@gmail.com', '@test.in',
  • 143.
    • Solution –2 Extract only domain name using “( )” Code • result=re.findall(r'@w+.(w+)','abc.test@gm ail.com, xyz@test.in, test.first@analyticsvidhya.com, first.test@rest.biz') • print (result) Output: ['com', 'in', 'com', 'biz']
  • 144.
    Problem 4: Returndate from given string • Here we will use “d” to extract digit. • Solution: Code • result=re.findall(r'd{2}-d{2}-d{4}','Amit 34- 3456 12-05-2007, XYZ 56-4532 11-11-2011, ABC 67-8945 12-01-2009') • print (result) Output: ['12-05-2007', '11-11-2011', '12-01- 2009']
  • 145.
    • result=re.findall(r'd{2}-d{2}-(d{4})','Amit 34-3456 12-05-2007, XYZ 56-4532 11-11-2011, ABC 67-8945 12-01-2009') • print (result) Output: ['2007', '2011', '2009']
  • 146.
    Problem 5: Returnall words of a string Solution-1 Code >>> result=re.findall(r'w+','AV is largest Analytics community of India') >>> print( result) ['AV', 'is', 'largest', 'Analytics', 'community', 'of', 'India']
  • 147.
    • Solution-2 Returnwords starts with vowel (using []) Code • result=re.findall(r'[aeiouAEIOU]w+','AV is largest Analytics community of India') • print (result) Output: ['AV', 'is', 'argest', 'Analytics', 'ommunity', 'of', 'India'] Above you can see that it has returned “argest” and “ommunity” from the mid of word
  • 148.
    • Solution- 3 •Code • result=re.findall(r'b[aeiouAEIOU]w+','AV is largest Analytics community of India') • print (result) Output: ['AV', 'is', 'Analytics', 'of', 'India']
  • 149.
    • In similarways, we can extract words those starts with consonant using “^” within square bracket. • Code • result=re.findall(r'b[^aeiouAEIOU]w+','AV is largest Analytics community of India') • print (result) Output: [' is', ' largest', ' Analytics', ' community', ' of', ' India']
  • 150.
    • Above youcan see that it has returned words starting with space. • To drop it from output, include space in square bracket[]. Code • result=re.findall(r'b[^aeiouAEIOU ]w+','AV is largest Analytics community of India') • print (result) Output: ['largest', 'community']
  • 151.
    Problem 6: Validatea phone number (phone number must be of 10 digits and starts with 8 or 9) • We have a list phone numbers in list “li” and here we will validate phone numbers using regular Solution • import re • li=['9999999999','999999-999','99999x9999'] • for val in li: if re.match(r'[8-9]{1}[0-9]{9}',val) and len(val) == 10: print ('yes' ) else: print (‘No' )
  • 152.
    Problem 7: Splita string with multiple delimiters • Solution • import re • line = 'asdf fjdk;afed,fjek,asdf,foo' # String has multiple delimiters (";",","," "). • result= re.split(r'[;,s]', line) • print (result) Output: ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
  • 153.
    • The groupscontain the matched values. • In particular: # match.group(0) always returns the fully matched string • # match.group(1) match.group(2), ... will return the capture # groups in order from left to right in the input string • # match.group() is equivalent to match.group(0)
  • 154.
  • 155.
    Python Database Connectivity •In this topic you will learn how to – use the SQLite database management system with Python – use SQLite, SQL queries, RDBMS
  • 156.
    Content • Python DatabaseConnection –Introduction –Connections –Executing queries –Transactions –Handling error
  • 157.
    Python Database • Datais everywhere and software applications use that. • Data is either in memory, files or databases. • Python has bindings for many database systems including MySQL, Postregsql, Oracle, Microsoft SQL Server and Maria DB. • One of these database management systems (DBMS) is called SQLite. • SQLite was created in the year 2000 by D. Richard Hipp.
  • 158.
    Introduction • The Pythonstandard for database interfaces is the Python DB- API, which is used by Python's database interfaces. • The DB-API has been defined as a common interface, which can be used to access relational databases. • The code in Python for communicating with a database should be the same, regardless of the database and the database module used.
  • 159.
    • The DBAPI provides a minimal standard for working with databases using Python structures and syntax wherever possible. • This API includes the following: – Importing the API module. – Acquiring a connection with the database. – Issuing SQL statements and stored procedures. – Closing the connection
  • 160.
    Connect Create Interact Commit Rollback Close Establish aconnection to your chosen database backend Create a cursor to communicate through the connection to your data Using the cursor manipulate your Data using SQL Tell your connection to apply all of your SQL-manipulation to your data and make them permanent Tell your connection to abort your SQL manipulation, returning your data to the state it was in before the connection started Destroy the connection to the database backend
  • 161.
    SQLite • SQLite isa simple relational database system, which saves its data in regular data files or even in the internal memory of the computer, i.e. the RAM. • It was developed for embedded applications, like Mozilla- Firefox (Bookmarks), Symbian and Android OS. • SQLITE is "quite" fast, even though it uses a simple file. • It can be used for large databases as well. • Python has an in-built support for SQLite.
  • 162.
    Process of databaseconnectivity in python • If you want to use SQLite, you have to import the module sqlite3. • To use a database, you have to create first a Connection object. • The connection object will represent the database.
  • 163.
    Establishing connection toa database • It has to be a SQLite database file of course! >>> import sqlite3 >>> connection =sqlite3.connect("company.db") • We have now created a database with the name "company". • If a file with this name exists, it will be opened. • It's like having sent the command • "CREATE DATABASE company;" to a SQL server. • If you call "sqlite3.connect('company.db')" again, it will open the previously created database.
  • 164.
    Creating a tableusing SQL • The SQL syntax for creating a table "employee" in the database "company" looks like this: CREATE TABLE employee ( staff_number INT NOT NULL, fname lname gender joining birth_date VARCHAR(20), VARCHAR(30), CHAR(1), DATE, DATE, PRIMARY KEY (staff_number) );
  • 165.
    Cursor object • Tobe capable to send a command to "SQL", or SQLite, we need a cursor object. • Usually, a cursor in SQL and databases is a control structure to traverse over the records in a database. • So it's used for the fetching of the results. • In SQLite (and other Python DB interfaces) it is more generally used. • It's used for performing all SQL commands.
  • 166.
    • We getthe cursor object by calling the cursor() method of connection. • An arbitrary number of cursors can be created. • The cursor is used to traverse the records from the result set. • We can define a SQL command with a triple quoted string in Python.
  • 167.
    Syntax of cursorobject cursor = connection.cursor() cursor.execute(sql_command) • Where sql_command is a triple quoted string having sql command.
  • 168.
    sql_command = """ CREATETABLE employee ( staff_number INTEGER PRIMARY KEY, fname lname gender joining VARCHAR(20), VARCHAR(30), CHAR(1), DATE, birth_date DATE);"""
  • 169.
    Manipulation of data •Now we have a database with a table but no data included. • To populate the table we will have to send the "INSERT" command to SQLite. • We will use again the execute method. • To run the program you will either have to remove the file company.db or uncomment the "DROP TABLE" line in the SQL command:
  • 170.
    Example: import sqlite3 connection =sqlite3.connect("employee.db") cursor = connection.cursor() cursor.execute("DROP TABLE IF EXISTS manger") sql = """CREATE TABLE manager( ID INT PRIMARY KEY, FIRST_NAME CHAR(20) NOT NULL, LAST_NAME CHAR(20), AGE INT, SALARY INT)""“ cursor.execute(sql) connection.close()
  • 171.
    INSERT Operation import sqlite3 db=sqlite3.connect("employee.db") cursor=db.cursor() sql="""INSERTINTO employee(ID, FIRST_NAME, LAST_NAME, AGE, SALARY) VALUES(1007,'JAMES','BOND',50,1000), (1009,'AGENT','VINOD',55,500 ), (1010,'AGENT','TIGER',60,700)""“ try: cursor.execute(sql) db.commit() except: db.rollback() finally: db.close()
  • 172.
    READ Operation • Youcan use either fetchone() method to fetch a single record • fetchall() method to fetch multiple values from a database table. • fetchone(): It fetches the next row of a query result set. A result set is an object that is returned when a cursor object is used to query a table. • fetchall(): It fetches all the rows in a result set. If some rows have already been extracted from the result set, then it retrieves the remaining rows from the result set. • rowcount: This is a read-only attribute and returns the number of rows that were affected by an execute() method.
  • 173.
    import sqlite3 db=sqlite3.connect("employee.db") cursor=db.cursor() sql="SELECT *FROMemployee" try: cursor.execute(sql) results=cursor.fetchall() for rows in results: print(rows) except: print("some error") db.close()
  • 174.
    • (1007, 'JAMES','BOND', 50, 1000) • (1009, 'AGENT', 'VINOD', 55, 500) • (1010, 'AGENT', 'TIGER', 60, 700)
  • 175.
    Update Operation • UPDATEOperation on any database means to update one or more records, which are already available in the database. • The following procedure updates all the records. • Here, we increase the SALARY of all the managers by 5000 if their salary is less than 1000.
  • 176.
    • import sqlite3 •db=sqlite3.connect("employee.db") • cursor=db.cursor() • sql = """UPDATE manager SET SALARY = SALARY + 5000 • WHERE SALARY < 1000""" • try: • cursor.execute(sql) • db.commit() • except: • db.rollback() • db.close()
  • 177.
    DELETE Operation • importsqlite3 • db=sqlite3.connect("employee.db") • cursor=db.cursor() • sql = """DELETE FROM manager • WHERE SALARY < 5000""" • try: • cursor.execute(sql) • db.commit() • except: • db.rollback() • db.close()
  • 178.
    Performing Transactions • Transactionsare a mechanism that ensure data consistency. Transactions have the following four properties- • Atomicity: Either a transaction completes or nothing happens at all. • Consistency: A transaction must start in a consistent state and leave the system in a consistent state. • Isolation: Intermediate results of a transaction are not visible outside the current transaction. • Durability: Once a transaction was committed, the effects are persistent, even after a system failure.
  • 179.
    • The PythonDB API provides two methods to either commit or rollback a transaction. • # Prepare SQL query to DELETE required records • sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) • try: • # Execute the SQL command • cursor.execute(sql) • # Commit your changes in the database • db.commit() • except: • # Rollback in case there is any error • db.rollback()
  • 180.
    COMMIT Operation • Commitis an operation, which gives a green signal to the database to finalize the changes, and after this operation, no change can be reverted back. • Here is a simple example to call the commit method. • db.commit()
  • 181.
    ROLLBACK Operation • Ifyou are not satisfied with one or more of the changes and you want to revert back those changes completely, then use the rollback() method. • Here is a simple example to call the rollback() method. • db.rollback()
  • 182.
    Disconnecting Database • Todisconnect the Database connection, use the close() method. • db.close()
  • 183.
    Points to remember •The SQLite database system is included within Python as the sqlite3 standard library. • The connect() method establishes a connection to your database file. • The cursor() method lets you communicate with your database via an existing connection. • The execute() method lets you send an SQL query to your database via an existing cursor. • The commit() method makes changes to your database permanent. • The rollback() method cancels any pending changes to your data. • The close() method closes an existing connection to your database.
  • 184.
    Handling Errors • Thereare many sources of errors. • A few examples are a syntax error in an executed SQL statement, a connection failure, or calling the fetch method for an already cancelled or finished statement handle. • The DB API defines a number of errors that must exist in each database module.
  • 187.
  • 189.
    • Multiprocessing(can bemore than one processor)
  • 190.
    • Just likemultiprocessing, multithreading is a way of achieving multitasking. In multithreading, the concept of threads is used. Multiprocessing
  • 191.
    Thread • In computing,a process is an instance of a computer program that is being executed. • Any process has 3 basic components: 1. An executable program. 2. The associated data needed by the program (variables, work space, buffers, etc.) 3. The execution context of the program (State of process)
  • 192.
    • A threadis • an entity within a process that can be scheduled for execution. • smallest unit of processing that can be performed in an OS (Operating System). • a sequence of such instructions within a program that can be executed independently of other code. • a subset of a process!
  • 193.
    • Multithreading isdefined as the ability of a processor to execute multiple threads concurrently. • In a simple, single-core CPU, it is achieved using frequent switching between threads. • This is termed as context switching. • In context switching, the state of a thread is saved and state of another thread is loaded whenever any interrupt (due to I/O or manually set) takes place. • Context switching takes place so frequently that all the threads appear to be running parallel (this is termed as multitasking).
  • 194.
  • 195.
    The methods providedby the Thread class are as follows: 1. start(): The start() method starts a thread by calling the run method. 2. run(): The run() method is the entry point for a thread. 3. join([time]): The join() waits for threads to terminate. 4. isAlive(): The isAlive() method checks whether a thread is still executing. 5. getName(): The getName() method returns the name of a thread. 6. setName(): The setName() method sets the name of a thread.
  • 197.
    • To importthe threading module, we do: import threading
  • 198.
    • To createa new thread, we create an object of Thread class. • It takes following arguments: • target: the function to be executed by thread • args: the arguments to be passed to the target function • In above example, we created 2 threads with different target functions: t1 = Thread(target = print_square, args=(10,)) t2 = Thread(target = print_cube, args=(10,))
  • 199.
    • To starta thread, we use start method of Thread class. t1.start() t2.start()
  • 200.
    • Once thethreads start, the current program (you can think of it like a main thread) also keeps on executing. • In order to stop execution of current program until a thread is complete, we use join method. t1.join() t2.join() • As a result, the current program will first wait for the completion of t1 and then t2. • Once, they are finished, the remaining statements of current program are executed.
  • 202.
  • 204.
    Synchronization between threads •Thread synchronization is defined as a mechanism which ensures that • two or more concurrent threads do not simultaneously execute some particular program segment known as critical section. • Critical section refers to the parts of the program where the shared resource is accessed.
  • 205.
    • For example,in the diagram below, 3 threads try to access shared resource or critical section at the same time.
  • 206.
    • Concurrent accessesto shared resource can lead to race condition. • A race condition occurs when two or more threads can access shared data and they try to change it at the same time. • As a result, the values of variables may be unpredictable and vary depending on the timings of context switches of the processes.
  • 207.
  • 208.
    import threading # globalvariable x x = 0 def increment(): global x x += 1 def thread_task(): for i in range(100000): increment() def main_task(): global x # setting global variable x as 0 x = 0 # creating threads t1 = threading.Thread(target=thread_task) t2 = threading.Thread(target=thread_task) # start threads t1.start() t2.start() # wait until threads finish their job t1.join() t2.join()
  • 209.
    if __name__ =="__main__": for i in range(10): main_task() print("Iteration {0}: x = {1}".format(i,x))
  • 210.
    • Output: • Iteration0: x = 1477960 • Iteration 1: x = 1468219 • Iteration 2: x = 1497968 • Iteration 3: x = 1666032 • Iteration 4: x = 1581574 • Iteration 5: x = 1474136 • Iteration 6: x = 1539974 • Iteration 7: x = 1559577 • Iteration 8: x = 1575546 • Iteration 9: x = 1613644
  • 211.
    • In aboveprogram: • Two threads t1 and t2 are created in main_task function and global variable x is set to 0. • Each thread has a target function thread_task in which increment function is called 100000 times. • increment function will increment the global variable x by 1 in each call.
  • 212.
    • The expectedfinal value of x is 200000 but what we get in 10 iterations of main_task function is some different values. • This happens due to concurrent access of threads to the shared variable x. This unpredictability in value of x is nothing but race condition.
  • 213.
    Notice that expectedvalue of x in above diagram is 12 but due to race condition, it turns out to be 11!
  • 214.
    Using Locks • threadingmodule provides a Lock class to deal with the race conditions. • Lock is implemented using a Semaphore object provided by the Operating System.
  • 215.
    Semaphore • A semaphoreis a synchronization object that controls access by multiple processes/threads to a common resource in a parallel programming environment. • It is simply a value in a designated place in operating system (or kernel) storage that each process/thread can check and then change. • Depending on the value that is found, the process/thread can use the resource or will find that it is already in use and must wait for some period before trying again. • Semaphores can be binary (0 or 1) or can have additional values. • Typically, a process/thread using semaphores checks the value and then, if it using the resource, changes the value to reflect this so that subsequent semaphore users will know to wait.
  • 216.
    Lock() • A newlock is created by calling the Lock() method, which returns the new lock. • The acquire(blocking) method of the new lock object is used to force threads to run synchronously. • The optional blocking parameter enables you to control whether the thread waits to acquire the lock. • If blocking is set to 0, • the thread returns immediately with a 0 value if the lock cannot be acquired and • with a 1 if the lock was acquired. • If blocking is set to 1, the thread blocks and wait for the lock to be released.
  • 217.
    release() : Torelease a lock. • The release() method of the new lock object is used to release the lock when it is no longer required. • If lock is already unlocked, a ThreadError is raised.
  • 219.
    import threading # globalvariable x x = 0 def increment(): global x x += 1 def thread_task(lock): for i in range(100000): lock.acquire() increment() lock.release() def main_task(): global x x = 0 # creating a lock lock = threading.Lock() # creating threads t1 = threading.Thread(target=thread_task, args=(lock,)) t2 = threading.Thread(target=thread_task, args=(lock,)) # start threads t1.start() t2.start() # wait until threads finish their job t1.join() t2.join()
  • 220.
    if __name__ =="__main__": for i in range(10): main_task() print("Iteration {0}: x = {1}".format(i,x))
  • 221.
    Output: • Iteration 0:x = 200000 • Iteration 1: x = 200000 • Iteration 2: x = 200000 • Iteration 3: x = 200000 • Iteration 4: x = 200000 • Iteration 5: x = 200000 • Iteration 6: x = 200000 • Iteration 7: x = 200000 • Iteration 8: x = 200000 • Iteration 9: x = 200000
  • 222.
    • Firstly, aLock object is created using: lock = threading.Lock() • Then, lock is passed as target function argument: t1 = threading.Thread(target=thread_task, args=(lock,)) t2 = threading.Thread(target=thread_task, args=(lock,))
  • 223.
    • In thecritical section of target function, we apply lock using lock.acquire()method. • As soon as a lock is acquired, no other thread can access the critical section (here, increment function) until the lock is released using lock.release()method. lock.acquire() increment() lock.release() • As you can see in the results, the final value of x comes out to be 200000 every time (which is the expected final result).
  • 224.
    Advantages of Mutithreading •It doesn’t block the user. This is because threads are independent of each other. • Better use of system resources is possible since threads execute tasks parallely. • Enhanced performance on multi-processor machines. • Multi-threaded servers and interactive GUIs use multithreading exclusively.
  • 225.
    Disadvantages of multithreading •As number of threads increase, complexity increases. • Synchronization of shared resources (objects, data) is necessary. • It is difficult to debug, result is sometimes unpredictable. • Potential deadlocks which leads to starvation, i.e. some threads may not be served with a bad design • Constructing and synchronizing threads is CPU/memory intensive.
  • 226.