CHAPTER 15: MISCELLANEOUS FEATURES

EXAMPLE ON PAGE:473-474

In [1]:
 
def emp_dept(*sequential, **named):                                           #definition of enumeration
    enums = dict(zip(sequential, range(len(sequential))), **named)
    return type('Enum', (), enums)
class employee():                                                             #structure
    def __init__(self,**kwds):
        self.__dict__.update(kwds)
department=emp_dept('assembly','manufacturing','accounts','stores')           #defining a variable of type enum emp_dept
e=employee(name="Lothar Mattheus",age=28,bs=5575.50,department=department.manufacturing)
print "Name=%s\n" % (e.name)
print "Age=%d\n" % (e.age)
print "Basic salary=%f\n" % (e.bs)
print "Dept=%d\n" % (e.department)
if e.department==department.accounts:
    print "%s is an accountant\n" % (e.name)
else:
    print "%s is not an accountant\n" % (e.name)
Name=Lothar Mattheus

Age=28

Basic salary=5575.500000

Dept=1

Lothar Mattheus is not an accountant

EXAMPLE ON PAGE:475

In [2]:
 
def ASSEMBLY():
    return 0
def MANUFACTURING():
    return 1
def ACCOUNTS():
    return 2
def STORES():
    return 3
class employee():
    def __init__(self,**kwds):
        self.__dict__.update(kwds)
e=employee()
e.name="Lothar Mattheus"
e.age=28
e.bs=5575.50
e.department=MANUFACTURING()
print "Name=%s\n" % (e.name)
print "Age=%d\n" % (e.age)
print "Basic salary=%f\n" % (e.bs)
print "Dept=%d\n" % (e.department)
Name=Lothar Mattheus

Age=28

Basic salary=5575.500000

Dept=1

EXAMPLE ON PAGE:477-478

In [3]:
 
x=6
y=4
a=x/y                                              #int
print "Value of a=%f\n" % (a)
Value of a=1.000000

EXAMPLE ON PAGE:478

In [4]:
 
x=6
y=4
a=float(x)/y                                        #float
print "Value of a=%f\n" % (a)
Value of a=1.500000

EXAMPLE ON PAGE:478-479

In [5]:
 
a=6.35
print "Value of a on type casting=%d\n" % (int(a))         #converts float to int
print "Value of a=%f\n" % (a)                         
Value of a on type casting=6

Value of a=6.350000

EXAMPLE ON PAGE:480

In [6]:
 
from ctypes import *
def MALE():
    return 0
def FEMALE():
    return 1
def SINGLE():
    return 0
def MARRIED():
    return 1
def DIVORCED():
    return 2
def WIDOWED():
    return 3
class employee(Structure):
    _fields_= [("gender",c_short, 1),                            #1 bit size for storage
               ("mar_stat", c_short, 2),                         #2 bit size for storage
               ("hobby",c_short, 3),                             #3 bit size for storage
               ("scheme",c_short, 4)]                            #4 bit size for storage
e=employee()
e.gender=MALE()
e.mar_status=DIVORCED()
e.hobby=5
e.scheme=9
print "Gender=%d\n" % (e.gender)
print "Marital status=%d\n" % (e.mar_status)
import ctypes
print "Bytes occupied by e=%d\n" % (ctypes.sizeof(e))
Gender=0

Marital status=2

Bytes occupied by e=2

EXAMPLE ON PAGE:481

In [7]:
 
def display():
    print "Long live viruses!!\n"
print "Address of function display is %s\n" % (display)
display()
Address of function display is <function display at 0x02D93DF0>

Long live viruses!!

EXAMPLE ON PAGE:481-482

In [8]:
 
def display():
    print "\nLong live viruses!!"
func_ptr=display
print "Address of function display is %s\n" % (display)
func_ptr()
Address of function display is <function display at 0x05696730>


Long live viruses!!

EXAMPLE ON PAGE:483

In [9]:
 
def fun():
    i=20
    return (id(i))
p=fun
p()
Out[9]:
20688356

EXAMPLE ON PAGE:483-484

In [18]:
 
def copy(t,s):
    t="\0"
    for item in s:
        t=t+item
    return t
source="Jaded"
target=[]
str1=copy(target,source)
print "%s\n" % (str1)
Jaded

EXAMPLE ON PAGE:485

In [19]:
 
def findmax(tot_num,*num):                      #tuples
    max=0
    for count in range(0,len(num),1):
        if num[count]>max:
            max=num[count]
    return max
max=findmax(5,23,15,1,92,50)
print "maximum=%d\n" % (max)
max=findmax(3,100,300,29)
print "maximum=%d\n" % (max)
maximum=92

maximum=300

EXAMPLE ON PAGE:486-487

In [21]:
 
def display(type,num,*t):
    def integer():
        for j in range(0,num,1):
            print "%d" % (t[j])
    def char():
        for j in range(0,num,1):
            print "%c" % (t[j])
    def floating():
        for j in range(0,num,1):
            print "%f" % (t[j])
    def switch(ch):
        return {1: integer,
                2: char,
                3: floating,
                }[ch]()
    switch(type)
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

EXAMPLE ON PAGE:488

In [22]:
 
from ctypes import *
class a(Union):                               #definition of union
    _fields_= [("i", c_short),
               ("ch",c_byte*2)]
key=a()
key.i=512
print key.i
print key.ch[0]
print key.ch[1]
512
0
2

EXAMPLE ON PAGE:491

In [1]:
 
from ctypes import *
class a(Union):
    _fields_= [("i", c_short),
               ("ch",c_byte*2)]
key=a()
key.i=512
print key.i
print key.ch[0]
print key.ch[1]
key.ch[0]=50
print key.i
print key.ch[0]
print key.ch[1]
512
0
2
562
50
2

EXAMPLE ON PAGE:492-493

In [2]:
 
from ctypes import *
class a(Structure):
    _fields_ = [("i", c_short),
                ("c", c_byte*2)]

class b(Structure):
    _fields_ = [("j", c_short),
                ("d", c_byte*2)]

class z(Union):
    _fields_ = [("key", a),
                 ("data", b)]
strange = z()
strange.key.i=512
strange.data.d[0]=0
strange.data.d[1]=32
print strange.key.i
print strange.data.j
print strange.key.c[0]
print strange.data.d[0]
print strange.key.c[1]
print strange.data.d[1]
512
512
0
0
32
32

EXAMPLE ON PAGE:494-495

In [3]:
 
from ctypes import *
class info1(Structure):
    _fields_ = [("hobby", c_byte*10),
                ("crcardno", c_short)]

class info2(Structure):
    _fields_ = [("vehno", c_byte*10),
                ("dist", c_short)]

class info(Union):
    _fields_ = [("a", info1),
                ("b", info2)]
class emp(Structure):
    _fields_ = [("n", c_byte*20),
                ("grade", c_byte*4),
                ("age", c_short),
                ("f", info)]
e=emp()