82
Shell Programming & Scripting Languages Dr. K. Sasidhar

Spsl vi unit final

Embed Size (px)

Citation preview

Page 1: Spsl vi unit final

Shell Programming & Scripting Languages

Dr. K. Sasidhar

Page 2: Spsl vi unit final

UNIT – VI Contents

Object-Orientation

Data in Python, Data Structures in Python, Defining

Classes

The Python Database Interface.

Database Interfaces, The Underlying Interface Model,

Some Database Modules, A Simple Database-Driven Web,

SQL/Python Communication.

Page 3: Spsl vi unit final

Unit VI outcomes From the VI unit Student can

Implement user defined type class.

Design oops concepts like inheritance.

Understand, learn Python interface with databases.

Connect with database and can write solution as a python

program and executes.

Page 4: Spsl vi unit final

Object Oriented Programming in Python

Class: A user-defined prototype for an object that defines

a set of attributes that characterize any object of the class.

The attributes are data members (class variables and

instance variables) and methods, accessed via dot

notation.

Object: Instance of a class. An object comprises both data

members (class variables and instance variables) and

methods.

Page 5: Spsl vi unit final

OOP in Python: Defining a class

A class is a special data type which defines how to build a certain kind of object.

The class also stores some data items that are shared by all the instances of this class

Instances are objects that are created which follow the definition given inside of the class

Python doesn’t use separate class interface definitions as in some languages

You just define the class and then use it

Page 6: Spsl vi unit final

Defining Methods Define a method in a class by including function definitions within the

scope of the class block There must be a special first argument self in all method definitions which

gets bound to the calling instance There is usually a special method called __init__ in most classes There is no “new” keyword in python Use the class name with ( ) notation and assign the result to a

variable __init__ serves as a constructor for the class. Usually does some

initialization work The arguments passed to the class name are given to its __init__()

method So, the __init__ method for employee is passed “Bob” and 15000

and the new class instance is bound to b: b=Employee(“Bob”,15000)

Page 7: Spsl vi unit final

Simple class example class shareddata(object): a=44

x=shareddata() y=shareddata() z=shareddata()

print(x.a) print(y.a) print(z.a)

# Creates three instances

# Inherit and share a

Page 8: Spsl vi unit final

Creating classes class ClassName:

class variables methods # methods are functions inside a class

Example: class Employee: empCount = 0 def __init__(self, name, salary): #__init__ runs when a new instance object

is created. #self is an instance self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self):

print(“count”, self.empCount) def displayDetails(self): print( "Name : ", self.name, ", Salary: ", self.salary )

Page 9: Spsl vi unit final

Constructor: __init__

An __init__ method can take any number of arguments.

Like other functions or methods, the arguments can be defined with default values, making them optional to the caller.

However, the first argument self in the definition of __init__ is special…

Page 10: Spsl vi unit final

Program to update the employee salary. class Person: def __init__(self, name, job=None, pay=0): self.name = name self.job = job self.pay = pay def lastName(self): # Behavior methods return self.name.split()[-1] # self is implied subject def giveRaise(self, percent): self.pay = int(self.pay * (1 + percent)) # Must change here only if __name__ == '__main__': bob = Person(' Smith') sue = Person(' Jones', job='dev', pay=10000) print(bob.name, bob.pay) print(sue.name, sue.pay) print(bob.lastName(), sue.lastName()) # Use the new methods sue.giveRaise(.10) # instead of hardcoding print(sue.pay)

Page 11: Spsl vi unit final

Inheritance

Page 12: Spsl vi unit final

Subclasses

A class can extend the definition of another class Allows use (or extension ) of methods and attributes already defined in the previous one.

To define a subclass, put the name of the superclass in parentheses after the subclass’s name on the first line of the definition. Class ECM_student(student):Python has no ‘extends’ keyword like Java.Multiple inheritance is supported.

Page 13: Spsl vi unit final

Redefining Methods

To redefine a method of the parent class, include a new definition using the same name in the subclass.

The old code won’t get executed.

To execute the method in the parent class in addition to new code for some method, explicitly call the parent’s version of the method.

parentClass.methodName(self, a, b, c)

The only time you ever explicitly pass ‘self’ as an argument is when calling a method of an ancestor.

Page 14: Spsl vi unit final

Definition of a class extending student

