Can't overload python object while using pickle

Viewed 86

Taking inputs through input command and calling object from main file.

rafTur = input(' : ')
rafKat = input(' : ')
rafNo = input( : ')
rafIndex = input(' : ')
req.search_record(' : ')
req.search_record(rafTur, rafKat, rafNo, rafIndex)

req.py :

def search_record( rafTur, rafKat, rafNo, rafIndex= None):
   infile = open('noSqlDB', 'rb')
   flag = False

   while True:
       try:
           reading the oject from file

        sistem = pickle.load(infile)
        if sistem['rafTur'] == rafTur and sistem['rafKat'] == rafKat and sistem['rafNo'] == rafNo and rafIndex==None: # This is where the error happens
            print("Raf No : ",sistem['rafIndex'],"Raf data : ",sistem['rafIndexData'])
            print(":", rafIndex)
            flag = True
            break
        # display record if found and set flag
        if (sistem['rafTur'] == rafTur and sistem['rafKat'] == rafKat and sistem['rafNo'] == rafNo and sistem['rafIndex'] == rafIndex):
            print(sistem['rafIndexData'],"\n")
            flag = True
            break

    except EOFError:
        print("deb")
        break

   if flag == False:
       print('Record not Found')
       print()


   infile.close()

I'm trying to overload search_record() function. When I put = None to rafIndex variable the program exits with OFError and prints deb ( just for debugging ). The code works fine without = None command also second if works without = None command.

I tried changing if statement with == None to == '' it also doesn't work. I also tried assigning rafIndex to None manually by rafIndex = None but it also doesn't work.

Output

Using python 3.9 virtualenv in Clion

2 Answers

I think your problem is with the previous line where you try to load the pickled object. It would be very helpful if you can instead of catching the exception paste the stack trace here. Also for debugging it will be helpful for you if you print the 'sistem' object after performing the pickle.load

So the main problem is the variables and input was different type. So casting them to int and string will solve the problem.

Created a function for casting :

def toCast(rafTur, rafKat, rafNo, rafIndex):
    if rafTur != "":
        rafTur = str(rafTur) #Change this

    if rafKat != "":
        rafKat = int(rafKat)

    if rafNo != "":
        rafNo = int(rafNo)

    if rafIndex != "":
        rafIndex = int(rafIndex)


    return  rafTur,rafKat,rafNo,rafIndex

That function makes casting if function isn't empty after that i used this function in main like this :

elif choice == '3':

            rafTur = input('Rafın türünü giriniz : ')
            rafKat = input('Rafın katını giriniz : ')
            rafNo = input('Rafın Nosunu giriniz : ')
            rafIndex = input('Rafın indexini giriniz : ') 
            rafTur,rafKat,rafNo,rafIndex = req.toCast(rafTur, rafKat, rafNo, rafIndex)

            req.search_record(rafTur, rafKat, rafNo, rafIndex)

after that i rechanged if else's in search_record(rafTur, rafKat, rafNo, rafIndex) with this :

 def search_record(rafTur, rafKat, rafNo, rafIndex):
    infile = open('noSqlDB', 'rb')
    sistem = pickle.load(infile)
    flag = False

    # read to the end of file.
    for x in range((len(sistem)+1)):
        try:

            if (sistem['rafTur'].upper() == rafTur.upper() and rafKat == "" and rafNo == ""
                    and rafIndex == ""):

                print(sistem['rafTur'],sistem['rafNo'], sistem['rafIndex'], sistem['rafIndexData'])
                flag = True


            elif (sistem['rafTur'].upper() == rafTur.upper() and sistem['rafKat'] == rafKat and
                    rafNo == "" and rafIndex == ""):

                print(sistem['rafNo'], sistem['rafIndex'], sistem['rafIndexData'])
                flag = True

            elif (sistem['rafTur'].upper() == rafTur.upper() and sistem['rafKat'] == rafKat and sistem[
                'rafNo'] == rafNo and rafIndex == ""):

                print(sistem['rafIndex'], sistem['rafIndexData'])
                flag = True

            elif (sistem['rafTur'].upper() == rafTur.upper() and sistem['rafKat'] == rafKat and sistem[
                'rafNo'] == rafNo and sistem['rafIndex'] == rafIndex):

                print(sistem['rafIndexData'])
                flag = True

            sistem = pickle.load(infile)
        except EOFError:
            break

    if flag == False:
        print('Record not Found')
        print()

    # close the file
    infile.close()
Related