Chapter 13: Structure and Union

Example 13.1, Page number: 409

In [6]:
#Display size of structure elements

import sys

#Class is used instead of structure in python
class book1:
    book = ''
    pages = 0
    price = 0.00

#Python variables uses value tagged methods for storing the values

#Class variable declaration
bk1 = book1()

#Result
sys.stdout.write("\nSize of Structure Elements")
sys.stdout.write("\nBook    :  %d"%(sys.getsizeof(bk1.book)))
sys.stdout.write("\nPages    :  %d"%(sys.getsizeof(bk1.pages)))
sys.stdout.write("\nPrice    :  %d"%(sys.getsizeof(bk1.price)))
sys.stdout.write("\nTotal Bytes    :  %d"%(sys.getsizeof(bk1)))
Size of Structure Elements
Book    :  21
Pages    :  12
Price    :  16
Total Bytes    :  36

Example 13.2, Page number: 410

In [16]:
#Define a structure and initialize its member variables

import sys

#Class declaration
class book1:
    book = ''
    pages = 0
    price = 0.0
    
#Class variable declaration and Initialization
bk1 = book1()
bk1.book = "C++"
bk1.pages = 300
bk1.price = 285

#Result
sys.stdout.write("\nBook Name  : %s"%(bk1.book))
sys.stdout.write("\nNo. of Pages : %d"%(bk1.pages))
sys.stdout.write("\nBook Price  : %d"%(bk1.price))
Book Name  : C++
No. of Pages : 300
Book Price  : 285

Example 13.3, Page number: 411

In [13]:
#Copy structure elements from one object to another object

import sys

#Class declaration
class disk:
    co = ''
    type1 = 0.0
    price = 0
    
#Class variable declaration
d1 = disk()
d2 = disk()
d3 = disk()

#Class variable initialization
d1.co = "SONY"
d1.type1 = 1.44
d1.price = 20

#Copying
d2.co = d1.co
d2.type1 = d1.type1
d2.price = d1.price

d3 = d2

#Result
sys.stdout.write("\n%s  %g  %d"%(d1.co,d1.type1,d1.price))
sys.stdout.write("\n%s  %g  %d"%(d2.co,d2.type1,d2.price))
sys.stdout.write("\n%s  %g  %d"%(d3.co,d3.type1,d3.price))
SONY  1.44  20
SONY  1.44  20
SONY  1.44  20

Example 13.4, Page number: 412

In [1]:
#Read values and assign them to structure variables

import sys

#Class declaration
class book1:
    book = ''
    pages = 0
    price = 0.0
    
#Class variable declaration
bk1 = book1()

#Variable initialization
bk1.book = raw_input("Enter Book name,pages,price : ")
bk1.pages = int(raw_input("Enter Book name,pages,price : "))
bk1.price = float(raw_input("Enter Book name, pages,price : "))

#Result
sys.stdout.write("\nBook Name : %s"%(bk1.book))
sys.stdout.write("\nNo. of Pages : %d"%(bk1.pages))
sys.stdout.write("\nBook Price : %d"%(bk1.price))
Enter Book name,pages,price : C
Enter Book name,pages,price : 500
Enter Book name, pages,price : 450

Book Name : C
No. of Pages : 500
Book Price : 450

Example 13.5, Page number: 414

In [2]:
#Read and display car details

import sys

#Class declaration
class time:
    second = 0
    minute = 0
    hour = 0
    
class t:
    carno = 0
    st = time()
    rt = time()
    
#Class variable declaration
r1 = t()

#Variable Initialization
r1.carno = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.st.hour = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.st.minute = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.st.second = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.rt.hour = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.rt.minute = int(raw_input("Car No., Starting Time, Reaching Time :"))
r1.rt.second = int(raw_input("Car No., Starting Time, Reaching Time :"))

#Result
sys.stdout.write("\nCar No. \tStarting Time \tReaching Time\n")
sys.stdout.write("%d\t"%(r1.carno))
sys.stdout.write("\t%d:%d:%d\t\t"%(r1.st.hour,r1.st.minute,r1.st.second))
sys.stdout.write("%d:%d:%d"%(r1.rt.hour,r1.rt.minute,r1.rt.second))
Car No., Starting Time, Reaching Time :125
Car No., Starting Time, Reaching Time :2
Car No., Starting Time, Reaching Time :50
Car No., Starting Time, Reaching Time :30
Car No., Starting Time, Reaching Time :3
Car No., Starting Time, Reaching Time :50
Car No., Starting Time, Reaching Time :25

