Hour 19 : Understanding Structures

Example 19.1, Page No 316

In [6]:
class computer:
    def __init__(self, cost, year, cpu_speed, cpu_type):
        self.cost = cost
        self.year = year
        self.cpu_speed = cpu_speed
        self.cpu_type = cpu_type
if __name__ == '__main__':
    cpu_type = raw_input("The type of CPU inside your computer: ")
    cpu_speed = int(raw_input("The speed(MHz) of the CPU?: "))
    year = int(raw_input("The year your computer was made?: "))
    cost = float(raw_input("How much you paid for the computer?: "))
    computer(cost,year,cpu_speed,cpu_type)
    print "Hear are what you have entered"
    print "Year: ",year,"\n"
    print "Cost: ",cost,"\n"
    print "CPU type: ",cpu_type,"\n"
    print "CPU speed: ",cpu_speed,"MHz"
The type of CPU inside your computer: Pentium
The speed(MHz) of the CPU?: 100
The year your computer was made?: 1996
How much you paid for the computer?: 1234.56
Hear are what you have entered
Year:  1996 

Cost:  1234.56 

CPU type:  Pentium 

CPU speed:  100 MHz

Example 19.2, Page No 318

In [1]:
class employee:
    def __init__(self,id1,name):
        self.id1 = id1
        self.name = name
if __name__ ==   '__main__':
    info = employee(1,"B.Smith")
    print "Here is a sample: \n"
    print "Employee Name : ",info.name,"\n"
    print "Employee ID : ",info.id1
    info.name = raw_input("What's your name?: ")
    info.id1 = int(raw_input("What's your ID number: "))
    print "\nHere are what you entered: \n"
    print "Name: ",info.name,"\n"
    print "Id: ",info.id1,"\n"
Here is a sample: 

Employee Name :  B.Smith 

Employee ID :  1
What's your name?: T. Zhang
What's your ID number: 1234

Here are what you entered: 

Name:  T. Zhang 

Id:  1234 

Example 19.3, Page No 320

In [7]:
class computer:
    def __init__(self, cost, year, cpu_speed, cpu_type):
        self.cost = cost
        self.year = year
        self.cpu_speed = cpu_speed
        self.cpu_type = cpu_type
def DataReceiver(s):
    cpu_type = raw_input("The type of CPU inside your computer: ")
    cpu_speed = int(raw_input("The speed(MHz) of the CPU?: "))
    year = int(raw_input("The year your computer was made?: "))
    cost = float(raw_input("How much you paid for the computer?: "))
    s = computer(cost,year,cpu_speed,cpu_type)
    return s
if __name__ == '__main__':
    model = DataReceiver(model)
    print "Hear are what you have entered"
    print "Year: ",model.year,"\n"
    print "Cost: ",model.cost,"\n"
    print "CPU type: ",model.cpu_type,"\n"
    print "CPU speed: ",model.cpu_speed ,"MHz" 
The type of CPU inside your computer: Pentium
The speed(MHz) of the CPU?: 100
The year your computer was made?: 1996
How much you paid for the computer?: 1234.56
Hear are what you have entered
Year:  1996 

Cost:  1234.56 

CPU type:  Pentium 

CPU speed:  100 MHz

Example 19.4, Page No 322

In [8]:
# There is no pointer in python
class computer:
    def __init__(self, cost, year, cpu_speed, cpu_type):
        self.cost = cost
        self.year = year
        self.cpu_speed = cpu_speed
        self.cpu_type = cpu_type
def DataReceiver(s):
    cpu_type = raw_input("The type of CPU inside your computer: ")
    cpu_speed = int(raw_input("The speed(MHz) of the CPU?: "))
    year = int(raw_input("The year your computer was made?: "))
    cost = float(raw_input("How much you paid for the computer?: "))
    s = computer(cost,year,cpu_speed,cpu_type)
    return s
if __name__ == '__main__':
    model = DataReceiver(model)
    print "Hear are what you have entered"
    print "Year: ",model.year,"\n"
    print "Cost: ",model.cost,"\n"
    print "CPU type: ",model.cpu_type,"\n"
    print "CPU speed: ",model.cpu_speed ,"MHz" 
The type of CPU inside your computer: Pentium
The speed(MHz) of the CPU?: 100
The year your computer was made?: 1996
How much you paid for the computer?: 1234.56
Hear are what you have entered
Year:  1996 

Cost:  1234.56 

CPU type:  Pentium 

CPU speed:  100 MHz

Example 19.5, Page No 325

In [10]:
class haiku:
    def __init__(self,start_year,end_year,author,str1,str2,str3):
        self.start_year = start_year
        self.end_year = end_year
        self.author = author
        self.str1 = str1
        self.str2 = str2
        self.str3 = str3
def DataDisplay(ptr_s):
    print ptr_s.str1
    print ptr_s.str2
    print ptr_s.str3
    print " ---",ptr_s.author
    print "(",ptr_s.start_year,",",ptr_s.end_year,")"
if __name__ == '__main__':
    poem = []
    poem.append(haiku(1641,1716,"Sodo","Leading me along","my shadow goes back home","from looking at the moon"))
    poem.append(haiku(1729,1781,"Chora","A strom wind blows","out from among the grasses","the full moon grows"))
    for i in range(len(poem)):
        DataDisplay(poem[i])
Leading me along
my shadow goes back home
from looking at the moon
 --- Sodo
( 1641 , 1716 )
A strom wind blows
out from among the grasses
the full moon grows
 --- Chora
( 1729 , 1781 )

Example 19.6, Page No 327

In [14]:
class department:
    def __init__(self,code,dname,position):
        self.code = code
        self.dname = dname
        self.position = position
class employee:
    def __init__(self,code,dname,position,id1,name):
        self.DPT = department(code,dname,position)
        self.id1 = id1
        self.name = name
def InfoDisplay(ptr):
    print "Name: ",ptr.name,"\n"
    print "ID #: ",ptr.id1,"\n"
    print "Dept. name: ",ptr.DPT.dname,"\n"
    print "Dept. code: ",ptr.DPT.code,"\n"
    print "Your Position : ",ptr.DPT.position,"\n"
def InfoEnter(ptr):
    print "Please enter your information:\n"
    name = raw_input("Your name: ")
    position = raw_input("Your position: ")
    name = raw_input("Dept. name: ")
    code = raw_input("Dept. code: ")
    id1 = raw_input("Your employee ID #: ")
if __name__ == '__main__':
    info = employee("1","marketing","manager",1,"B.Smith")
    print "\nHere is a sample"
    InfoDisplay(info)
    InfoEnter(info)
    print "\nHere are what you entered"
    InfoDisplay(info)
 
Here is a sample
Name:  B.Smith 

ID #:  1 

Dept. name:  marketing 

Dept. code:  1 

Your Position :  manager 

Please enter your information:

Your name: T. Zhang
Your position: Engineer
Dept. name: R&D
Dept. code: 3
Your employee ID #: 1234

Here are what you entered
Name:  B.Smith 

ID #:  1 

Dept. name:  marketing 

Dept. code:  1 

Your Position :  manager