SlideShare a Scribd company logo
mathmetics
“math” module
mathmetics
• Provides functions for specialized mathematical
operations.
The following functions are provided by this module:
 Number-theoretic and representation functions
 Power and logarithmic function
 Trigonometric function
 Angular conversion
 Hyperbolic functions
 Special functions
 Constants
• math.ceil(x)
– Return the ceiling of x, the smallest integer
greater than or equal to x.
• Example:
• >>> math.ceil(12.3)
• 13
• math.floor(x)
– Return the floor of x, the largest integer less than
or equal to x.
• >>> math.floor(12.3)
• 12
• math.factorial(x)
– Return x factorial.
• >>> math.factorial(5)
• 120
• math.gcd(a, b)
– Return the greatest common divisor of the integers a
and b.
• >>> math.gcd(12,26)
• 2
• math.exp(x)
– Return e**x
• >>> math.exp(5)
• 148.4131591025766
• math.log(x, base)
– With one argument, return the natural logarithm of x (to base e).
– With two arguments, return the logarithm of x to the given base.
• >>> math.log(10,10)
• 1.0
• >>> math.log(10,3)
• 2.095903274289385
• math.pow(x, y)
– Return x raised to the power y.
• >>> math.pow(2,3)
• 8.0
• math.sqrt(x)
– Return the square root of x.
• >>> math.sqrt(16)
• 4.0
• math.cos(x)
– Return the cosine of x radians.
• math.sin(x)
– Return the sine of x radians.
• math.tan(x)
– Return the tangent of x radians.
• math.degrees(x)
– Convert angle x from radians to degrees.
• math.radians(x)
– Convert angle x from degrees to radians
• math.pi
– The mathematical constant p = 3.141592..., to
available precision.
• math.e
– The mathematical constant e = 2.718281..., to
available precision.
• math.inf
– A floating-point positive infinity. (For negative
infinity, use -math.inf)
assertion
• Assertion is a conditional statement.
• This is used to test an assumption in the program.
• It verifies a conditional statement must be always
TRUE.
• Syntax:
– assert True==fun(x)
– Here assert is keyword, fun() is user defined function, x is
argument
– fun(x) returns a Boolean value which is either True or
False.
– If the returned value is True continues, otherwise Raises an
AssertionError
Data compression
“zlib” module
Introduction
• The zlib module provides a lower-level interface
to many of the functions in the zlib compression
library from the GNU project.
• zlib.compress(data, level=-1)
– Compresses the bytes in data, returning a bytes object
containing compressed data.
– level is an integer from 0 to 9 or -1 controlling the
level of compression
– 1 is fastest and produces the least compression, 9 is
slowe
• zlib.decompress(data, wbits=MAX_WBITS,
bufsize=DEF_BUF_SIZE)
• Decompresses the bytes in data, returning a
bytes object containing the uncompressed
data.
• Example:
• a=b'i want to meet you on sunday'
• >>> a
• b'i want to meet you on sunday'
• >>> c=zlib.compress(a,level=2)
• >>> c
• b'x^xcbT(Oxcc+Q(xc9Wxc8MM-
Qxa8xcc/Uxc8xcfS(.xcdKIxacx04x00x90xbfn@'
• p=zlib.decompress(c)
• >>> p
• b'i want to meet you on sunday'
• zlib.adler32(data, value)
– Computes an Adler-32 checksum of data. (An Adler-32
checksum is almost as reliable as a CRC32 but can be
computed much more quickly.) The result is an
unsigned 32-bit integer.
– If value is present, it is used as the starting value of
the checksum; otherwise, a default value of 1 is used.
– Passing in value allows computing a running checksum
over the concatenation of several inputs.
– cr=zlib.adler32(a,12)
– >>> cr
– 2448624203
• zlib.crc32(data, value)
– Computes a CRC (Cyclic Redundancy Check) checksum
of data.
– The result is an unsigned 32-bit integer.
– If value is present, it is used as the starting value of
the checksum; otherwise, a default value of 0 is used.
– Passing in value allows computing a running checksum
over the concatenation of several inputs.
– cr=zlib.crc32(a,12)
– >>> cr
– 1321257511
multithreading
thread module
Introduction
• The program in execution is called “process”
• Executing the number of processes
simultaneously or concurrently is called “multi
processing”
• The light-weight process is called “Thread”
• Executing the number of threads is called
“multi threading”
• A process can be divided into several threads,
each will do a specific task.
• Multiple threads within a process share same
address space, and hence share information and
communicate more easily.
• The threads can communicate with each other
which is called “Inter thread communication”
• These are cheaper than processes.
• The Thread has three things:
– It has beginning
– It has an execution sequence
– It has conclusion
• A thread can be pre-empted ( interrupted)
• It can be temporarily put on hold, while other
threads are running. This is called “Yielding”.
• To start new thread we call the following
method in Python 2.7*
– thread.start_new_thread(function, arguments)
Creating the New thread
• import time
• import thread
• def print_time(threadname,delay): #function definition
• count=0
• while count<5:
• time.sleep(delay)
• count+=1
• print "The thread name is:",threadname,"Delay is:",delay,"n"
• #creating the thread
• try:
• thread.start_new_thread(print_time,("Thread-1",2))
• thread.start_new_thread(print_time,("Thread-2",4))
• except:
• print "There is some error in threading"
output
• >>> The thread name is: Thread-1 Delay is: 2
• The thread name is: Thread-2 Delay is: 4
• The thread name is: Thread-1 Delay is: 2
• The thread name is: Thread-1 Delay is: 2
• The thread name is: Thread-2 Delay is: 4
• The thread name is: Thread-1 Delay is: 2
• The thread name is: Thread-1 Delay is: 2
• The thread name is: Thread-2 Delay is: 4
• The thread name is: Thread-2 Delay is: 4
• The thread name is: Thread-2 Delay is: 4
The threading module
• The newer threading module included with Python 2.4
provides much more powerful, high-level support for
threads than the thread module discussed in the
previous section.
• The threading module exposes all the methods of the
thread module and provides some additional methods:
– threading.activeCount(): Returns the number of thread
objects that are active.
– threading.currentThread(): Returns the number of thread
objects in the caller's thread control.
– threading.enumerate(): Returns a list of all thread objects
that are currently active.
Thread class methods
• In addition to the methods, the threading module has
the Thread class that implements threading. The
methods provided by the Thread class are as follows:
– run(): The run() method is the entry point for a thread.
– start(): The start() method starts a thread by calling the
run method.
– join([time]): The join() waits for threads to terminate.
– isAlive(): The isAlive() method checks whether a thread is
still executing.
– getName(): The getName() method returns the name of a
thread.
– setName(): The setName() method sets the name of a
thread.
Creating Thread Using Threading Module
• To implement a new thread using the
threading module, you have to do the
following −
– Define a new subclass of the Thread class.
– Override the __init__(self [,args]) method to add
additional arguments.
– Then, override the run(self [,args]) method to
implement what the thread should do when
started.
• import time
• import threading
• #defining class
• class MyThread(threading.Thread):
• def __init__(self,threadID,n,c):
• threading.Thread.__init__(self)
• self.tID=threadID
• self.name=n
• self.counter=c
• def run(self): #entry point to the thread
• for a in range(1,11):
• print("name",self.name,"ID is:",self.tID," :5 * ",a,"=",5*a,"n")
• time.sleep(self.counter) #used to delay
• #creating the threads
• t1=MyThread(1,"Guido Thread",2)
• t2=MyThread(2,"Steev Jobs",4)
• #starting the threads
• t1.start()
• t2.start()
To know the information of the current
threads
• print("The name of the First Thread is",t1.getName(),"n")
• print("The Name of the Second Thread is",t2.getName(),"n")
• print("The Name of the Second Thread is",t3.getName(),"n")
• print("The active threads are :",threading.activeCount())
• print("The list of threads is:",threading.enumerate())
Output
• The active threads are : 5
• The list of threads is:
[<_MainThread(MainThread, started 2544)>,
<Thread(SockThread, started daemon 3552)>,
<MyThread(Guido Thread, started 2560)>,
<MyThread(Steev Jobs, started 2088)>,
<MyThread(KSR Thread, started 1488)>]
Synchronizing Threads
• The threading module provided with Python includes a simple-to-
implement locking mechanism that allows you to synchronize
threads.
• 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.
• The release() method of the new lock object is used to release the
lock when it is no longer required.
Testing
• Unit Test
– This test is invented by Eric Gamma in 1977
– This test is used to test the individual units or
functions of the program.
– The function may be defined to do some specific
task.
– This functions is tested for all possible value, for
knowing where it is performing its intended task
correctly or not
Goals of Unit Test
• There are three goals of unittest
– To make it easy to write tests
– To make it easy to run tests
– To make it easy to tell if the tests are passed
How many tests should we have
• Test at least one “typical” case.
• The objective of the testing is not to prove that your
program is working.
• It is to try to find out where it does not test “Extreme” case
you can think.
• For example you want to write test case for “sort()”
function over a list.
• Then some issues you can consider are:
– What if the list contains equal number?
– Do the first element and last element moved to the correct
position?
– Can you sort a 1-element list without getting an error?
– How about an empty list?
unittest module
• In Python we have a module called “unittest”
to support unit test.
• to use these tests we create a class that
Inherits the properties from “TestCase” class
of this module.
• Define a method in this class and use the
different methods of the TestCase class.
Commonly used methods of TestCase class
assertEqual(a,b)
assertNotEqual(a,b)
assertTrue(x)
assertFalse(x)
assertIs(a,b)
assertIsNone(a,b)
assertIn(a,b)
assertNotIn(a,b)

More Related Content

PPTX
Python 3.6 Features 20161207
PPTX
Java 102
PPTX
Java 103
PPTX
Java 101
PPTX
06 Java Language And OOP Part VI
PPTX
Python programming –part 7
PPTX
Python programming- Part IV(Functions)
PPTX
Introduction to python programming 2
Python 3.6 Features 20161207
Java 102
Java 103
Java 101
06 Java Language And OOP Part VI
Python programming –part 7
Python programming- Part IV(Functions)
Introduction to python programming 2

What's hot (20)

PPTX
Introduction to python programming 1
PPTX
05 Java Language And OOP Part V
PPT
Python multithreading session 9 - shanmugam
PPTX
Java Foundations: Lists, ArrayList<T>
PPTX
Python programming –part 3
PDF
Machine Learning Live
PDF
The Ring programming language version 1.2 book - Part 78 of 84
PPTX
Concurrency with java
DOCX
Fundamental classes in java
PPT
Chapter 2 Java Methods
PDF
Nx tutorial basics
PDF
Java Day-5
PDF
Java programming-examples
PPTX
Java Foundations: Maps, Lambda and Stream API
PPTX
04 Java Language And OOP Part IV
PDF
Python Modules
ODP
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
PDF
The Ring programming language version 1.4.1 book - Part 29 of 31
PDF
From Lisp to Clojure/Incanter and RAn Introduction
PDF
Python programming : Threads
Introduction to python programming 1
05 Java Language And OOP Part V
Python multithreading session 9 - shanmugam
Java Foundations: Lists, ArrayList<T>
Python programming –part 3
Machine Learning Live
The Ring programming language version 1.2 book - Part 78 of 84
Concurrency with java
Fundamental classes in java
Chapter 2 Java Methods
Nx tutorial basics
Java Day-5
Java programming-examples
Java Foundations: Maps, Lambda and Stream API
04 Java Language And OOP Part IV
Python Modules
Java 5 6 Generics, Concurrency, Garbage Collection, Tuning
The Ring programming language version 1.4.1 book - Part 29 of 31
From Lisp to Clojure/Incanter and RAn Introduction
Python programming : Threads
Ad

Similar to Mathemetics module (20)

PPTX
Generators & Decorators.pptx
PDF
Python multithreaded programming
PPTX
Python UNIT-IV Multi Threading B.Tech CSE
PDF
25 must know python for Interview by Tutort Academy
PPTX
Standard Libraries in Python Programming
PDF
concurrency
PPT
Pythonintroduction
PPTX
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
PPTX
Advanced Programming (DS40108)_ Working with File Paths and Directories in P...
PDF
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
PDF
Python Advanced – Building on the foundation
PPT
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
PDF
Python Programming unit5 (1).pdf
PPTX
An Introduction : Python
ODP
Python internals and how they affect your code - kasra ahmadvand
PPT
PYTHON
PDF
Presentation pythonpppppppppppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyytttttt...
Generators & Decorators.pptx
Python multithreaded programming
Python UNIT-IV Multi Threading B.Tech CSE
25 must know python for Interview by Tutort Academy
Standard Libraries in Python Programming
concurrency
Pythonintroduction
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
Advanced Programming (DS40108)_ Working with File Paths and Directories in P...
Q-Step_WS_02102019_Practical_introduction_to_Python.pdf
Python Advanced – Building on the foundation
FALLSEM2022-23_ITA3007_ETH_VL2022230100613_Reference_Material_I_23-09-2022_py...
Python Programming unit5 (1).pdf
An Introduction : Python
Python internals and how they affect your code - kasra ahmadvand
PYTHON
Presentation pythonpppppppppppppppppppppppppppppppppyyyyyyyyyyyyyyyyyyytttttt...
Ad

More from manikanta361 (6)

PPT
Loops and functions in r
PPT
Inroduction to r
PPTX
Gui programming
PPT
Number theory
PPT
Graph theory
PPT
Set theory
Loops and functions in r
Inroduction to r
Gui programming
Number theory
Graph theory
Set theory

Recently uploaded (20)

PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
Soil Improvement Techniques Note - Rabbi
PPTX
Feature types and data preprocessing steps
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPT
Occupational Health and Safety Management System
PPTX
Management Information system : MIS-e-Business Systems.pptx
PDF
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
PPTX
Artificial Intelligence
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PDF
737-MAX_SRG.pdf student reference guides
Exploratory_Data_Analysis_Fundamentals.pdf
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Fundamentals of Mechanical Engineering.pptx
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
Categorization of Factors Affecting Classification Algorithms Selection
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
Soil Improvement Techniques Note - Rabbi
Feature types and data preprocessing steps
Automation-in-Manufacturing-Chapter-Introduction.pdf
Fundamentals of safety and accident prevention -final (1).pptx
Occupational Health and Safety Management System
Management Information system : MIS-e-Business Systems.pptx
Accra-Kumasi Expressway - Prefeasibility Report Volume 1 of 7.11.2018.pdf
Artificial Intelligence
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Nature of X-rays, X- Ray Equipment, Fluoroscopy
737-MAX_SRG.pdf student reference guides

Mathemetics module

  • 2. mathmetics • Provides functions for specialized mathematical operations. The following functions are provided by this module:  Number-theoretic and representation functions  Power and logarithmic function  Trigonometric function  Angular conversion  Hyperbolic functions  Special functions  Constants
  • 3. • math.ceil(x) – Return the ceiling of x, the smallest integer greater than or equal to x. • Example: • >>> math.ceil(12.3) • 13
  • 4. • math.floor(x) – Return the floor of x, the largest integer less than or equal to x. • >>> math.floor(12.3) • 12
  • 5. • math.factorial(x) – Return x factorial. • >>> math.factorial(5) • 120 • math.gcd(a, b) – Return the greatest common divisor of the integers a and b. • >>> math.gcd(12,26) • 2
  • 6. • math.exp(x) – Return e**x • >>> math.exp(5) • 148.4131591025766 • math.log(x, base) – With one argument, return the natural logarithm of x (to base e). – With two arguments, return the logarithm of x to the given base. • >>> math.log(10,10) • 1.0 • >>> math.log(10,3) • 2.095903274289385
  • 7. • math.pow(x, y) – Return x raised to the power y. • >>> math.pow(2,3) • 8.0 • math.sqrt(x) – Return the square root of x. • >>> math.sqrt(16) • 4.0
  • 8. • math.cos(x) – Return the cosine of x radians. • math.sin(x) – Return the sine of x radians. • math.tan(x) – Return the tangent of x radians. • math.degrees(x) – Convert angle x from radians to degrees. • math.radians(x) – Convert angle x from degrees to radians
  • 9. • math.pi – The mathematical constant p = 3.141592..., to available precision. • math.e – The mathematical constant e = 2.718281..., to available precision. • math.inf – A floating-point positive infinity. (For negative infinity, use -math.inf)
  • 10. assertion • Assertion is a conditional statement. • This is used to test an assumption in the program. • It verifies a conditional statement must be always TRUE. • Syntax: – assert True==fun(x) – Here assert is keyword, fun() is user defined function, x is argument – fun(x) returns a Boolean value which is either True or False. – If the returned value is True continues, otherwise Raises an AssertionError
  • 12. Introduction • The zlib module provides a lower-level interface to many of the functions in the zlib compression library from the GNU project. • zlib.compress(data, level=-1) – Compresses the bytes in data, returning a bytes object containing compressed data. – level is an integer from 0 to 9 or -1 controlling the level of compression – 1 is fastest and produces the least compression, 9 is slowe
  • 13. • zlib.decompress(data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE) • Decompresses the bytes in data, returning a bytes object containing the uncompressed data.
  • 14. • Example: • a=b'i want to meet you on sunday' • >>> a • b'i want to meet you on sunday' • >>> c=zlib.compress(a,level=2) • >>> c • b'x^xcbT(Oxcc+Q(xc9Wxc8MM- Qxa8xcc/Uxc8xcfS(.xcdKIxacx04x00x90xbfn@' • p=zlib.decompress(c) • >>> p • b'i want to meet you on sunday'
  • 15. • zlib.adler32(data, value) – Computes an Adler-32 checksum of data. (An Adler-32 checksum is almost as reliable as a CRC32 but can be computed much more quickly.) The result is an unsigned 32-bit integer. – If value is present, it is used as the starting value of the checksum; otherwise, a default value of 1 is used. – Passing in value allows computing a running checksum over the concatenation of several inputs. – cr=zlib.adler32(a,12) – >>> cr – 2448624203
  • 16. • zlib.crc32(data, value) – Computes a CRC (Cyclic Redundancy Check) checksum of data. – The result is an unsigned 32-bit integer. – If value is present, it is used as the starting value of the checksum; otherwise, a default value of 0 is used. – Passing in value allows computing a running checksum over the concatenation of several inputs. – cr=zlib.crc32(a,12) – >>> cr – 1321257511
  • 18. Introduction • The program in execution is called “process” • Executing the number of processes simultaneously or concurrently is called “multi processing” • The light-weight process is called “Thread” • Executing the number of threads is called “multi threading” • A process can be divided into several threads, each will do a specific task.
  • 19. • Multiple threads within a process share same address space, and hence share information and communicate more easily. • The threads can communicate with each other which is called “Inter thread communication” • These are cheaper than processes. • The Thread has three things: – It has beginning – It has an execution sequence – It has conclusion
  • 20. • A thread can be pre-empted ( interrupted) • It can be temporarily put on hold, while other threads are running. This is called “Yielding”. • To start new thread we call the following method in Python 2.7* – thread.start_new_thread(function, arguments)
  • 21. Creating the New thread • import time • import thread • def print_time(threadname,delay): #function definition • count=0 • while count<5: • time.sleep(delay) • count+=1 • print "The thread name is:",threadname,"Delay is:",delay,"n" • #creating the thread • try: • thread.start_new_thread(print_time,("Thread-1",2)) • thread.start_new_thread(print_time,("Thread-2",4)) • except: • print "There is some error in threading"
  • 22. output • >>> The thread name is: Thread-1 Delay is: 2 • The thread name is: Thread-2 Delay is: 4 • The thread name is: Thread-1 Delay is: 2 • The thread name is: Thread-1 Delay is: 2 • The thread name is: Thread-2 Delay is: 4 • The thread name is: Thread-1 Delay is: 2 • The thread name is: Thread-1 Delay is: 2 • The thread name is: Thread-2 Delay is: 4 • The thread name is: Thread-2 Delay is: 4 • The thread name is: Thread-2 Delay is: 4
  • 23. The threading module • The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module discussed in the previous section. • The threading module exposes all the methods of the thread module and provides some additional methods: – threading.activeCount(): Returns the number of thread objects that are active. – threading.currentThread(): Returns the number of thread objects in the caller's thread control. – threading.enumerate(): Returns a list of all thread objects that are currently active.
  • 24. Thread class methods • In addition to the methods, the threading module has the Thread class that implements threading. The methods provided by the Thread class are as follows: – run(): The run() method is the entry point for a thread. – start(): The start() method starts a thread by calling the run method. – join([time]): The join() waits for threads to terminate. – isAlive(): The isAlive() method checks whether a thread is still executing. – getName(): The getName() method returns the name of a thread. – setName(): The setName() method sets the name of a thread.
  • 25. Creating Thread Using Threading Module • To implement a new thread using the threading module, you have to do the following − – Define a new subclass of the Thread class. – Override the __init__(self [,args]) method to add additional arguments. – Then, override the run(self [,args]) method to implement what the thread should do when started.
  • 26. • import time • import threading • #defining class • class MyThread(threading.Thread): • def __init__(self,threadID,n,c): • threading.Thread.__init__(self) • self.tID=threadID • self.name=n • self.counter=c • def run(self): #entry point to the thread • for a in range(1,11): • print("name",self.name,"ID is:",self.tID," :5 * ",a,"=",5*a,"n") • time.sleep(self.counter) #used to delay • #creating the threads • t1=MyThread(1,"Guido Thread",2) • t2=MyThread(2,"Steev Jobs",4) • #starting the threads • t1.start() • t2.start()
  • 27. To know the information of the current threads • print("The name of the First Thread is",t1.getName(),"n") • print("The Name of the Second Thread is",t2.getName(),"n") • print("The Name of the Second Thread is",t3.getName(),"n") • print("The active threads are :",threading.activeCount()) • print("The list of threads is:",threading.enumerate())
  • 28. Output • The active threads are : 5 • The list of threads is: [<_MainThread(MainThread, started 2544)>, <Thread(SockThread, started daemon 3552)>, <MyThread(Guido Thread, started 2560)>, <MyThread(Steev Jobs, started 2088)>, <MyThread(KSR Thread, started 1488)>]
  • 29. Synchronizing Threads • The threading module provided with Python includes a simple-to- implement locking mechanism that allows you to synchronize threads. • 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. • The release() method of the new lock object is used to release the lock when it is no longer required.
  • 30. Testing • Unit Test – This test is invented by Eric Gamma in 1977 – This test is used to test the individual units or functions of the program. – The function may be defined to do some specific task. – This functions is tested for all possible value, for knowing where it is performing its intended task correctly or not
  • 31. Goals of Unit Test • There are three goals of unittest – To make it easy to write tests – To make it easy to run tests – To make it easy to tell if the tests are passed
  • 32. How many tests should we have • Test at least one “typical” case. • The objective of the testing is not to prove that your program is working. • It is to try to find out where it does not test “Extreme” case you can think. • For example you want to write test case for “sort()” function over a list. • Then some issues you can consider are: – What if the list contains equal number? – Do the first element and last element moved to the correct position? – Can you sort a 1-element list without getting an error? – How about an empty list?
  • 33. unittest module • In Python we have a module called “unittest” to support unit test. • to use these tests we create a class that Inherits the properties from “TestCase” class of this module. • Define a method in this class and use the different methods of the TestCase class.
  • 34. Commonly used methods of TestCase class assertEqual(a,b) assertNotEqual(a,b) assertTrue(x) assertFalse(x) assertIs(a,b) assertIsNone(a,b) assertIn(a,b) assertNotIn(a,b)