checkpointing in python to catch the runtime state

194 Views Asked by At

I have a problem to make my code more self-healable. Eg: I execute a method 1 to load the data from a CSV into the Vertica database. I have another method 2 to check if the number of rows in the database and the number of lines in CSV file is same. If the number of lines doesn't match, then I was thinking of calling the method 2 from the point where it called the query to load data from CSV into the database.

I was thinking of a checkpointing strategy for this problem. like, maintain some points in the code where the errors usually occur and recalling them at other points.

I already tried using pickle module in python, but came to know that pickle can only save objects, classes, variables etc. can't save the point from where I can actually execute a method.

i have provided some demo code:

import pickle        
class Fruits:
  def apple(self):
    filehandler= open ("Fruits.obj","wb")
    print "apple"
    pickle.dump(self,filehandler)
    print "mapple"
    filehandler.close()
  def mango(self):
    filehandler = open("Fruits.obj","rb")
    print "mango"
    obj=pickle.load(filehandler)
    obj.apple()

general = Fruits()
general.apple()
general.mango()

the output of above program is:
apple
mapple
mango
apple
mapple

I want my code to execute such that when mango method calls apple method, it must execute from the point of only print "mapple". it must not execute the whole method.

please do provide me some insight on how to solve this problem.

thanks in advance

1

There are 1 best solutions below

0
On

Note:
Your code doesn't work at all. filehandler in def mango(... IS NOT the same as filehandler in def apple(.... Therfore, the file opend in def mango(... are never closed.

Add a if condidtion to def apple, you don't need pickle at all.

def apple(self, mango=False):
    if not a´mango:
        filehandler= open ("Fruits.obj","wb")
        ...

    print "mapple"        
    ...

 def mango(self):
    filehandler = open("Fruits.obj","rb")
    ...
    obj.apple(True)