0% found this document useful (0 votes)
3 views22 pages

Python Unit-3

The document covers Regular Expressions (RegEx) in Python, explaining their usage, functions, and examples for searching, splitting, and substituting strings. It also discusses multithreaded programming, detailing the concepts of threads and processes, the Global Interpreter Lock, and how to manage threads using Python's threading and queue modules. Key functions and examples for both RegEx and multithreading are provided to illustrate their applications.

Uploaded by

koppusaiteja143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Python Unit-3

The document covers Regular Expressions (RegEx) in Python, explaining their usage, functions, and examples for searching, splitting, and substituting strings. It also discusses multithreaded programming, detailing the concepts of threads and processes, the Global Interpreter Lock, and how to manage threads using Python's threading and queue modules. Key functions and examples for both RegEx and multithreading are provided to illustrate their applications.

Uploaded by

koppusaiteja143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

UNIT - III

Regular Expressions: Introduction, Special Symbols


and Characters, Res and Python

Multithreaded Programming: Introduction, Threads


and Processes, Python, Threads, and the Global
Interpreter Lock, Thread Module, Threading Module,
Related Modules
REGULAR EXPRESSIONS

A RegEx, or Regular Expression, is a sequence of characters that


forms a search pattern.

RegEx can be used to check if a string contains the specified search


pattern.

RegEx Module
Python has a built-in package called re, which can be used to work
with Regular Expressions.

Import the re module:

import re
EXAMPLE
Search the string MALLA

import re
txt = "MALLA REDDY COLLEGE OF ENGINEERING"
x = re.search("MALLA", txt)
print(x)

OUTPUT
<re.Match object; span=(0, 5), match='MALLA'>
EXAMPLE
Search the string to see if it starts with MALLA

import re
txt = "MALLA REDDY COLLEGE OF ENGINEERING"
x = re.search("^MALLA", txt)
if x:
print("The string starts with MALLA")
else:
print("The string dosn't starts with MALLA")

OUTPUT
The string starts with MALLA
EXAMPLE
Search the string to see if it ends with ENGINEERING

import re
txt = "MALLA REDDY COLLEGE OF ENGINEERING"
x = re.search("ENGINEERING$", txt)
if x:
print("The string ends with ENGINEERING")
else:
print("The string dosn't ends with ENGINEERING")

OUTPUT
The string ends with ENGINEERING
REGEX FUNCTIONS
The re module offers a set of functions that allows us to
search a string for a match:

Function Description
 findall: Returns a list containing all matches
 Search: Returns a Match object if there is a match
anywhere in the string
 Split: Returns a list where the string has been split at
each match
 Sub: Replaces one or many matches with a string
THE FINDALL() FUNCTION
The findall() function returns a list containing all
matches.
Example
Print a list of all matches:
import re
txt = "MALLA REDDY COLLEGE OF
ENGINEERNG"
x = re.findall("O", txt)
print(x)
Output
['O', 'O']
The list contains the matches in the order they are found.
If no matches are found, an empty list is returned:
Example
Return an empty list if no match was found:
import re
txt = "MALLA REDDY COLLEGE OF
ENGINEERNG"
x = re.findall("CMR", txt)
print(x)
Output
[]
The search() Function
 The search() function searches the string for a match,
and returns a Match object if there is a match.
 If there is more than one match, only the first
occurrence of the match will be returned:
Example
 Search for the first white-space character in the string:
import re
txt = "MALLA REDDY COLLEGE OF ENGINEERNG"
x = re.search("\s", txt)
print("The first white-space character is located in position:",
x.start())