Car No. 	Starting Time 	Reaching Time
125		2:50:30		3:50:25

Example 13.6, Page number: 415

In [3]:
#Read and display the details of a person

import sys

#Class declaration
class name:
    first = ''
    second = ''
    last = ''
    
class b_date:
    day = 0
    month = 0
    year = 0
    
class data:
    nm = name()
    bt = b_date()
    
#Class variable declaration
r1 = data()

#Variable Initialization
r1.nm.first = raw_input("Enter Name (First/Second/Last)")
r1.nm.second = raw_input("Enter Name (First/Second/Last)")
r1.nm.last = raw_input("Enter Name (First/Second/Last)")
r1.bt.day = int(raw_input("Enter Birth Date Day/Month/Year"))
r1.bt.month = int(raw_input("Enter Birth Date Day/Month/Year"))
r1.bt.year = int(raw_input("Enter Birth Date Day/Month/Year"))

#Result
sys.stdout.write("Name  : %s %s %s\n"%(r1.nm.first,r1.nm.second,r1.nm.last))
sys.stdout.write("Birth Date : %d.%d.%d"%(r1.bt.day,r1.bt.month,r1.bt.year))
Enter Name (First/Second/Last)Ram
Enter Name (First/Second/Last)Sham
Enter Name (First/Second/Last)Pande
Enter Birth Date Day/Month/Year12
Enter Birth Date Day/Month/Year12
Enter Birth Date Day/Month/Year1980
Name  : Ram Sham Pande
Birth Date : 12.12.1980

Example 13.7, Page number: 416

In [4]:
#Create array of structure objects

import sys

#Class declaration
class time:
    second = 0
    minute = 0
    hour = 0
    
class t:
    carno = 0
    st = time()
    rt = time()
    
#Class variable declaration
r1 = [t() for i in range(0,3)]

#Variable Initialization
for k in range(0,3):
    r1[k].carno = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
    r1[k].st.hour = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
    r1[k].st.minute = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
    r1[k].st.second = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
    r1[k].rt.hour = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
    r1[k].rt.minute = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
    r1[k].rt.second = int(raw_input("Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)"))
   
#Result
sys.stdout.write("\nCar No.\t\tStarting Time\tReaching Time")
for k in range(0,3):
    sys.stdout.write("\n%d\t"%(r1[k].carno))
    sys.stdout.write("\t%d:%d:%d\t"%(r1[k].st.hour,r1[k].st.minute,r1[k].st.second))
    sys.stdout.write("\t%d:%d:%d\t"%(r1[k].rt.hour,r1[k].rt.minute,r1[k].rt.second))
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)120
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)2
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)20
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)3
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)58
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)121
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)3
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)4
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)25
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)122
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)4
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)30
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)52
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)5
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)40
Car No. Starting Time(hh:mm:ss),Reaching Time(hh:mm:ss)10

Car No.		Starting Time	Reaching Time
120		4:30:52		5:40:10	
121		4:30:52		5:40:10	
122		4:30:52		5:40:10	

Example 13.8, Page number: 417

In [5]:
#Read and display student details

import sys

#Class declaration
class stud:
    name = ''
    rollno = 0
    grade = ''
    
#Class variable declaration
st = [stud() for i in range(0,3)]

#Variable Initialization
k = 0 
while k < 3:
    st[k].name = raw_input("Name :")
    st[k].rollno = int(raw_input("Roll No. :"))
    st[k].grade = raw_input("Grade : ")
    k += 1
    
#Result
k = 0
sys.stdout.write("\nName \tRollno \tGrade\n")
while k < 3:
    sys.stdout.write("\n%s\t%d\t%s"%(st[k].name,st[k].rollno,st[k].grade))
    k += 1
    
Name :Suresh
Roll No. :125
Grade : A
Name :Mahesh
Roll No. :126
Grade : B
Name :Rajesh
Roll No. :127
Grade : A

Name 	Rollno 	Grade

Suresh	125	A
Mahesh	126	B
Rajesh	127	A

Example 13.9, Page number: 419

In [27]:
#Pointer to structure 

import sys

#Class declaration
class book:
    name = ''
    author = ''
    pages = 0
    
