[英]checkpointing in python to catch the runtime state
我有一個問題使我的代碼更容易修復。 例如:我執行方法1,將數據從CSV加載到Vertica數據庫中。 我有另一種方法2來檢查數據庫中的行數和CSV文件中的行數是否相同。 如果行數不匹配,那么我正在考慮從方法2調用查詢以將數據從CSV加載到數據庫中開始。
我當時正在考慮針對此問題的檢查點策略。 例如,在代碼中維護通常會發生錯誤的某些點,並在其他地方重新調用它們。
我已經嘗試在python中使用pickle模塊,但后來發現pickle只能保存對象,類,變量等。不能保存我實際可以執行方法的地方。
我提供了一些演示代碼:
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
我希望我的代碼能夠執行,以便當芒果方法調用apple方法時,它必須僅從打印“ mapple”的角度執行。 它不能執行整個方法。
請提供一些有關如何解決此問題的見解。
提前致謝
注意事項 :
您的代碼根本不起作用。filehandler
在def mango(...
是不一樣filehandler
在def apple(...
。Therfore,在該文件中運行結束def mango(...
是從來不關 。
向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.