Output
The first white-space character is located in position: 5
The split() Function
The split() function returns a list where the string has
been split at each match:
Example
Split at each white-space character:
import re
txt = "MALLA REDDY COLLEGE OF
ENGINEERNG"
x = re.split("\s", txt)
print(x)
Output
['MALLA', 'REDDY', 'COLLEGE', 'OF',
'ENGINEERNG']
The sub() Function
The sub() function replaces the matches with the text of
your choice:
Example
Replace every white-space character with the number 9:
import re
txt = "MALLA REDDY COLLEGE OF
ENGINEERNG"
x = re.sub("\s", "9", txt)
Print(x)
Output
MALLA9REDDY9COLLEGE9OF9ENGINEERNG
The split() Function
The split() function returns a list where the string has
been split at each match:
Example
Split at each white-space character:
import re
txt = "MALLA REDDY COLLEGE OF
ENGINEERNG"
x = re.split("\s", txt)
print(x)
Output
['MALLA', 'REDDY', 'COLLEGE', 'OF',
'ENGINEERNG']
Special Symbols and Characters for REs
Multithreaded Programming
• asynchronous in nature
• require multiple concurrent activities
• processing of each activity may be nondeterministic
• Using an MT program with a shared data structure such as a
Queue, programming tasks can be organized with a few
threads
• UserRequestThread - reading client input
• RequestProcessor - retrieving requests from the queue
and processing them, providing output for a third
thread
• ReplyThread - taking output destined for the user
• programming task with multiple threads reduces the
complexity of the program
Multithreaded Programming
• asynchronous in nature
• require multiple concurrent activities
• processing of each activity may be nondeterministic
• Using an MT program with a shared data structure such as a
Queue, programming tasks can be organized with a few
threads
• UserRequestThread - reading client input
• RequestProcessor - retrieving requests from the
queue and processing them, providing output for a
third thread
• ReplyThread - taking output destined for the user
• programming task with multiple threads reduces the
complexity of the program
Threads and Processes
What Are Processes?
• A process is a program in execution
• Each process has its own address space, memory, a data
stack, and other auxiliary data
• Processes can also fork
What Are Threads?
• similar to processes except that they all execute within the
same process
• A thread has a beginning, an execution sequence, and a
conclusion
• Multiple threads within a process share the same data space
• share information or communicate with each other more
easily
Threads and Python
Global Interpreter Lock
• Execution by Python code is controlled by the Python
Virtual Machine
• Access to the Python Virtual Machine is controlled by a
global interpreter lock
• Set the GIL,
• Switch in a thread to run,
• Execute for a specified no. of bytecode instructions,
• Put the thread back to sleep (switch out thread),
• Unlock the GIL, and,
• Do it all over again (rinse, lathr, repeat).

Exiting Threads
• quit by calling an exit function such as thread.exit()
Accessing Threads From Python
• To tell whether threads are installed, attempt to import the thread
module from the interactive interpreter
>>> import thread
>>>
Python Threading Modules
• thread,
• threading, and
• Queue modules.
• thread and threading modules allow the programmer to create and
manage threads.
• Queue module allows the user to create a queue data structure

thread Module
• provides a basic synchronization data structure called a lock object
(primitive lock, simple lock, mutual exclusion lock, mutex, binary
semaphore)
• The key function of the thread module is
start_new_thread().
Accessing Threads From Python
import thread
from time import sleep, time, ctime
def loop0():
print 'start loop 0 at:', ctime(time())
sleep(4)
print 'loop 0 done at:', ctime(time())
def loop1():
print 'start loop 1 at:', ctime(time())
sleep(2)
print 'loop 1 done at:', ctime(time())
def main():
print 'starting threads…'
thread.start_new_thread(loop0, ())
thread.start_new_thread(loop1, ())
sleep(6)
print 'all DONE at:', ctime(time))
if __name__ == '__main__':
main()
Output:
% mtsleep1.py
starting threads…
start loop 0 at: Sun Aug 13 05:04:50 2000
start loop 1 at: Sun Aug 13 05:04:50 2000
loop 1 done at: Sun Aug 13 05:04:52 2000
loop 0 done at: Sun Aug 13 05:04:54 2000
all DONE at: Sun Aug 13 05:04:56 2000
threading Module

You might also like