Chapter 15: Miscellaneous Features

Enumerated Data Type , Page number: 508

In [1]:
def enum(**enums):
    return type('Enum', (), enums)
#Enum declaration
emp_dept = enum(assembly = 0, manufacturing = 1,accounts = 2, stores = 3)

from collections import namedtuple
#Structure declaration
struct_employee = namedtuple("struct_employee", "name age bs emp_dept")


#Structure for employee
department = emp_dept.manufacturing
e = struct_employee("Lothar Mattheus",28,5575.50,department)

#Result
print "Name = ",e.name
print "Age = ",e.age
print "Basic salary = ",e.bs
print "Dept = ",e.emp_dept

if(e.emp_dept == 2):
    print "%s is an accountant" %(e.name)
else:
    print "%s is not an accountant" %(e.name)
    
        
Name =  Lothar Mattheus
Age =  28
Basic salary =  5575.5
Dept =  1
Lothar Mattheus is not an accountant

Are Enums Necessary, Page number: 510

In [2]:
#macro definition
ASSEMBLY = 0
MANUFACTURING = 1
ACCOUNTS = 2
STORES = 3

from collections import namedtuple
#Structure declaration
struct_employee = namedtuple("struct_employee", "name age bs department")

#Structure for employee
e = struct_employee("Lothar Mattheus",28,5575.50,MANUFACTURING)

Division Without Typecasting , Page number: 513

In [3]:
#Variable declaration
x=6
y=4

#Calculation
a = int( x/y) 

#Result
print "Value of a = %f" %(a)
Value of a = 1.000000

Division With Typecasting , Page number: 513

In [6]:
#Variable declaration
x=6
y=4

#Calculation
a = float( x)/y

#Result
print "Value of a = %f" %(a)
Value of a = 1.500000

Type Casting , Page number: 514

In [7]:
#Variable declaration
a=6.35

#Result
print "Value of a on typecasting = %d" %(int(a))
print "Value of a = %f"%(a)
Value of a on typecasting = 6
Value of a = 6.350000

Bit Fields , Page number: 516

In [8]:
from ctypes import *
import ctypes
import math

#macro definition
MALE = 0
FEMALE = 1
SINGLE = 0
MARRIED = 1
DIVORCED = 2
WIDOWED = 3

#Structure declaration
class employee(Structure):
    _fields_ = [("gender", c_int, 1),("mar_stat", c_int, 3),("hobby", c_int, 3),("scheme", c_int, 4)]
    _sizeof_ = 2
#Structure for employee
e = employee()
e.gender = MALE
e.mar_stat = DIVORCED
e.hobby = 5
e.scheme =9

#Result
print "Gender = ",e.gender
print "Marital status = ",e.mar_stat
print "Bytes occupied by e = ",sizeof(e)
Gender =  0
Marital status =  2
Bytes occupied by e =  4

Address of a Function , Page number: 517

In [9]:
#Function definition
def display():
    print "Long live viruses!!"

#Result
print "Address of function display is ",id(display)
display() #function call
Address of function display is  133993192
Long live viruses!!

Invoking a Function Using Pointer, Page number: 518

In [10]:
#Function definition
def display():
    print "Long live viruses!!"

func_ptr = id(display) #assigning address of function
print "Address of function display is ",func_ptr
display() #function call
Address of function display is  133993080
Long live viruses!!

Functions Returning Pointers , Page number: 520

In [11]:
#Function definition
def fun():
    i = 20
    return (i)

#Result
p =fun() #function call

String Copy , Page number: 520

In [12]:
#Function definition
def copy(t,s):
    i = 0
    while ( s[i] != '\0' ):
        t = t + s[i]
        i = i + 1
    return t

#Variable declaration
source = "Jaded\0" 
target = ''
string = copy( target, source ) # function call

#Result
print string
Jaded

Findmax Function , Page number: 522

In [13]:
to find out max value from a set of values

#function declaration
def findmax(*arg):
    maxi = arg[1]
    for count in range(2,arg[0]):
        num = arg[count]
        if (num > maxi):
            maxi = num
    return maxi

maxi = findmax(5,23,15,1,92,50)#function call 
print "maximum = ",maxi

maxi = findmax(3,100,300,29)#function call 
print "maximum = ",maxi
maximum =  92
maximum =  300

Display Function, Page number: 524

In [14]:
#function declaration
def display(*arg):
    if(arg[0] == 1): #case int
        for j in range(2,arg[1]+2):
            i = arg[j]
            print "%d"%(i)
    elif(arg[0] == 2): #case char
        for j in range(2,arg[1]+2):
            i = arg[j]
            print "%c"%(i)
    elif(arg[0] == 3): #case double
        for j in range(2,arg[1]+2):
            i = arg[j]
            print "%lf"%(i)
        
#function calls
display(1,2,5,6)
display(2,4,'A','a','b','c')
display(3,3,2.5,299.3,-1.0)
5
6
A
a
b
c
2.500000
299.300000
-1.000000

Union Demo , Page number: 526

In [ ]:
from ctypes import *

#union declaration
class union_a(Union):
    _fields_ = [("i", c_int),
                 ("ch", ((c_char * 1)*2))]

key = union_a()
key.i = 512
print "key.i = ",key.i
print "key.ch[0] = ",(key.ch[0][0])
print "key.ch[1] = ",(key.ch[1][0])

Union Example , Page number: 530

In [ ]:
from ctypes import *

#Union declaration
class union_a(Union):
    _fields_ = [("i", c_int),
                 ("ch", ((c_char * 1)*2))]

key = union_a()
key.i = 512
print "key.i = ",key.i
print "key.ch[0] = ",(key.ch[0][0])
print "key.ch[1] = ",(key.ch[1][0])

key.ch[0][0] = 50 #assign new value to key.ch[0]
print"key.i = ",key.i
print"key.ch[0] = ",(key.ch[0][0])
print"key.ch[1] = ",(key.ch[1][0])

Union of Structures , Page number: 531

In [ ]:
from ctypes import *

#Structure declarations
class a(Structure):
    _fields_ = [("i", c_int),
                 ("c", ((c_char * 1)*2))]

class b(Structure):
    _fields_ = [("j", c_int),
                 ("d", ((c_char * 1)*2))]
    
#Union declaration
class union_z(Union):
    _fields_ = [("key", a),
                 ("data", b )]

strange = union_z()
strange.key.i = 512
strange.data.d[0][0] = 0
strange.data.d[1][0] = 32

print strange.key.i
print strange.data.j
print strange.key.c[0][0]
print strange.data.d[0][0]
print strange.key.c[1][0]
print strange.data.d[1][0]