#Class variable declaration
b1 = book()

#Variable Initialization
b1.name = "JAVA COMPLETE REFERENCE"
b1.author = "P.NAUGHTON"
b1.pages = 886

#There is no pointer concept in python

#Result
sys.stdout.write("\n%s by %s of %d pages"%(b1.name,b1.author,b1.pages))
sys.stdout.write("\n%s by %s of %d pages"%(b1.name,b1.author,b1.pages))
JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages
JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages

Example 13.10, Page number: 420

In [28]:
#Pointer as members of structure 

import sys

#Class declaration
class boy:
    name = ''
    age = 0
    height = 0.0
    
#Class variable declaration
sp = boy()

#Variable Initialization
sp.name = "Mahesh"
sp.age = 20
sp.height = 5.40

#There is no pointer concept in python

#Result
sys.stdout.write("\nName  = %s"%(sp.name))
sys.stdout.write("\nAge  = %s"%(sp.age))
sys.stdout.write("\nHeight = %s"%(sp.height))
Name  = Mahesh
Age  = 20
Height = 5.4

Example 13.11, Page number: 421

In [30]:
#Pointer as members of structure 

import sys

#Class declaration
class boy:
    name = ''
    age = 0
    height = 0.0
    
#Class variable declaration
b = boy()

#Variable Initialization
nm = "Somesh"
ag = 20
ht = 5.40

#There is no pointer concept in python
b.name = nm
b.age = ag
b.height = ht

#Result
sys.stdout.write("\nName  : %s"%(b.name))
sys.stdout.write("\nAge = %d"%(b.age))
sys.stdout.write("\nHeight = %.2g"%(b.height))
Name  : Somesh
Age = 20
Height = 5.4

Example 13.12, Page number: 422

In [32]:
#Display the contents of the structure using ordinary pointer

import sys

#Class declaration
class num:
    a = 0
    b = 0
    c = 0

#Class variable declaration
d = num()

#Variable Initialization
d.a = 2
d.b = 3
d.c = 4

#There is no pointer concept in python

#Result
sys.stdout.write("\na = %d"%(d.a))
sys.stdout.write("\nb = %d"%(d.b))
sys.stdout.write("\nc = %d"%(d.c))
a = 2
b = 3
c = 4

Example 13.13, Page number: 423

In [34]:
#Passing address of structure variable

import sys

#Class declaration
class book:
    name = ''
    author = ''
    pages = 0
    
#Class variable declaration and initialization
b1 = book()
b1.name = "JAVA COMPLETE REFERENCE"
b1.author = "P.NAUGHTON"
b1.pages = 886

#Function definition
def show(b1):
    sys.stdout.write("\n%s by %s of %d pages"%(b1.name,b1.author,b1.pages))

#Function call
show(b1)
JAVA COMPLETE REFERENCE by P.NAUGHTON of 886 pages

Example 13.14, Page number: 424

In [36]:
#Passing structure elements to function

import sys

#Class declaration
class boy:
    name = ''
    age = 0
    wt = 0
    
#Class variable declaration and initialization
b1 = boy()
b1.name = "Amit"
b1.age = 20
b1.wt = 25

#Function definition
def print1(s,t,n):
    sys.stdout.write("\n%s %d %d"%(s,t,n))
    
#Function call
print1(b1.name,b1.age,b1.wt)
Amit 20 25

Example 13.15, Page number: 425

In [37]:
#Pass entire structure to user defined function

import sys

#Class declaration
class boy:
    name = ''
    age = 0
    wt = 0
    
#Class variable declaration and initialization
b1 = boy()
b1.name = "Amit"
b1.age = 20
b1.wt = 25

#Function definition
def print1(b):
    sys.stdout.write("\n%s %d %d"%(b.name,b.age,b1.wt))
    
#Function call
print1(b1)
Amit 20 25

Example 13.16, Page number: 426

In [6]:
#User defined data type 

import sys

#There is no preprocessor directive in python
H = 60

#There is no typedef function in python and no separate variable declaration is needed
hrs = int(raw_input("Enter Hours :"))

#Result
sys.stdout.write("\nMinutes = %d"%(hrs*H))
sys.stdout.write("\nSeconds = %d"%(hrs*H*H))
Enter Hours :2

Minutes = 120
Seconds = 7200

