Hour 20 : Understanding Unions

Example 20.1, Page No 335

In [2]:
class menu:
    def __init__(self,name,price):
        self.name = name
        self.price = price
if __name__ == '__main__':
    print "The content assigned to the union separately:\n"
    dish = menu("Sweet and Sour Chicken",9.95)
    print "Dish Name: ",dish.name
    print "Dish Price: ",dish.price
The content assigned to the union separately:

Dish Name:  Sweet and Sour Chicken
Dish Price:  9.95

Example 20.2, Page No 338

In [4]:
class employee:
    def __init__(self,start_year,dpt_code,id_number):
        self.start_year = start_year
        self.dpt_code = dpt_code
        self.id_number = id_number
if __name__ == '__main__':
    info = employee(1997,8,1234)
    print "Start Year: ",info.start_year,"\n"
    print "Dpt. Code: ",info.dpt_code,"\n"
    print "ID Number: ",info.id_number,"\n"
Start Year:  1997 

Dpt. Code:  8 

ID Number:  1234 

Example 20.3, page no. 340

In [2]:
import sys
class u:
    def __init__(self,x,y):
        self.x = x
        self.y = y
class s:
    def __init__(self,x,y):
        self.x = x
        self.y = y
if __name__ == '__main__':
    a_union = u(10,20)
    a_struct = s(10,20)
    #print "The size of double: ",sys.getsizeof(double),"byte\n" Double is not in python
    print "The size of int: ",sys.getsizeof(int),"byte\n"
    print "The size of union: ",sys.getsizeof(a_union),"byte\n"
    print "The size of struct: ",sys.getsizeof(a_struct),"byte\n"
The size of int:  872 byte

The size of union:  72 byte

The size of struct:  72 byte

Example 20.4, Page No 341

In [11]:
class u:
    ch = [0, 0]
    num = 0

def UnionInitialize(val):
    val.ch[0] = 'H'
    val.ch[1] = 'I'
    return val.ch
if __name__ == '__main__':
    val = u()
    x = UnionInitialize(val)
    print "The two characters held by the union:\n"
    print x[0],"\n"
    print x[1],"\n"
The two characters held by the union:

H 

I 

Example 20.5, Page No 344

In [2]:
class survey:
    def __init__(self,name,c_d_p,age,hour_per_week):
        self.name = name
        self.c_d_p = c_d_p
        self.age = age
        self.hour_per_week = hour_per_week
    class provider:
        def __init__(self,cable_company,dish_company):
            self.cable_company = cable_company
            self.dish_company = dish_company
def DataEnter(ptr):
    is_yes = raw_input("Are you using cable at home? (Yes or No): ")
    if is_yes == 'Y' or is_yes == 'y':
        ptr.provider.cable_company = raw_input("Enter the cable company name: ")
        ptr.c_d_p = 'c'
    else:
        is_yes = raw_input("Are you using a satellite dish? (Yes or No): ")
        if is_yes == 'Y' or is_yes == 'y':
            ptr.provider.dish_company = raw_input("Enter the satellite dish company name: ")
            ptr.c_d_p = 'd'
        else:
            ptr.c_d_p = 'p'
    ptr.name = raw_input("Please enter your name: ")
    ptr.age = int(raw_input("Your age : "))
    ptr.hour_per_week = int(raw_input("How many hours you spend on watching TV per week: "))
def DataDisplay(ptr):
    print "\nHere’s what you’ve entered:\n"
    print "Name: ",ptr.name,"\n"
    print "Age: ",ptr.age,"\n"
    print "Hour per week: ",ptr.hour_per_week,"\n"
    if ptr.c_d_p == 'c':
        print "Your cable company is: ",ptr.provider.cable_company,"\n"
    elif ptr.c_d_p == 'd':
        print "Your satellite dish company is: ",ptr.provider.dish_company,"\n"
    else:
        print "You don’t have cable or a satellite dish.\n"
    print "\nThanks and Bye!\n"
if __name__ == '__main__':
    tv = survey("","","","")
    DataEnter(tv)
    DataDisplay(tv)
Are you using cable at home? (Yes or No): N
Are you using a satellite dish? (Yes or No): Y
Enter the satellite dish company name: ABCD Company
Please enter your name: Tony Zhang
Your age : 30
How many hours you spend on watching TV per week: 8
 
Here’s what you’ve entered:

Name:  Tony Zhang 

Age:  30 

Hour per week:  8 

Your satellite dish company is:  ABCD Company 


Thanks and Bye!

Example 20.6, Page No 348

In [6]:
class bit_field:
    def __init__(self,cable,dish):
        self.cable = cable
        self.dish = dish
    cable = 1
    dish = 1
class survey:
    def __init__(self,name,age,hour_per_week):
        self.name = name
        self.age = age
        self.hour_per_week = hour_per_week
    c_d = bit_field("","")
    class provider:
        def __init__(self,cable_company,dish_company):
            self.cable_company = cable_company
            self.dish_company = dish_company
def DataEnter(ptr):
    is_yes = raw_input("Are you using cable at home? (Yes or No): ")
    if is_yes == 'Y' or is_yes == 'y':
        ptr.provider.cable_company = raw_input("Enter the cable company name: ")
        ptr.c_d.cable = 1
        ptr.c_d.dish = 0
    else:
        is_yes = raw_input("Are you using a satellite dish? (Yes or No): ")
        if is_yes == 'Y' or is_yes == 'y':
            ptr.provider.dish_company = raw_input("Enter the satellite dish company name: ")
            ptr.c_d.cable = 0
            ptr.c_d.dish = 1
        else:
            ptr.c_d.cable = 0
            ptr.c_d.dish = 0
    ptr.name = raw_input("Please enter your name: ")
    ptr.age = int(raw_input("Your age : "))
    ptr.hour_per_week = int(raw_input("How many hours you spend on watching TV per week: "))
def DataDisplay(ptr):
    print "\nHere’s what you’ve entered:\n"
    print "Name: ",ptr.name,"\n"
    print "Age: ",ptr.age,"\n"
    print "Hour per week: ",ptr.hour_per_week,"\n"
    if ptr.c_d.cable and not ptr.c_d.dish:
        print "Your cable company is: ",ptr.provider.cable_company,"\n"
    elif not ptr.c_d.cable and ptr.c_d.dish:
        print "Your satellite dish company is: ",ptr.provider.dish_company,"\n"
    else:
        print "You don’t have cable or a satellite dish.\n"
    print "\nThanks and Bye!\n"
if __name__ == '__main__':
    tv = survey("","","")
    DataEnter(tv)
    DataDisplay(tv)
Are you using cable at home? (Yes or No): N
Are you using a satellite dish? (Yes or No): Y
Enter the satellite dish company name: ABCD Company
Please enter your name: Tony Zhang
Your age : 30
How many hours you spend on watching TV per week: 8

Here’s what you’ve entered:

Name:  Tony Zhang 

Age:  30 

Hour per week:  8 

Your satellite dish company is:  ABCD Company 


Thanks and Bye!