Class Student: #“A class representing a student.”def __init__(self,n,a): self.full_name = n self.age = a

def get_age(self): return self.age

Class ECM_student (student): #class extending student.”

def __init__(self,n,a,s): student.__init__(self,n,a) #Call __init__ for student self.section_num = s

def get_age(): #Redefines get_age method entirely print “Age: ” + str(self.age)

Page 15: Spsl vi unit final

Database Programming The Python standard for database interfaces is the Python DB-

API. Python Database API supports a wide range of database servers

such as: MySQL PostgreSQL Microsoft SQL Server 2000 Informix Interbase Oracle Sybase GadFly mSQL

Page 16: Spsl vi unit final

Working with Databases through Python

Download a separate DB API module for each database

you need to access.

For example, to access an Oracle database as well as a

MySQL database, download both the Oracle and the

MySQL database modules.

Page 17: Spsl vi unit final

Database connectivity and display version import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. cursor.execute("SELECT VERSION()") # Fetch a single row using fetchone() method. data = cursor.fetchone() print "Database version : %s " % data # disconnect from server db.close()

Page 18: Spsl vi unit final

Creating a Table Using python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Drop table if it already exist using execute() method. cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # Create table as per requirement sql = """CREATE TABLE EMPLOYEE ( empid Number(20) NOT NULL, NAME CHAR(20), Job char(10), Sal number(10)) cursor.execute(sql) db.close

Page 19: Spsl vi unit final

READ Operation

READ Operation on any database means to fetch some useful information from the database.

Once database connection is established, then we can send a query into this database.

use either fetchone() method to fetch 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.

Page 20: Spsl vi unit final

Program to display all the records from EMPLOYEE table having salary more than 1000:

import MySQLdb db =

MySQLdb.connect("localhost","testuser","test123","TESTDB" )

# prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = "SELECT * FROM EMPLOYEE \ WHERE INCOME > '%d'" % (1000) try: cursor.execute(sql)

Page 21: Spsl vi unit final

results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # print fetched result print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ) except: print "Error: unable to fecth data" db.close()

Page 22: Spsl vi unit final

Python Data Structures

Page 23: Spsl vi unit final

Lists

An ordered group of items

Does not need to be the same type

Could put numbers, strings or donkeys in the same list

List notation

A = [1,”This is a list”, c, Donkey(“kong”)]

Page 24: Spsl vi unit final

Methods of Lists

List.append(x) adds an item to the end of the list

List.extend(L) Extend the list by appending all in the given list L

List.insert(I,x) Inserts an item at index I

List.remove(x) Removes the first item from the list whose value is x

Page 25: Spsl vi unit final

Examples of other methods

a = [66.25, 333, 333, 1, 1234.5] //Defines List print a.count(333), a.count(66.25), a.count('x') //calls

method 2 1 0 //output

a.index(333) //Returns the first index where the given value appears 1 //ouput

a.reverse() //Reverses order of list a //Prints list a [333, 1234.5, 1, 333, -1, 66.25] //Ouput

a.sort() a //Prints list a [-1, 1, 66.25, 333, 333, 1234.5] //Output

Page 26: Spsl vi unit final

Using Lists as Stacks

The last element added is the first element retrieved

To add an item to the stack, append() must be used

stack = [3, 4, 5]

stack.append(6)

Stack is now [3, 4, 5, 6]

To retrieve an item from the top of the stack, pop must be used

Stack.pop()

6 is output

Stack is now [3, 4, 5] again

Page 27: Spsl vi unit final

Using Lists as Queues

First element added is the first element retrieved To do this collections.deque

must be implemented

Page 28: Spsl vi unit final

List Programming Tools

Filter(function, sequence)

Returns a sequence consisting of the items from the sequence

for which function(item) is true

Computes primes up to 25

Page 29: Spsl vi unit final

Map Function

Map(function, sequence) Calls function(item) for each of the sequence’s

items

Computes the cube for the range of 1 to 11

Page 30: Spsl vi unit final

Reduce Function

Reduce(function, sequence) Returns a single value constructed by calling the binary

function (function)

Computes the sum of the numbers 1 to 10

Page 31: Spsl vi unit final

The del statement

A specific index or range can be deleted

Page 32: Spsl vi unit final

Tuples

Tuple A number of values separated by commas Immutable