Example 13.17, Page number: 426

In [7]:
#String data type

import sys

#There is no typedef function in python
a = " Hello "
b = raw_input("Enter Your Name : ")

#Result
sys.stdout.write("%s %s"%(a,b))
Enter Your Name : KAMAL
 Hello  KAMAL

Example 13.18, Page number: 427

In [44]:
#Create user defined data type from structure

import sys

#Class declaration
#There is no typedef function in python
class struct:
    name = ''
    sex = ''
    acno = 0
    
#class variable declaration and initialization
employee = struct()
employee.name = "Sanjay"
employee.sex = "M"
employee.acno = 125

#Result
sys.stdout.write("\nName\tSex\tA/c No.\n")
sys.stdout.write("%s\t"%(employee.name))
sys.stdout.write("%s\t"%(employee.sex))
sys.stdout.write("%s\n"%(employee.acno))
Name	Sex	A/c No.
Sanjay	M	125

Example 13.19, Page number: 427

In [8]:
#Create user defined data type from structure

import sys

#Class declaration
class struct:
    name = ''
    sex = ''
    acno = 0
    
#class Variable declaration and initialization
employee = [struct() for i in range(0,2)]

for k in range(0,2):
    employee[k].name = raw_input("Name of the Employee :")
    employee[k].sex = raw_input("Sex")
    employee[k].acno = raw_input("A/c No.")
    
#Result
sys.stdout.write("\nName\tSex\tA/c No.\n")
for k in range(0,2):
    sys.stdout.write("%s\t"%(employee[k].name))
    sys.stdout.write("%s\t"%(employee[k].sex))
    sys.stdout.write("%s\n"%(employee[k].acno))
Name of the Employee :AJAY
SexM
A/c No.122
Name of the Employee :ANITA
SexF
A/c No.124

Name	Sex	A/c No.
AJAY	M	122
ANITA	F	124

Example 13.20, Page number: 429

In [10]:
#Structure containing the details of the employees

import sys

#There is no typedef function in python

#Class definition
class struct:
    first = ''
    middle = ''
    last = ''
    city = ''
    pincode = 0
    
#Class variable declaration and initialization
person = [struct() for i in range(0,2)]

for j in range(0,2):
    person[j].first = raw_input("First Name :")
    person[j].middle = raw_input("Middle Name : ")
    person[j].last = raw_input("Last Name : ")
    person[j].city = raw_input("City & Pincode")
    person[j].pincode = int(raw_input("City & Pincode"))
    
#Result
for j in range(0,2):
    sys.stdout.write("\nRecord No : %d"%(j+1))
    sys.stdout.write("\nFirst Name : %s"%(person[j].first))
    sys.stdout.write("\nMiddle Name : %s"%(person[j].middle))
    sys.stdout.write("\nLast Name : %s"%(person[j].last))
    sys.stdout.write("\nCity & Pincode : %s - %d\n"%(person[j].city,person[j].pincode))
First Name :Jay
Middle Name : Mohan
Last Name : Deshmukh
City & PincodeNanded
City & Pincode431602
First Name :Vijay
Middle Name : Kamal
Last Name : Nandedkar
City & PincodeNanded
City & Pincode431602

Record No : 1
First Name : Jay
Middle Name : Mohan
Last Name : Deshmukh
City & Pincode : Nanded - 431602

Record No : 2
First Name : Vijay
Middle Name : Kamal
Last Name : Nandedkar
City & Pincode : Nanded - 431602

Example 13.21, Page number: 431

In [52]:
#Information of vehicles

import sys

#Variable Initialization
PETROL = 1
DISEL = 2
TWO_WH = 3
FOUR_WH = 4
OLD = 5
NEW = 6

#Class declaration
class vehicle:
    type1 = 3
    fuel = 2
    model = 3
    
#Class variable declaration and initialization
v = vehicle()
v.type1 = FOUR_WH
v.fuel = DISEL
v.model = OLD

#Result
sys.stdout.write("\nType of Vehicle : %d"%(v.type1))
sys.stdout.write("\nFuel  : %d"%(v.fuel))
sys.stdout.write("\nModel  : %d"%(v.model))
Type of Vehicle : 4
Fuel  : 2
Model  : 5

Example 13.22, Page number: 432

In [53]:
#Display the examination result of the student 

import sys

