Shell Programming & Scripting Languages
Dr. K. Sasidhar
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.
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.
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.
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
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)
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
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 )
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…
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)
Inheritance
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.
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.
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)
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
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.
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()
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
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.
Program to display all the records from EMPLOYEE
table having salary more than 1000:
 import MySQLdb
 db =
MySQLdb.connect("localhost","testuser","test123","TEST
DB" )
 # 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)
 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()
Python Data Structures
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”)]
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
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
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
Using Lists as Queues
 First element added is the first element retrieved
 To do this collections.deque
must be implemented
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
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
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
The del statement
 A specific index or range can be deleted
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’,
Sets
 An unordered collection with no duplicate elements
 Basket = [‘apple’, ‘orange’, ‘apple’, ‘pear’]
 Fruit = set(basket)
 Fruit
 Set([‘orange’, ‘apple’, ‘pear’])
Dictionaries
 Indexed by keys
 This can be any immutable type (strings, numbers…)
 Tuples can be used if they contain only immutable objects
Looping Techniques
 Iteritems():
 for retrieving key and values through a dictionary
Looping Techniques
 Enumerate():
 for the position index and values in a sequence
 Zip():
 for looping over two or more sequences
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
Fundamentals of Python: From First Programs Through Data Structures 39
Tree Terminology
Fundamentals of Python: From First Programs Through Data Structures 40
Tree Terminology (continued)
Fundamentals of Python: From First Programs Through Data Structures 41
Note: The height of a tree containing one node is 0
By convention, the height of an empty tree is –1
Tree Terminology (continued)
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
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
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
Fundamentals of Python: From First Programs Through Data Structures 45
Why Use a Tree? (continued)
 File system structures are also tree-like
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
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 nodes
Height: N – 1
A full binary tree contains
the maximum number of
nodes for a given
height H
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)
Fundamentals of Python: From First Programs Through Data Structures 49
The Shape of Binary Trees (continued)
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
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
Fundamentals of Python: From First Programs Through Data Structures 52
The Interface for a Binary Tree ADT
Fundamentals of Python: From First Programs Through Data Structures 53
The Interface for a Binary Tree ADT
(continued)
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
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
Fundamentals of Python: From First Programs Through Data Structures 56
Implementing a Binary Tree
Fundamentals of Python: From First Programs Through Data Structures 57
Implementing a Binary Tree (continued)
Fundamentals of Python: From First Programs Through Data Structures 58
Implementing a Binary Tree (continued)
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
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
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
Fundamentals of Python: From First Programs Through Data Structures 62
Data Structures for the Implementation
of BST
…
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
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
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
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
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
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
Fundamentals of Python: From First Programs Through Data Structures 69
Case Study: Parsing and Expression Trees
(continued)
Fundamentals of Python: From First Programs Through Data Structures 70
Case Study: Parsing and Expression Trees
(continued)
 Design and Implementation of the Node Classes:
Fundamentals of Python: From First Programs Through Data Structures 71
Case Study: Parsing and Expression Trees
(continued)
Fundamentals of Python: From First Programs Through Data Structures 72
Case Study: Parsing and Expression Trees
(continued)
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
Fundamentals of Python: From First Programs Through Data Structures 74
Case Study: Parsing and Expression Trees
(continued)
Fundamentals of Python: From First Programs Through Data Structures 75
Case Study: Parsing and Expression Trees
(continued)
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
Fundamentals of Python: From First Programs Through Data Structures 77
An Array Implementation of Binary Trees
(continued)
Fundamentals of Python: From First Programs Through Data Structures 78
An Array Implementation of Binary Trees
(continued)
Fundamentals of Python: From First Programs Through Data Structures 79
An Array Implementation of Binary Trees
(continued)
Fundamentals of Python: From First Programs Through Data Structures 80
Implementing Heaps
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)
Fundamentals of Python: From First Programs Through Data Structures 82
Using a Heap to Implement a Priority
Queue
 we can use a heap