Cannot assign values to individual items of a tuple However tuples can contain mutable objects such as lists

Single items must be defined using a comma Singleton = ‘hello’,

Page 33: Spsl vi unit final

Sets

An unordered collection with no duplicate elements Basket = [‘apple’, ‘orange’, ‘apple’, ‘pear’] Fruit = set(basket) Fruit

Set([‘orange’, ‘apple’, ‘pear’])

Page 34: Spsl vi unit final

Dictionaries

Indexed by keys This can be any immutable type (strings, numbers…) Tuples can be used if they contain only immutable objects

Page 35: Spsl vi unit final

Looping Techniques

Iteritems(): for retrieving key and values through a dictionary

Page 36: Spsl vi unit final

Looping Techniques

Enumerate(): for the position index and values in a sequence

Page 37: Spsl vi unit final

Zip(): for looping over two or more sequences

Page 38: Spsl vi unit final

Comparisons

Operators “in” and “not in” can be used to see if an item exists in a sequence

Comparisons can be chained a < b == c

This tests whether a is less than b and that b equals c

Page 39: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 39

Tree Terminology

Page 40: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 40

Tree Terminology (continued)

Page 41: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 41

Note: The height of a tree containing one node is 0By convention, the height of an empty tree is –1

Tree Terminology (continued)

Page 42: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 42

General Trees and Binary Trees

In a binary tree, each node has at most two children: The left child and the right child

Page 43: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 43

Recursive Definitions of Trees

A general tree is either empty or consists of a finite set of nodes T Node r is called the root The set T – {r} is partitioned into disjoint subsets, each of

which is a general tree A binary tree is either empty or consists of a root plus a left

subtree and a right subtree, each of which are binary trees

Page 44: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 44

Why Use a Tree?

A parse tree describes the syntactic structure of a particular sentence in terms of its component parts

Page 45: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 45

Why Use a Tree? (continued)

File system structures are also tree-like

Page 46: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 46

Why Use a Tree? (continued)

Sorted collections can also be represented as tree-like structures Called a binary search tree, or BST for short

Can support logarithmic searches and insertions

Page 47: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 47

The Shape of Binary Trees

The shape of a binary tree can be described more formally by specifying the relationship between its height and the number of nodes contained in it

N nodesHeight: N – 1

A full binary tree contains the maximum number of nodes for a givenheight H

Page 48: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 48

The Shape of Binary Trees (continued)

The number of nodes, N, contained in a full binary tree of height H is 2H + 1 – 1

The height, H, of a full binary tree with N nodes is log2(N + 1) – 1

The maximum amount of work that it takes to access a given node in a full binary tree is O(log N)

Page 49: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 49

The Shape of Binary Trees (continued)

Page 50: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 50

Three Common Applications of Binary Trees In this section, we introduce three special uses of binary

trees that impose an ordering on their data: Heaps Binary search trees Expression trees

Page 51: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 51

A Binary Tree ADT

Provides many common operations required for building more specialized types of trees

Should support basic operations for creating trees, determining if a tree is empty, and traversing a tree

Remaining operations focus on accessing, replacing, or removing the component parts of a nonempty binary tree—its root, left subtree, and right subtree

Page 52: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 52

The Interface for a Binary Tree ADT

Page 53: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 53

The Interface for a Binary Tree ADT (continued)

Page 54: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 54

Processing a Binary Tree

Many algorithms for processing binary trees follow the trees’ recursive structure

Programmers are occasionally interested in the frontier, or set of leaf nodes, of a tree Example: Frontier of parse tree for English sentence shown

earlier contains the words in the sentence

Page 55: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 55

Processing a Binary Tree (continued)

frontier expects a binary tree and returns a list Two base cases:

Tree is empty return an empty list Tree is a leaf node return a list containing root item

Page 56: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 56

Implementing a Binary Tree

Page 57: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 57

Implementing a Binary Tree (continued)

Page 58: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 58

Implementing a Binary Tree (continued)

Page 59: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 59

The String Representation of a Tree

__str__ can be implemented with any of the traversals

Page 60: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 60

Developing a Binary Search Tree

A BST imposes a special ordering on the nodes in a binary tree, so as to support logarithmic searches and insertions

In this section, we use the binary tree ADT to develop a binary search tree, and assess its performance

