def isFeet(str): #return true if the string is a correct feet value
slen = len(str) #get length
if slen==0 or slen>5: #if no input, or too long
return False #not an int
for j in range(slen): #check each character
if (str[j] < '0' or str[j] > '9' ) and str[j] != '-': #if not digit or minus
return False #string is not correct feet
n = float(str) #convert to float
if(n<-999.0 or n>999.0): #is it out of range
return False #if so, not correct feet
return True #it is correct feet
class Distance: #Distance class
def __init__(self,ft=0,inc=0): #constructor
self.__feet = ft
self.__inches = inc
def showdist(self): #display distance
print self.__feet , '\' -' , self.__inches , '\"'
def getdist(self): #get length from user
while True:
self.__feet = raw_input('Enter feet:') #it will not skip white space
if(isFeet(self.__feet)): #is it a correct feet value?
self.__feet.replace(' ','') #eat white spaces
self.__feet = int(self.__feet) #convert to integer
break; #break out of 'while'
print 'Feet must be an integer less than 1000'
while True: #cycle untill inches are right
self.__inches = input('Enter inches:') #get inches and do not skip white space
if(self.__inches<12.0 and self.__inches>0.0):
break;
if(self.__inches>=12.0 or self.__inches<0.0):
print 'Inches must be between 0.0 and 11.99'
print 'Incorrect inches input'
d = Distance() #make a distance object
while True:
d.getdist() #get its value from user
print 'Distance =',;d.showdist() #display it
ans = raw_input("Do another (y/n)? ")
if ans == 'n': #cycle untill 'n'
break
ch = "x"
str1 = "Kafka" #strings without white spaces
str2 = "Proust"
outfile=open("fdata.txt",'w') #open a file
outfile.write(ch) #insert (write) data
outfile.write("77")
outfile.write("6.02")
outfile.write(' ') #needed space between numbers and string
outfile.write(str1)
outfile.write(' ') #needed space between strings
outfile.write(str2)
print 'File written'
outfile.close()
infile = open("fdata.txt",'r') #open the file for reading
ch = infile.read(1) #extract (read) data from it
j = infile.read(2)
d = infile.read(5)
str1 = infile.read(6)
str2 = infile.read()
print ch,"\n",j,"\n",d,"\n",str1,"\n",str2,"\n", #display the data
outfile = open("TEST.TXT",'w') #create a file for output
#send text to file
outfile.write("I fear thee, ancient Marner!\n")
outfile.write("I fear thy skinny hand\n")
outfile.write("And thou art long, and lank, and brown,\n")
outfile.write("As is the ribbed sea sand.\n")
outfile.close() #close the file
MAX = 80 #size of buffer
buffer = []*MAX #character buffer
infile = open("TEST.TXT",'r') #open a file to read
buffer = infile.readline() #read one line from the file
while buffer: #untill buffer has some data in it or untill end-of-file
print buffer, #display buffer
buffer = infile.readline() #read a line of text
infile.close() #close file
str = "Time is a great teache, but unfortunately " + "it kills all its puplies. Berlioz"
outfile = open("Test.txt","w") #create file for output
for j in range(len(str)): #for each character
ch = ""+str[j]
outfile.write(ch),
print "File written"
outfile.close()
infile = open("Test.txt","r") #open file for read
ch = infile.read(1) #read character
while ch != '': #read untill EOF
print ch, #display it
ch = infile.read(1) #read character
infile = open("Test.txt","r") #open file for read
print infile.read() #read whole file and print it
MAX = 100 #size of buffer
buff = [j for j in range(MAX)] #buffer for integers and fill buffer with data (0, 1, 2, ...)
os = open("edata.txt",'r+b') #first make edata.txt file
s = ''
for j in range(MAX):
s += '%d' %buff[j]
os.write(s) #write to file
for j in range(MAX): #erase buffer
buff[j] = 0
os.seek(0,0)
#read data from file
for j in range (10):
buff[j] = os.read(1)
for j in range(10,MAX):
buff[j] = os.read(2)
c=0
for j in range(MAX): #check data
if int(buff[j])!=j:
print "Data is incorrect\n"
c=1
break
if c==0:
print "Data is correct"
from pickle import dump
class person: #class person
def getData(self): #get person's data
self.name = raw_input("Enter name: ") #person's name
self.age = input("Enter age: ") #person's age
pers = person() #create a person
pers.getData() #get data for person
outfile = open("PERSON.txt",'wb') #open file for write obect to it
dump(pers, outfile) #write object
outfile.close()
from pickle import load
class person:
def showData(self): #display person's data
print "Name:",self.name #person's name
print "Age:",self.age #person's age
pers = person() #create person variable
infile = open("PERSON.txt",'rb') #open file in binary mode
pers = load(infile) #read object
pers.showData() #display person
from pickle import dump,load
class person: #class of persons
def getData(self): #get person's data
self._name = raw_input("Enter name: ")
self._age = input("Enter age: ")
def showData(self): #display person's data
print " Name:",self._name
print " Age:",self._age
pers = person() #create person object
file = open("Group.txt",'r+b') #open file
c=0 #for total number of objects
while True:
print "\nEnter person's data: "
pers.getData() #get one person's data
dump(pers, file) #wriet to file
c += 1
ch = raw_input("Enter another person (y/n)? ")
if(ch == 'n'):
break
file.seek(0,0)
print ''
for j in range(c):
print 'person:'
pers = load(file) #read person from file
pers.showData() #display person
from pickle import load
class person: #class of person's
def getData(self): #get person's data
self._name = raw_input("Enter name: ")
self._age = input("Enter age: ")
def showData(self): #dislay person's data
print " Name:",self._name
print " Age:",self._age
pers = person() #create person object
infile = open("Group.txt",'r+b') #create input file and open it
c=3
print 'There are',c,'persons in file' #print number of objects in file
n = input("Enter person number: ")
print ''
for j in range(3):
pers = load(infile) #read one person
if j==n-1:
pers.showData() #display the person
from pickle import dump,load
MAX = 1000
buff = []*1000
for j in range(MAX): #fill buffer with data
buff.append(j)
try:
os = open("edata.txt",'r+b') #open a file
except IOError:
print "could not open output file"
print 'Writing...'
try:
dump(buff, os) #write buffer to it
except IOError:
print "could not write ot file"
os.close() #must close it
for j in range(MAX): #clear buffer
buff[j]=0
try:
is1 = open("edata.txt",'r+b') #open the same file in read mode
except IOError:
print "could not open output file"
print 'Reading...'
try:
buff = load(is1) #read file data and put it into a buffer
except IOError:
print "could not read from file"
c=0
for j in range(MAX): #check data
if int(buff[j])!=j:
print "Data is incorrect\n"
c=1
break
if c==0:
print "Data is correct"
class error: #class error#
def rdstate(self): #show the error state
try:
file = open("GROUP1.txt",'r')
return 0
except IOError:
return 4
def good(self): #check no error occur to open the file
try:
file = open("GROUP1.txt",'r')
return 1
except IOError:
return 0
def eof(self): #check end-of-file
try:
file = open("GROUP1.txt",'r')
return 1
except IOError:
return 0
def fail(self): #check any error has come?
try:
file = open("GROUP1.txt",'r')
return 0
except IOError:
return 4
def bad(self): #file can't be open
try:
file = open("GROUP1.txt",'r')
return 0
except IOError:
return 4
try:
file = open("GROUP1.txt",'r')
except IOError:
print "can't open output file"
print 'file =',hex(id(file))
file = error()
print 'Error state =',file.rdstate()
print 'good() =',file.good()
print 'eof() =',file.eof()
print 'fail() =',file.fail()
print 'bad() =',file.bad()
from pickle import dump,load
class person: #class of persons
def getData(self): #get person's data
self._name = raw_input("Enter name: ") #person's name
self._age = input("Enter age: ") #person's age
def showData(self): #display person's data
print " Name:",self._name
print " Age:",self._age
def diskIn(self,pn,c): #read person number pn from file
infile = open("PERSFILE.txt",'r+b') #open file
for j in range(c):
self = load(infile) #read one person
if j==pn:
return self;
def diskOut(self): #write person to end of file
outfile = open("PERSFILE.txt",'ab') #open file
dump(self, outfile) #write to it
outfile.close()
def diskCount(self,c): #return number of persons in file
return c
p = person() #make an empty person
c=0
outfile = open("PERSFILE.txt",'wb') #open file to delete its containt
outfile.close()
while True: #save person to disk
print "\nEnter person's data: "
p.getData() #get data
p.diskOut() #write to disk
c += 1
ch = raw_input("Enter another person (y/n)? ")
if(ch == 'n'): #untill user enters 'n'
break
c = p.diskCount(c) #how many persons are in file?
print 'There are',c,'persons in file'
print ''
for j in range(c): #for each person
print 'person'
p = p.diskIn(j,c) #read person from disk
p.showData() #display person
from sys import getsizeof
from pickle import dump,load
employee_type = ["tmanager","tscientist","tlaborer"]
(tmanager,tscientist,tlaborer) = (0,1,2) #Atteching the names with number
LEN = 32 #maximum length of last names
MAXEM = 100 #maximum length of employees
class employee: #employee class
__name = [None]*LEN #employee name
__n = 0 #current number of employee
__arrap = [] #array of ptrs to emps
def getdata(self): #get data from user
self.__name = raw_input(" Enter last name: ")
self.__number = input(" Enter number: ") #employee number
def putdata(self): #display data
print ' Name:',self.__name
print ' Number:',self.__number
def get_type(self): #return the type of object
if getsizeof(self)==getsizeof(manager()):
return tmanager
elif getsizeof(self)==getsizeof(scientisit()):
return tscientist
elif getsizeof(self)==getsizeof(laborer()):
return tlaborer
else:
print 'Bad employee type'
return tmanager
def add(self): #add employee to list in memory
print "'m' to add manager \n's' to add a scientist\n'l' to add a laborer"
ch = raw_input("Enter selection: ")
#create specific employee type
if ch=='m':
employee.__arrap.append(manager())
elif ch=='s':
employee.__arrap.append(scientist())
elif ch=='l':
employee.__arrap.append(laborer())
else:
print 'Unknown command'
employee.__arrap[employee.__n].getdata() #get employee data from user
employee.__n += 1 #increase the number of employee
def display(self): #display all employees
for j in range(employee.__n):
print j+1, #display number
#display type
if employee.__arrap[j].get_type()==tmanager:
print '. Type: Manager'
elif employee.__arrap[j].get_type()==tscientist:
print '. Type: scientist'
elif employee.__arrap[j].get_type()==tlaborer:
print '. Type: laborer'
else:
print '. Unknown type'
employee.__arrap[j].putdata() #display employee data
def write(self): #write all current memory object to file
ouf = open("EMPLOY.txt",'w')
ouf.close()
print 'Writing',employee.__n,'employees.'
try:
ouf = open("EMPLOY.txt",'ab') #open file in binary mode
except IOError:
print "could not open file"
for j in range(employee.__n): #for every employee objects
etype = employee.__arrap[j].get_type() #get its type
dump(etype, ouf) #write type to file
#find its size
if etype == tmanager:
size = getsizeof(manager)
elif etype == tscientist:
size = getsizeof(scientist)
elif etype == tlaborer:
size = getsizeof(laborer)
try:
dump(employee.__arrap[j], ouf) #write employee object to file
except IOError:
print "could not write to file"
ouf.close() #close the file
def read(self,c): #read data for all employee from file into memory
try:
inf = open("EMPLOY.txt",'rb') #open file into binary mode
except IOError:
print "could not open file"
employee.__n = 0 #no employee in memeory yet
for j in range(c):
try:
etype = load(inf) #read type of next employee
except IOError:
print "could not read type from file"
if j==c:
break
if etype == tmanager: #make new employee of correct type
employee.__arrap[employee.__n] = manager()
size = getsizeof(manager)
elif etype == tscientist:
employee.__arrap[employee.__n] = scientist()
size = getsizeof(scientist)
elif etype == tlaborer:
employee.__arrap[employee.__n] = laborer()
size = getsizeof(laborer)
else:
print 'Unknown type in file'
try:
employee.__arrap[employee.__n] = load(inf) #read data from file into it
except IOError:
print "could not read type from file"
employee.__n += 1 #count employee
print 'Reading',employee.__n,'employees'
class manager(employee): #management class
__title = [None]*LEN #'vice-president' etc.
def getdata(self):
employee.getdata(self)
self.__title = raw_input(" Enter Title: ")
self.__dues = input(" Enter golf club dues: ") #golf club dues
def putdata(self):
employee.putdata(self)
print ' Title:',self.__title
print ' golf club dues:',self.__dues
class scientist(employee): #scientist class
def getdata(self):
employee.getdata(self)
self.__pubs = raw_input(" Enter Number of pubs: ") #number of publication
def putdata(self):
employee.putdata(self)
print ' Number of publications:',self.__pubs
class laborer(employee): #labour class
pass
z = 0
while True:
print "\n'a' --to add data for an employee \n'd' --display data for all employees\n'w' --write all employee data to file"
print "'r' --read all employee data to file\n'x' --exit"
ch = raw_input("Enter selection: ")
if ch == 'a': #add an employee to list
z += 1
employee().add()
elif ch == 'd': #display all employess
employee().display()
elif ch == 'w': #write employee to file
employee().write()
elif ch == 'r': #read all employee from file
employee().read(z)
elif ch == 'x': #exit program
break;
else:
print 'Unknown command'
class Distance: #Distance class
def __init__(self,ft=0,inc=0): #constructor
self.__feet = ft
self.__inches = inc
def gd(d): #get length from user (friend function)
d._Distance__feet = input('\nEnter feet:')
d._Distance__inches = input('Enter inches:')
def sd(d): #display distance (friend function)
print d._Distance__feet , '\' -' , d._Distance__inches , '\"'
#define distances
dist1 = Distance()
dist2 = Distance()
#define and initialize dist3
dist3 = Distance(11,6.25)
#get dist1 and dist2 from user
print 'Enter two Distance value:',
gd(dist1)
gd(dist2)
#display all lengths
print '\ndist1 = ',;sd(dist1)
print 'dist2 = ',;sd(dist2)
print 'dist3 = ',;sd(dist3)
from pickle import dump,load
class Distance: #Distance class
def __init__(self,ft=0,inc=0): #constructor
self.__feet = ft
self.__inches = inc
def gd(d): #get length from user (friend function)
d._Distance__feet,dummy,dummy,d._Distance__inches,dummy = input(),raw_input(),raw_input(),input(),raw_input()
def sd(d): #display distance (friend function)
print d._Distance__feet , '\' -' , d._Distance__inches , '\"'
dist1 = Distance()
ofile = open('Dist.txt','w') #create and open a file
z=0
while True:
print 'Enter Distance:'
gd(dist1) #get distance from user
dump(dist1,ofile) #write it to file
z += 1
ch = raw_input("Enter another person (y/n)? ")
if(ch == 'n'):
break
ofile.close() #close file
ifile = open('Dist.txt','r') #open same file in read mode
print '\nContents of disk file is:'
for j in range(z):
dist1 = load(ifile) #read dist from stream
print 'Distance =',;sd(dist1) #display dist
#test data
ch = 'x'
j=77
d=67890.12345
str1 = "Kafka"
str2 = "Freud"
#print data
print 'ch =',ch
print 'j =',j
print 'd = %.2f' %d
print 'str1 =',str1
print 'str2 =',str2
from sys import argv
print 'argc =',len(argv) #number of arguments
for j in range(len(argv)): #display arguments
print 'Argument',j,'=',argv[j]
from sys import argv
if not(len(argv)==2):
print "Format: otype filename"
else:
#Open a file
infile=open(argv[1],'r')
#In case file cannot open
if(not(infile)):
print "Cannot open file."
else:
#Read file
ch=In.read()
print ch
In.close()
s1 = "\nToday's winning number is "
n1 = "17982"
outfile = open("PRN1.txt",'w') #open the file
#send data to file
outfile.write(s1)
outfile.write(n1)
outfile.write("\x0C")
outfile.close()
from sys import argv
if not(len(argv)==2):
print "Format: otype filename"
else:
#Open a file
infile=open(argv[1],'r')
#In case file cannot open
if(not(infile)):
print "Cannot open file."
else:
outfile = open("PRN1.txt")
ch=In.read()
print ch
In.close()