Spsl vi unit final

  • 1.
    Shell Programming &Scripting Languages Dr. K. Sasidhar
  • 2.
    UNIT – VIContents  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.
  • 3.
    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.
  • 4.
    Object Oriented Programmingin 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.
  • 5.
    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
  • 6.
    Defining Methods  Definea 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)
  • 7.
    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
  • 8.
    Creating classes class ClassName: classvariables 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 )
  • 9.
    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…
  • 10.
    Program to updatethe 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)
  • 11.
  • 12.
    Subclasses  A classcan 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.
  • 13.
    Redefining Methods  Toredefine 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.
  • 14.
    Definition of aclass 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)
  • 15.
    Database Programming  ThePython 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
  • 16.
    Working with Databasesthrough 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.
  • 17.
    Database connectivity anddisplay 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()
  • 18.
    Creating a TableUsing 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
  • 19.
    READ Operation  READOperation 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.
  • 20.
    Program to displayall the records from EMPLOYEE table having salary more than 1000:  import MySQLdb  db = MySQLdb.connect("localhost","testuser","test123","TEST DB" )  # 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)
  • 21.
     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()
  • 22.
  • 23.
    Lists  An orderedgroup 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”)]
  • 24.
    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
  • 25.
    Examples of othermethods  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
  • 26.
    Using Lists asStacks  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
  • 27.
    Using Lists asQueues  First element added is the first element retrieved  To do this collections.deque must be implemented
  • 28.
    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
  • 29.
    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
  • 30.
    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
  • 31.
    The del statement A specific index or range can be deleted
  • 32.
    Tuples  Tuple  Anumber 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’,
  • 33.
    Sets  An unorderedcollection with no duplicate elements  Basket = [‘apple’, ‘orange’, ‘apple’, ‘pear’]  Fruit = set(basket)  Fruit  Set([‘orange’, ‘apple’, ‘pear’])
  • 34.
    Dictionaries  Indexed bykeys  This can be any immutable type (strings, numbers…)  Tuples can be used if they contain only immutable objects
  • 35.
    Looping Techniques  Iteritems(): for retrieving key and values through a dictionary
  • 36.
    Looping Techniques  Enumerate(): for the position index and values in a sequence
  • 37.
     Zip():  forlooping over two or more sequences
  • 38.
    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
  • 39.
    Fundamentals of Python:From First Programs Through Data Structures 39 Tree Terminology
  • 40.
    Fundamentals of Python:From First Programs Through Data Structures 40 Tree Terminology (continued)
  • 41.
    Fundamentals of Python:From First Programs Through Data Structures 41 Note: The height of a tree containing one node is 0 By convention, the height of an empty tree is –1 Tree Terminology (continued)
  • 42.
    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
  • 43.
    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
  • 44.
    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
  • 45.
    Fundamentals of Python:From First Programs Through Data Structures 45 Why Use a Tree? (continued)  File system structures are also tree-like
  • 46.
    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
  • 47.
    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 nodes Height: N – 1 A full binary tree contains the maximum number of nodes for a given height H
  • 48.
    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)
  • 49.
    Fundamentals of Python:From First Programs Through Data Structures 49 The Shape of Binary Trees (continued)
  • 50.
    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
  • 51.
    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
  • 52.
    Fundamentals of Python:From First Programs Through Data Structures 52 The Interface for a Binary Tree ADT
  • 53.
    Fundamentals of Python:From First Programs Through Data Structures 53 The Interface for a Binary Tree ADT (continued)
  • 54.
    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
  • 55.
    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
  • 56.
    Fundamentals of Python:From First Programs Through Data Structures 56 Implementing a Binary Tree
  • 57.
    Fundamentals of Python:From First Programs Through Data Structures 57 Implementing a Binary Tree (continued)
  • 58.
    Fundamentals of Python:From First Programs Through Data Structures 58 Implementing a Binary Tree (continued)
  • 59.
    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
  • 60.
    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
  • 61.
    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
  • 62.
    Fundamentals of Python:From First Programs Through Data Structures 62 Data Structures for the Implementation of BST …
  • 63.
    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
  • 64.
    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
  • 65.
    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
  • 66.
    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
  • 67.
    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
  • 68.
    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
  • 69.
    Fundamentals of Python:From First Programs Through Data Structures 69 Case Study: Parsing and Expression Trees (continued)
  • 70.
    Fundamentals of Python:From First Programs Through Data Structures 70 Case Study: Parsing and Expression Trees (continued)  Design and Implementation of the Node Classes:
  • 71.
    Fundamentals of Python:From First Programs Through Data Structures 71 Case Study: Parsing and Expression Trees (continued)
  • 72.
    Fundamentals of Python:From First Programs Through Data Structures 72 Case Study: Parsing and Expression Trees (continued)
  • 73.
    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
  • 74.
    Fundamentals of Python:From First Programs Through Data Structures 74 Case Study: Parsing and Expression Trees (continued)
  • 75.
    Fundamentals of Python:From First Programs Through Data Structures 75 Case Study: Parsing and Expression Trees (continued)
  • 76.
    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
  • 77.
    Fundamentals of Python:From First Programs Through Data Structures 77 An Array Implementation of Binary Trees (continued)
  • 78.
    Fundamentals of Python:From First Programs Through Data Structures 78 An Array Implementation of Binary Trees (continued)
  • 79.
    Fundamentals of Python:From First Programs Through Data Structures 79 An Array Implementation of Binary Trees (continued)
  • 80.
    Fundamentals of Python:From First Programs Through Data Structures 80 Implementing Heaps
  • 81.
    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)
  • 82.
    Fundamentals of Python:From First Programs Through Data Structures 82 Using a Heap to Implement a Priority Queue  we can use a heap