jeudi 26 novembre 2015

Pickle module in python program?

what does the pickle module do?? I came across a program where we input details and it gets printed at the end. Here ,the pickle functions are implemented(only dump,load). What is supposed to happen in this program?? I have typed the program exactly the way it is.

import pickle
import os
class Teacher:
   def __init__(self):
       self.Tcode=0
       self.Tname=''
       self.subject=''
def read_data(self):
       self.Tcode=input("Enter the teacher's code: ")
       self.Tname=raw_input("Enter the teacher's name: ")
       self.subject=raw_input("Enter the teacher's subject: ")
       f=open('Teacherdetails.dat','w+')
       f.write(self.Tcode)
       f.write(self.Tname)
       f.write(self.subject)
def display(self):
      print "The teacher's code: ",self.Tcode
      print "The teacher's name is: ",self.Tname
      print "The teacher's subject:",self.subject

def main():  
     if os.path.isfile('Teacherdetails.dat'):
            f=open('Teacherdetails.dat','r+')
            T=Teacher()
            while True:
                T.read_data()
                pickle.dump(T,f)
                ch=raw_input("continue,y/n")
                if(ch=='n'):
                    break
            print"Teacher details"
            f.seek(0,0)
            while True:
                T=pickle.load(f)
                T.display()

 main()

Aucun commentaire:

Enregistrer un commentaire