class inv_type:
def __init__(self):
self.item=None
self.cost=0
self.retail=0
self.on_hand=0
self.lead_time=0
#Variable declaration
size=100
invtry = []*size
i=5 #User iput for menu selection
#Initialize the array
def init_list():
for t in range(size):
invtry.append(inv_type())
#get a menu selection
def menu():
global i
print "(E)nter"
print "(D)isplay"
print "(U)pdate"
print "(Q)uit"
print "choose one: "
i-=1
return i
#enter items into the list
def enter():
#find the first free structure
for i in range(size):
if not(invtry[i].item==None):
break
#i will be size if list is full
if i==size:
print "List full."
return
input(i)
#Input the information
def input(i):
#Enter information; User input
invtry[i].item="Gloves"
invtry[i].cost=10
invtry[i].retail=25
invtry[i].on_hand=50
invtry[i].lead_time=10
#Modify an existing item
def update():
name="Gloves" #User input
for i in range(size):
if not(name==invtry[i].item):
break
if i==size:
print "Item not found."
return
print "Enter new information."
input(i)
#Display the list
def display():
for t in range(size):
if not(invtry[t].item==None):
print invtry[t].item
print "Cost: $",invtry[t].cost
print "Retail: $",invtry[t].retail
print "On hand: ",invtry[t].on_hand
print "Resupply time: ",invtry[t].lead_time," days"
init_list()
while True:
choice=menu()
if choice==4:
enter()
elif choice==3:
display()
elif choice==2:
update()
elif choice==1:
break
class sample:
a=None
ch=None
def f1(parm):
print parm.a," ",parm.ch
#declare arg
arg=sample()
#initialize arg
arg.a=1000
arg.ch='X'
#call function
f1(arg)
class stype:
a=None
b=None
#Variable declaration
svar1=stype()
svar2=stype()
svar1.a=svar1.b=10
svar2.a=svar2.b=20
print "Structures before assignment."
print "svar1: ",svar1.a,' ',svar1.b
print "svar1: ",svar2.a,' ',svar2.b
svar2=svar1 #assign structures
#Result
print "\nStructures before assignment."
print "svar1: ",svar1.a,' ',svar1.b
print "svar1: ",svar2.a,' ',svar2.b
import datetime
date=datetime.datetime.now()
#Result
print date.time()
import datetime
date=datetime.datetime.now()
#Result
print date.ctime()
class mystruct:
a=None
b=None
def f(var):
var[0].a=var[0].a*var[0].a
var[0].b=var[0].b/var[0].b
return var[0]
#Variable declaration
x=[]
x.append(mystruct())
y=mystruct()
#Initializing
x[0].a=10
x[0].b=20
print "Original x.a and x.b: ",x[0].a,' ',x[0].b
y=f(x) #function call
#Result
print "Modified x.a and x.b: ",x[0].a,' ',x[0].b
print "Modified y.a and y.b: ",y.a,' ',y.b
class swap_bytes:
ch=[0,0]
#Exchange of bytes
def disp_binary(u):
t=128
while not(t==0):
if u&t:
print "1 ",
else:
print "0 ",
t=t/2
#Variable declaration
sb=swap_bytes()
sb.ch[0]=15
print "Original bytes: ",
disp_binary(sb.ch[1])
disp_binary(sb.ch[0])
#Exchange bytes
temp=sb.ch[0]
sb.ch[0]=sb.ch[1]
sb.ch[1]=temp
#Result
print "\nExchanged bytes: ",
disp_binary(sb.ch[1])
disp_binary(sb.ch[0])
ch='a'
while True:
print "\n",ch,
print bin(ord(ch)) #Display the bit pattern for each character
ch=chr(ord(ch)+1)
if ch=='r':
break
#Variable declaration
ch=['X','Y']
c=""
def disp_bits(u):
t=128
global c
while not(t==0):
if u&t:
c=c+"1"
else:
c=c+"0"
t=t/2
return c
#Result
print "union as chars: ",ch[0],ch[1]
print "union as integer: ",
c= disp_bits(ord(ch[1]))
c= disp_bits(ord(ch[0]))
print int(str(c),2)