将字典读入自己
read a dict into self
我有一个 class 正在做很多事情。最后,它把所有的东西都保存在泡菜里。当我重新运行此 class 时,我想阅读泡菜而不是重新做所有事情。不幸的是,如果我解开,变量总是空的。为什么会这样?
import pandas as pd
class Test:
def __init__(path, value):
# path points to a .txt file but its in the same folder as the pickle
data_path, data = os.path.split(path)
pickle_path = os.path.join(data_path, name.split('.')[1] + '.pickle'
if os.path.isfile(pickle_path):
self = pd.read_pickle(path)
else:
# do a ton of stuff and safe it as pickle afterwards
variable = Test(path, value)
在这种情况下,如果我从 pickle 中读取变量是空的,但如果我执行所有操作则变量是正确的...
如果我想缓存一些计算结果,我会 load/dump class 之外的对象,比如
pickle_path = os.path.join(data_path, name.split('.')[1] + '.pickle'
if os.path.isfile(pickle_path):
with open(pickle_path, 'rb') as f:
variable = pickle.load(f) # use cached results
else:
variable = Test() # do all the calculations
我有一个 class 正在做很多事情。最后,它把所有的东西都保存在泡菜里。当我重新运行此 class 时,我想阅读泡菜而不是重新做所有事情。不幸的是,如果我解开,变量总是空的。为什么会这样?
import pandas as pd
class Test:
def __init__(path, value):
# path points to a .txt file but its in the same folder as the pickle
data_path, data = os.path.split(path)
pickle_path = os.path.join(data_path, name.split('.')[1] + '.pickle'
if os.path.isfile(pickle_path):
self = pd.read_pickle(path)
else:
# do a ton of stuff and safe it as pickle afterwards
variable = Test(path, value)
在这种情况下,如果我从 pickle 中读取变量是空的,但如果我执行所有操作则变量是正确的...
如果我想缓存一些计算结果,我会 load/dump class 之外的对象,比如
pickle_path = os.path.join(data_path, name.split('.')[1] + '.pickle'
if os.path.isfile(pickle_path):
with open(pickle_path, 'rb') as f:
variable = pickle.load(f) # use cached results
else:
variable = Test() # do all the calculations