#Variable Initialization
#There is no preprocessor directives in python
PASS = 1
FAIL = 0
A = 0
B = 1
C = 2

#Class declaration
class student:
    name = ''
    result = 1
    grade = 2
    
#Class variable declaration and initialization
v = student()

v.name = "Sachin"
v.result = PASS
v.grade = C

#Result
sys.stdout.write("\nName : %s"%(v.name))
sys.stdout.write("\nResult : %d"%(v.result))
sys.stdout.write("\nGrade : %d"%(v.grade))
Name : Sachin
Result : 1
Grade : 2

Example 13.23, Page number: 433

In [1]:
#Enumerated data type for 12 months

import sys

#There is no enumerated data type in python and an alternate is to use class
#Class declaration
class month:
    Jan = 0
    Feb = 1
    Mar = 2
    Apr = 3
    May = 4
    June = 5
    July = 6
    Aug = 7
    Sep = 8
    Oct = 9
    Nov = 10
    Dec = 11
    
#Class variable declaration
c = month()

#Result
sys.stdout.write("\nJan = %d"%(c.Jan))
sys.stdout.write("\nFeb = %d"%(c.Feb))
sys.stdout.write("\nJune = %d"%(c.June))
sys.stdout.write("\nDec = %d"%(c.Dec))
    
Jan = 0
Feb = 1
June = 5
Dec = 11

Example 13.24, Page number: 434

In [1]:
#Enumerated data type 

import sys

#There is no enumerated data type in python and an alternate is to use class
#Class declaration
class month:
    Jan = 1
    Feb = 2
    Mar = 3
    Apr = 4
    May = 5
    June = 6
    July = 7
    Aug = 8
    Sep = 9
    Oct = 10
    Nov = 11
    Dec = 12
    
#Class variable declaration
c = month()

#Result
sys.stdout.write("\nJan = %d"%(c.Jan))
sys.stdout.write("\nFeb = %d"%(c.Feb))
sys.stdout.write("\nJune = %d"%(c.June))
sys.stdout.write("\nDec = %d"%(c.Dec))
    
Jan = 1
Feb = 2
June = 6
Dec = 12

Example 13.25, Page number: 434

In [4]:
#Display name of month using enumerated data type

import sys

#There is no enumerated data type in python and an alternate is to use class
#Class declaration
class month:
    Jan = 1
    Feb = 2
    Mar = 3
    Apr = 4
    May = 5
    June = 6
    July = 7
    Aug = 8
    Sep = 9
    Oct = 10
    Nov = 11
    Dec = 12
    
#Class variable declaration
c = month()

#Result
for f in range(c.Jan,c.Dec+1):
    #There is no switch case statement in python
    if f == c.Jan:
        sys.stdout.write("\nJanuary")
    else:
        if f == c.Feb:
            sys.stdout.write("\nFebruary")
        else:
            if f == c.Mar:
                sys.stdout.write("\nMarch")
            else:
                if f == c.Apr:
                    sys.stdout.write("\nApril")
                else:
                    if f == c.May:
                        sys.stdout.write("\nMay")
                    else:
                        if f == c.June:
                            sys.stdout.write("\nJune")
                        else:
                            if f == c.July:
                                sys.stdout.write("\nJuly")
                            else:
                                if f == c.Aug:
                                    sys.stdout.write("\nAugust")
                                else:
                                    if f == c.Sep:
                                        sys.stdout.write("\nSeptember")
                                    else:
                                        if f == c.Oct:
                                            sys.stdout.write("\nOctober")
                                        else:
                                            if f == c.Nov:
                                                sys.stdout.write("\nNovember")
                                            else:
                                                sys.stdout.write("\nDecember")
January
February
March
April
May
June
July
August
September
October
November
December

Example 13.26, Page number: 436

In [6]:
#Enumerated data type

import sys

#There is no enumerated data type in python. class is used instead
class capital:
    Mumbai = 0
    Hyderabad = 1
    Bangalore = 2

class state:
    name = ''
    c = capital()
    
#Class variable declaration
s = state()
c1 = capital()

#Class variable initialization
s.name = "Andhra Pradesh"
s.c = s.c.Hyderabad

#Result
sys.stdout.write("\nState  : %s"%(s.name))
sys.stdout.write("\nCapital : %d"%(s.c))

if s.c == c1.Hyderabad:
    sys.stdout.write("\nHyderabad is Capital of %s"%(s.name))
