Python Notes - 4
Python Notes - 4
Output:
The Factorial of 5 is: 120
The Factorial of 9 is: 362880
In the above program instead of RLock if we use normal Lock then the thread will be blocked.
Lock:
1. Lock object can be acquired by only one thread at a time.Even owner thread also cannot acquire
multiple times.
2. Not suitable to execute recursive functions and nested access calls
3. In this case Lock object will takes care only Locked or unlocked and it never takes care
about owner thread and recursion level.
RLock:
1. RLock object can be acquired by only one thread at a time, but owner thread can acquire same
lock object multiple times.
2. Best suitable to execute recursive functions and nested access calls
3. In this case RLock object will takes care whether Locked or unlocked and owner
thread information, recursiion level.
Sometimes our requirement is at a time a particular number of threads are allowed to access(like
at a time 10 memebers are allowed to access database server,4 members are allowed to access
Network connection etc).To handle this requirement we cannot use Lock and RLock concepts and
we should go for Semaphore concept.
Semaphore can be used to limit the access to the shared resources with limited capacity.
s=Semaphore(counter)
Here counter represents the maximum number of threads are allowed to access simultaneously.
The default value of counter is 1.
Whenever thread executes acquire() method,then the counter value will be decremented by 1 and
if thread executes release() method then the counter value will be incremented by 1.
i.e for every acquire() call counter value will be decremented and for every release() call counter
value will be incremented.
Case-1: s=Semaphore()
In this case counter value is 1 and at a time only one thread is allowed to access. It is exactly same
as Lock concept.
Case-2: s=Semaphore(3)
In this case Semaphore object can be accessed by 3 threads at a time.The remaining threads have
to wait until releasing the semaphore.
Eg:
In the above program at a time 2 threads are allowed to access semaphore and hence 2 threads
are allowed to execute wish() function.
BoundedSemaphore:
Normal Semaphore is an unlimited semaphore which allows us to call release() method any
number of times to increment counter.The number of release() calls can exceed the number of
acquire() calls also.
Eg:
It is valid because in normal semaphore we can call release() any number of times.
BounedSemaphore is exactly same as Semaphore except that the number of release() calls should
not exceed the number of acquire() calls,otherwise we will get
Eg:
Conclusion:
The main advantage of synchronization is we can overcome data inconsistency problems.But the
main disadvantage of synchronization is it increases waiting time of threads and creates
performance problems. Hence if there is no specific requirement then it is not recommended to
use synchronization.
Eg: After producing items Producer thread has to communicate with Consumer thread to notify
about new item.Then consumer thread can consume that new item.
1. Event
2. Condition
3. Queue
etc
event = threading.Event()
2. clear() inernal flag value will become False and it represents RED signal for all
waiting threads.
3. isSet() This method can be used whether the event is set or not
4. wait()|wait(seconds) Thread can wait until event is set
Pseudo Code:
event = threading.Event()
Demo Program-1:
Output:
Consumer thread is waiting for updation
Producer thread producing items
Producer thread giving notification by setting event
Consumer thread got notification and consuming items
Demo Program-2:
In the above program driver thread has to wait until Trafficpolice thread sets event.ie until giving
GREEN signal.Once Traffic police thread sets event(giving GREEN signal),vehicles can cross the
signal.Once traffic police thread clears event (giving RED Signal)then the driver thread has to wait.
Methods of Condition:
1. acquire() To acquire Condition object before producing or consuming items.i.e
thread acquiring internal lock.
2. release() To release Condition object after producing or consuming items. i.e thread
releases internal lock
Case Study:
The producing thread needs to acquire the Condition before producing item to the resource and
notifying the consumers.
#Producer Thread
...generate item..
condition.acquire()
...add item to the resource...
condition.notify()#signal that a new item is available(notifyAll())
condition.release()
The Consumer must acquire the Condition and then it can consume items from the resource
#Consumer Thread
condition.acquire()
condition.wait()
consume item
condition.release()
Demo Program-1:
Demo Program-2:
Output:
Consumer waiting for updation
Producer Producing Item: 49
Producer giving Notification
Consumer consumed the item 49
.....
In the above program Consumer thread expecting updation and hence it is responsible to call
wait() method on Condition object.
Producer thread performing updation and hence it is responsible to call notify() or notifyAll() on
Condition object.
Interthread communication by using Queue:
Queues Concept is the most enhanced Mechanism for interthread communication and to share
data between threads.
Queue internally has Condition and that Condition has Lock.Hence whenever we are using Queue
we are not required to worry about Synchronization.
import queue
q = queue.Queue()
Producer Thread uses put() method to insert data in the queue. Internally this method has logic to
acquire the lock before inserting data into queue. After inserting data lock will be released
automatically.
put() method also checks whether the queue is full or not and if queue is full then the Producer
thread will entered in to waiting state by calling wait() method internally.
Consumer Thread uses get() method to remove and get data from the queue. Internally this
method has logic to acquire the lock before removing data from the queue.Once removal
completed then the lock will be released automatically.
If the queue is empty then consumer thread will entered into waiting state by calling wait()
method internally.Once queue updated with data then the thread will be notified automatically.
Note:
The queue module takes care of locking for us which is a great advantage.
Eg:
Output:
Consumer waiting for updation
Producer Producing Item: 58
Producer giving Notification
Consumer consumed the item: 58
Types of Queues:
Python Supports 3 Types of Queues.
1. FIFO Queue:
q = queue.Queue()
This is Default Behaviour. In which order we put items in the queue, in the same order the items
will come out (FIFO-First In First Out).
Eg:
1) import queue
2) q=queue.Queue()
3) q.put(10)
4) q.put(5)
5) q.put(20)
6) q.put(15)
7) while not q.empty():
8) print(q.get(),end=' ')
Output: 10 5 20 15
2. LIFO Queue:
The removal will be happend in the reverse order of insertion(Last In First Out)
Eg:
1) import queue
2) q=queue.LifoQueue()
3) q.put(10)
4) q.put(5)
5) q.put(20)
6) q.put(15)
7) while not q.empty():
8) print(q.get(),end=' ')
Output: 15 20 5 10
3. Priority Queue:
The elements will be inserted based on some priority order.
1) import queue
2) q=queue.PriorityQueue()
3) q.put(10)
4) q.put(5)
5) q.put(20)
6) q.put(15)
7) while not q.empty():
8) print(q.get(),end=' ')
Output: 5 10 15 20
Eg 2: If the data is non-numeric, then we have to provide our data in the form of tuple.
(x,y)
x is priority
y is our element
1) import queue
2) q=queue.PriorityQueue()
3) q.put((1,"AAA"))
4) q.put((3,"CCC"))
5) q.put((2,"BBB"))
6) q.put((4,"DDD"))
7) while not q.empty():
8) print(q.get()[1],end=' ')
Output: AAA BBB CCC DDD
It is highly recommended to write code of releasing locks inside finally block.The advantage is lock
will be released always whether exception raised or not raised and whether handled or not
handled.
l=threading.Lock()
l.acquire()
try:
perform required safe operations
finally:
l.release()
Demo Program:
Case-2:
It is highly recommended to acquire lock by using with statement. The main advantage of with
statement is the lock will be released automatically once control reaches end of with block and we
are not required to release explicitly.
with open('demo.txt','w') as f:
f.write("Hello...")
lock=threading.Lock()
with lock:
perform required safe operations
lock will be released
automatically
Demo Program:
Note: We can use with statement in multithreading for the following cases:
1. Lock
2. RLock
3. Semaphore
4. Condition
Python Database Programming
Storage Areas
As the Part of our Applications, we required to store our Data like Customers Information, Billing
Information, Calls Information etc..
To store this Data, we required Storage Areas. There are 2 types of Storage Areas.
These are the Memory Areas where Data will be stored temporarily.
Eg: Python objects like List, Tuple, Dictionary.
Once Python program completes its execution then these objects will be destroyed automatically
and data will be lost.
Also known as Persistent Storage Areas. Here we can store Data permanently.
Eg: File Systems, Databases, Data warehouses, Big Data Technologies etc
File Systems:
File Systems can be provided by Local operating System. File Systems are best suitable to store
very less Amount of Information.
Limitations:
1) We cannot store huge Amount of Information.
2) There is no Query Language support and hence operations will become very complex.
3) There is no Security for Data.
4) There is no Mechanism to prevent duplicate Data. Hence there may be a chance of Data
Inconsistency Problems.
Databases:
1) We can store Huge Amount of Information in the Databases.
2) Query Language Support is available for every Database and hence we can perform Database
Operations very easily.
3) To access Data present in the Database, compulsory username and pwd must be required.
Hence Data is secured.
4) Inside Database Data will be stored in the form of Tables. While developing Database Table
Schemas, Database Admin follow various Normalization Techniques and can implement various
Constraints like Unique Key Constrains, Primary Key Constraints etc which prevent Data
Duplication. Hence there is no chance of Data Inconsistency Problems.
Limitations of Databases:
1) Database cannot hold very Huge Amount of Information like Terabytes of Data.
2) Database can provide support only for Structured Data (Tabular Data OR Relational Data) and
cannot provide support for Semi Structured Data (like XML Files) and Unstructured Data (like
Video Files, Audio Files, Images etc)
To overcome these Problems we should go for more Advanced Storage Areas like Big Data
Technologies, Data warehouses etc.
We can use SQL Language to talk to the database and we can use Python to send those SQL
commands to the database.
Python provides inbuilt support for several databases like Oracle, MySql, SqlServer, GadFly, sqlite,
etc.
Python has seperate module for each database.
Eg: cx_Oralce module for communicating with Oracle database
pymssql module for communicating with Microsoft Sql Server
Eg: con=cx_Oracle.connect('scott/tiger@localhost')
3. To execute our sql queries and to hold results some special object is required, which is nothing
but Cursor object. We can create Cursor object by using cursor() method.
cursor=con.cursor()
4. Execute SQL Queries By using Cursor object. For this we can use the following methods
6. Fetch the result from the Cursor object in the case of select queries
fetchone() To fetch only one row
fetchall() To fetch all rows and it returns a list of rows
fecthmany(n) To fetch first n rows
Eg 1: data =cursor.fetchone()
print(data)
Eg 2: data=cursor.fetchall()
for row in data:
print(row)
cursor.close()
con.close()
Note: The following is the list of all important methods which can be used for python database
programming.
connect()
cursor()
execute()
executescript()
executemany()
commit()
rollback()
fetchone()
fetchall()
fetchmany(n)
fetch
close()
These methods won't be changed from database to database and same for all databases.
Diagram
Installing cx_Oracle:
From Normal Command Prompt (But not from Python console)execute the following command
Collecting cx_Oracle
Downloading cx_Oracle-6.0.2-cp36-cp36m-win32.whl (100kB)
100% |----------| 102kB 256kB/s
Installing collected packages: cx-Oracle
Successfully installed cx-Oracle-6.0.2
>>> help("modules")
....
_multiprocessing crypt ntpath timeit
_opcode csv nturl2path tkinter
_operator csvr numbers token
_osx_support csvw opcode tokenize
_overlapped ctypes operator trace
_pickle curses optparse traceback
_pydecimal custexcept os tracemalloc
_pyio cx_Oracle parser try
_random data pathlib tty
_sha1 datetime pdb turtle
_sha256 dbm pick turtledemo
_sha3 decimal pickle types
_sha512 demo pickletools typing
_signal difflib pip unicodedata
_sitebuiltins dis pipes unittest
_socket distutils pkg_resources unpick
_sqlite3 doctest pkgutil update
_sre dummy_threading platform urllib
_ssl Digitmath plistlib uu
_stat easy_install polymorph uuid
.....
App1: Program to connect with Oracle database and print its version.
1) import cx_Oracle
2) con=cx_Oracle.connect('scott/tiger@localhost')
3) print(con.version)
4) con.close()
Output:
D:\python_classes>py db1.py
11.2.0.2.0
1)import cx_Oracle
2)try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5) cursor.execute("create table employees(eno number,ename varchar2(10),esal number(10,2),eaddr varchar2(10))")
6) print("Table created successfully")
7)except cx_Oracle.DatabaseError as e:
8) if con:
9)con.rollback()
10) print("There is a problem with sql",e)
12) if cursor:
11) finally:
14) if con:
15) con.close()
13)cursor.close()
7)except cx_Oracle.DatabaseError
8) if con: as e:
7)print("Record
8)except Inserted Successfully")
cx_Oracle.DatabaseError as e:
10) con.rollback()
9)if con:
12) finally:
11)print("There is a problem with sql",e)
14) cursor.close()
Note:15)if
Whilecon:
performing DML Operations (insert|update|delte), compulsory we have to use
commit() method,then only the results will be reflected in the database.
8) (400,'Bunny',4000,'Hyd')]
11)print("Records Inserted Successfully")
10) con.commit()
13)if con:
12) except cx_Oracle.DatabaseError as e:
14) con.rollback()
15)print("There is a problem with sql",e)
16) finally:
17)if cursor:
18) cursor.close()
19)if con:
20) con.close()
App5: Write a program to insert multiple rows in the employees table with
dynamic input from the keyboard?
1)import cx_Oracle
2)try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5)while True:
6) eno=int(input("Enter Employee Number:"))
7) ename=input("Enter Employee Name:")
8) esal=float(input("Enter Employee Salary:"))
9)eaddr=input("Enter Employee Address:")
10) sql="insert into employees values(%d,'%s',%f,'%s')"
11)cursor.execute(sql
12) %(eno,ename,esal,eaddr))
print("Record Inserted Successfully")
14) if option=="No":
13)option=input("Do you want to insert one more record[Yes|No] :")
16) break
15)con.commit()
18) if con:
20)
17) print("There is a problem
except cx_Oracle.DatabaseError with sql :",e)
as e:
22) if cursor:
19)con.rollback()
24) if con:
25) con.close()
21) finally:
23)cursor.close()
App6: Write a program to update employee salaries with increment for the
certain range with dynamic input.
Eg: Increment all employee salaries by 500 whose salary < 5000
1)import cx_Oracle
2)try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5)increment=float(input("Enter Increment Salary:"))
6) salrange=float(input("Enter Salary Range:"))
7)sql="update
8) employees set esal=esal+%f
cursor.execute(sql where esal<%f"
%(increment,salrange))
10) con.commit()
9)print("Records Updated Successfully")
12) if con:
11) except cx_Oracle.DatabaseError as e:
14) print("There is a problem with sql :",e)
16) if cursor:
13)con.rollback()
18) if con:
15)
19) finally: con.close()
17)cursor.close()
7)cursor.execute(sql
8) %(cutoffsalary))
print("Records Deleted Successfully")
16) cursor.close()
13)print("There is a problem with sql :",e)
18) con.close()
15)if cursor:
17)if con:
7)while
8) row is not None:
print(row)
16) cursor.close()
13)print("There is a problem with sql :",e)
18) con.close()
15)if cursor:
17)if con:
App9: Write a program to select all employees info by using fetchall()
method?
1)import cx_Oracle
2)try:
3) con=cx_Oracle.connect('scott/tiger@localhost')
4) cursor=con.cursor()
5)cursor.execute("select * from employees")
6) data=cursor.fetchall()
16)
13)print() con.rollback()
18) finally:
15)if con:
20) cursor.close()
17)print("There is a problem with sql :",e)
22) con.close()
19)if cursor:
21)if con:
7)data=cursor.fetchmany(n)
8) for row in data:
16) cursor.close()
13)print("There is a problem with sql :",e)
18) con.close()
15)if cursor:
17)if con:
Output:
D:\python_classes>py test.py
Enter the number of required rows:3
(100, 'Digit', 1500.0, 'Hyd')
(200, 'Sunny', 2500.0, 'Mumbai')
(300, 'Chinny', 3500.0, 'Hyd')
D:\python_classes>py test.py
Enter the number of required rows:4
(100, 'Digit', 1500.0, 'Hyd')
(200, 'Sunny', 2500.0, 'Mumbai')
(300, 'Chinny', 3500.0, 'Hyd')
(400, 'Bunny', 4500.0, 'Hyd')
Note: In MySQL, everything we have to work with our own databases, which are also known as
Logical Databases.
Diagram
In the above diagram only one physical database is available and 4 logical databases are available.
5. To create a table:
create table employees(eno int(5) primary key,ename varchar(10),esal double(10,2),eaddr
varchar(10));
6. To insert data:
insert into employees values(100,'Digit',1000,'Hyd'); insert
into employees values(200,'Ravi',2000,'Mumbai');
Driver/Connector Information:
From Python program if we want to communicates with MySql database,compulsory some
translator is required to convert python specific calls into mysql database specific calls and mysql
database specific calls into python specific calls. This translator is nothing but Driver or Connector.
Diagram
https://dev.mysql.com/downloads/connector/python/2.1.html
Note: In the case of Python3.4 we have to set PATH and PYTHONPATH explicitly PATH=C:\
Python34
PYTHONPATH=C:\Python34\Lib\site-packages
Q. Write a Program to create table,insert data and display data by using mysql database.
1)import mysql.connector
2)try:
3) con=mysql.connector.connect(host='localhost',database='Digitdb',user='root',password='root')
4) cursor=con.cursor()
5)cursor.execute("create table employees(eno int(5) primary key,ename varchar(10),esal double(10,2),eaddr varchar( 10))")
6) print("Table Created...")
7)
8) sql = "insert into employees(eno, ename, esal, eaddr) VALUES(%s, %s, %s, %s)"
9)records=[(100,'Sachin',1000,'Mumbai'),
10) (200,'Dhoni',2000,'Ranchi'),
12) cursor.executemany(sql,records)
11)(300,'Kohli',3000,'Delhi')]
24)
23)print() print()
26) if con:
25) except mysql.connector.DatabaseError as e:
28) print("There is a problem with sql :",e)
27)con.rollback()
30) if cursor:
32) if con:
29) finally:
31)cursor.close()
33)con.close()
Q. Write a Program to copy data present in employees table of mysql database into Oracle
database.
1)import mysql.connector
2)import cx_Oracle
3)try:
4) con=mysql.connector.connect(host='localhost',database='Digitdb',user='root',password='root')
5) cursor=con.cursor()
6) cursor.execute("select * from employees")
7) data=cursor.fetchall()
8) list=[]
9) for row in data:
10) t=(row[0],row[1],row[2],row[3])
11) list.append(t)
12) except mysql.connector.DatabaseError as e:
13) if con:
14) con.rollback()
15) print("There is a problem with MySql :",e)
16) finally:
17) if cursor:
18) cursor.close()
19) if con:
20) con.close()
21)
22) try:
con=cx_Oracle.connect('scott/tiger@localhost')
23)
24) cursor=con.cursor()
25) sql="insert into employees values(:eno,:ename,:esal,:eaddr)"
26) cursor.executemany(sql,list)
27) con.commit()
28) print("Records Copied from MySQL Database to Oracle Database Successfully")
29) except cx_Oracle.DatabaseError as e:
30) if con:
31) con.rollback()
32) print("There is a problem with sql",e)
33) finally:
34) if cursor:
35)cursor.close()
36) if con:
37) con.close()
https://dev.mysql.com/downloads/connector/python/2.1.html