Page 61: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 61

The Binary Search Tree Interface

The interface for a BST should include a constructor and

basic methods to test a tree for emptiness, determine the

number of items, add an item, remove an item, and search

for an item

Another useful method is __iter__, which allows users to

traverse the items in BST with a for loop

Page 62: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 62

Data Structures for the Implementation of BST

Page 63: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 63

Searching a Binary Search Tree

find returns the first matching item if the target item is in the tree; otherwise, it returns None We can use a recursive strategy

Page 64: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 64

Inserting an Item into a Binary Search Tree add inserts an item in its proper place in the BST Item’s proper place will be in one of three positions:

The root node, if the tree is already empty A node in the current node’s left subtree, if new item is less

than item in current node A node in the current node’s right subtree, if new item is

greater than or equal to item in current node

For options 2 and 3, add uses a recursive helper function named addHelper

In all cases, an item is added as a leaf node

Page 65: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 65

Removing an Item from a Binary Search Tree Save a reference to root node Locate node to be removed, its parent, and its parent’s

reference to this node If item is not in tree, return None Otherwise, if node has a left and right child, replace

node’s value with largest value in left subtree and delete that value’s node from left subtree Otherwise, set parent’s reference to node to node’s only child

Reset root node to saved reference Decrement size and return item

Page 66: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 66

Removing an Item from a Binary Search Tree (continued) Fourth step is fairly complex: Can be factored out into a

helper function, which takes node to be deleted as a parameter (node containing item to be removed is referred to as the top node): Search top node’s left subtree for node containing the largest

item (rightmost node of the subtree) Replace top node’s value with the item If top node’s left child contained the largest item, set top node’s

left child to its left child’s left child Otherwise, set parent node’s right child to that right child’s left

child

Page 67: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 67

Complexity Analysis of Binary Search Trees

BSTs are set up with intent of replicating O(log n)

behavior for the binary search of a sorted list

A BST can also provide fast insertions

Optimal behavior depends on height of tree

A perfectly balanced tree supports logarithmic searches

Worst case (items are inserted in sorted order): tree’s height is

linear, as is its search behavior

Insertions in random order result in a tree with close-to-

optimal search behavior

Page 68: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 68

Case Study: Parsing and Expression Trees

Request: Write a program that uses an expression tree to evaluate

expressions or convert them to alternative forms

Analysis: Like the parser developed in Chapter 17, current program

parses an input expression and prints syntax error messages if errors occur

If expression is syntactically correct, program prints its value and its prefix, infix, and postfix representations

Page 69: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 69

Case Study: Parsing and Expression Trees (continued)

Page 70: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 70

Case Study: Parsing and Expression Trees (continued)

Design and Implementation of the Node Classes:

Page 71: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 71

Case Study: Parsing and Expression Trees (continued)

Page 72: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 72

Case Study: Parsing and Expression Trees (continued)

Page 73: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 73

Case Study: Parsing and Expression Trees (continued)

Design and Implementation of the Parser Class:

Easiest to build an expression tree with a parser that uses a

recursive descent strategy

parse should now return an expression tree to its caller,

which uses that tree to obtain information about the

expression

factor processes either a number or an expression nested in

parentheses

Calls expression to parse nested expressions

Page 74: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 74

Case Study: Parsing and Expression Trees (continued)

Page 75: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 75

Case Study: Parsing and Expression Trees (continued)

Page 76: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 76

An Array Implementation of Binary Trees An array-based implementation of a binary tree is difficult

to define and practical only in some cases For complete binary trees, there is an elegant and

efficient array-based representation Elements are stored by level

The array representation of a binary tree is pretty rare and is used mainly to implement a heap

Page 77: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 77

An Array Implementation of Binary Trees (continued)

Page 78: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 78

An Array Implementation of Binary Trees (continued)

Page 79: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 79

An Array Implementation of Binary Trees (continued)

Page 80: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 80

Implementing Heaps

Page 81: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 81

Implementing Heaps (continued)

At most, log2n comparisons must be made to walk up the tree from the bottom, so add is O(log n)

Method may trigger a doubling in the array size O(n), but amortized over all additions, it is O(1)

Page 82: Spsl vi unit final

Fundamentals of Python: From First Programs Through Data Structures 82

Using a Heap to Implement a Priority Queue we can use a heap