[英]checkpointing in python to catch the runtime state
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.
例如:我执行方法1,将数据从CSV加载到Vertica数据库中。 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.
我有另一种方法2来检查数据库中的行数和CSV文件中的行数是否相同。 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.
如果行数不匹配,那么我正在考虑从方法2调用查询以将数据从CSV加载到数据库中开始。
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. 我已经尝试在python中使用pickle模块,但后来发现pickle只能保存对象,类,变量等。不能保存我实际可以执行方法的地方。
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". 我希望我的代码能够执行,以便当芒果方法调用apple方法时,它必须仅从打印“ mapple”的角度执行。 it must not execute the whole method.
它不能执行整个方法。
please do provide me some insight on how to solve this problem. 请提供一些有关如何解决此问题的见解。
thanks in advance 提前致谢
Note :
注意事项 :
Your code doesn't work at all.您的代码根本不起作用。
filehandler
indef mango(...
IS NOT the same asfilehandler
indef apple(...
. Therfore, the file opend indef mango(...
are never closed .filehandler
在def mango(...
是不一样filehandler
在def apple(...
。Therfore,在该文件中运行结束def mango(...
是从来不关 。
Add a if condidtion
to def apple
, you don't need pickle
at all. 向
def apple
添加if condidtion
,则完全不需要pickle
。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.