State  : Andhra Pradesh
Capital : 1
Hyderabad is Capital of Andhra Pradesh

Example 13.27, Page number: 437

In [11]:
#Identify the type of entered character using enumerated data type.

import sys

#Class declaration instead of enumerated data type
class ctype:
    Letter = 0
    Digit = 1
    Other = 2
    
#Variable Initialization
ch = raw_input("Enter any character")
c = ctype()
f = ch.isalpha()

#Result
if f != 0:
    sys.stdout.write("\n%c is %d type of symbol"%(ch,c.Letter))
else:
    f = ch.isdigit()
    if f != 0:
        sys.stdout.write("\n%c is %d type of symbol"%(ch,c.Digit))
    else:
        sys.stdout.write("\n%c is %d type of symbol"%(ch,c.Other))
Enter any character=

= is 2 type of symbol

Example 13.28, Page number: 438

In [9]:
#Size of union and number of bytes reserved for it

import sys

#There is no union/structure in python. class is used instead
#Class declaration
class result:
    marks = 0
    grade = ''
    
class res:
    name = ''
    age = 0
    perf = result()
    
#Class variable declaration
data = res()

#Result
sys.stdout.write("Size of Union : %d\n"%(sys.getsizeof(data.perf)))
sys.stdout.write("Size of Structure : %d\n"%(sys.getsizeof(data)))

#in python, value tagged method is used for data storage and can represent large numbers
Size of Union : 36
Size of Structure : 36

Example 13.29, Page number: 440

In [4]:
#Memory size of the computer

import psutil

psutil.phymem_usage()

#Displays current status of the memory
#different systems will have different memory status
Out[4]:
usage(total=1600512000L, used=1496383488L, free=104128512L, percent=93.5)

Example 13.30, Page number: 440

In [11]:
#Display the system time at specified cursor position

import sys
import datetime

#Result
sys.stdout.write("%s"%(datetime.datetime.now()))
2013-09-20 11:04:15.649000

Example 13.31, Page number: 441

In [3]:
#Change the cursor in different sizes

import turtle
#Ipython supports graphics through turtle module

from Tkinter import *
import Tkinter

top = Tkinter.Tk()

B1 = Tkinter.Button(top, text ="circle", relief=RAISED,\
                         cursor="circle")
B2 = Tkinter.Button(top, text ="plus", relief=RAISED,\
                         cursor="plus")
B1.pack()
B2.pack()
#top.mainloop()

Example 13.32, Page number: 441

In [8]:
#Create a directory using dos interrupt

import os
import sys

#Variable Initialization
dir1 = raw_input("Enter Directory Name : ")

#Result
if not os.path.exists(dir1):
    os.makedirs(dir1)
    sys.stdout.write("Directory %s created"%(dir1))
else:
    sys.stdout.write("Directory %s not created"%(dir1))
Directory XYZ created

Example 13.33, Page number: 442

In [9]:
#Display the given character on the screen

import sys

#Variable Initialization
dl = 67

#Result
sys.stdout.write("%c"%(chr(dl)))
C

Example 13.34, Page number: 442

In [12]:
#Display the attributes of a file using DOS interrupt

import sys

import os

print os.stat("IO.SYS")
nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=0L, st_atime=1383454467L, st_mtime=1383454467L, st_ctime=1383454467L)

Example 13.35, Page number: 444

In [13]:
#Delete a file using dos interrupt

import os
import sys

#Variable Initialization
file1 = raw_input("Enter a file name : ")

try:
    os.remove(file1)
    sys.stdout.write("File successfully deleted")
except:
    sys.stdout.write("File could not be deleted")
File successfully deleted

Example 13.36, Page number: 445

In [12]:
#Structuret within union

import sys

#there is no union/structure in python. class is used instead
#Class declaration
class x:
    f = 0.0
    p = ['' for i in range(0,2)]
    
class z:
    set1 = x()
    
#Class variable declaration and initialization
st = z()

st.set1.f = 5.5
st.set1.p[0] = 65
st.set1.p[1] = 66

#Result
sys.stdout.write("\n%g"%(st.set1.f))
sys.stdout.write("\n%c"%(st.set1.p[0]))
sys.stdout.write("\n%c"%(st.set1.p[1]))
5.5
A
B
In [ ]: