我想持久地保留来自reverend.thomas.Bayes
的对象。当然,如果我尝试直接pickle其中一个类,我会得到:
TypeError: can't pickle instancemethod objects
为了解决这个问题,我尝试声明两个函数:
import types
from itertools import chain
from copy import copy
from reverend.thomas import Bayes
def prepare_bayes_for_pickle(bayes_obj):
dic = copy(bayes_obj.__dict__) #I also tried using deepcopy instead of copy
for k in dic:
if type(k) == types.MethodType:
dic.pop(k)
return dic
def reconstruct_bayes_from_pickle(bayes_dic):
b = Bayes()
# Merge b with bayes_dic, with bayes_dic taking precedence
dic = dict(chain(bayes_dic, b))
b.__dict__ = dic
return b
基本上,我尝试复制对象的__dict__
,并尝试通过测试types.MethodType
的类型来删除instancemethod
。
然后我会通过创建一个新的Bayes
对象来重建对象,然后将它与bayes_dic
合并在一起(在它被unpickle之后)。
但是,我还没有提到第二个方法,因为我仍然不能pickle从prepare_bayes_for_pickle
返回的对象而不得到原始错误。
更好的解决方案是将__getstate__
方法添加到Bayes
类中(附带__setstate__
):
import types
from reverend.thomas import Bayes
def Bayes__getstate__(self):
state = {}
for attr, value in self.__dict__.iteritems():
if not isinstance(value, types.MethodType):
state[attr] = value
elif attr == 'combiner' and value.__name__ == 'robinson':
# by default, self.combiner is set to self.robinson
state['combiner'] = None
return state
def Bayes__setstate__(self, state):
self.__dict__.update(state)
# support the default combiner (an instance method):
if 'combiner' in state and state['combiner'] is None:
self.combiner = self.robinson
Bayes.__getstate__ = Bayes__getstate__
Bayes.__setstate__ = Bayes__setstate__
现在Bayes
类总是可以被pickle和取消pickle,而无需额外的处理。
我确实看到类有一个self.cache = {}
映射;也许在腌制的时候应该把它排除在外?在__getstate__
中忽略它,在__setstate__
中调用self.buildCache()
。
k
是一个键,即属性/方法名。您需要测试属性本身:
if type(dic[k]) == types.MethodType:
^~~~~~ here
我更喜欢使用理解式;你还应该使用isinstance
:
dic = dict((k, v) for k, v in bayes_obj.__dict__
if not isinstance(v, types.MethodType))
这听起来就像用方钉来适应圆孔。如何使用pickle来pickle参数,然后unpickle来重建reverand.Thomas.Bayes
对象?
>>> from collections import namedtuple
>>> ArgList = namedtuple('your', 'arguments', 'for', 'the', 'reverand')
>>> def pickle_rtb(n):
... return pickle.dumps(ArgList(*n.args))
...
>>> def unpickle_rtb(s):
... return reverand.Thomas.Bayes(*pickle.loads(s))
...
>>> s = pickle_rtb(reverand.Thomas.Bayes(1, 2, 3, 4, 5)) # note arguments are a guess
>>> rtb = unpickle_norm(s)
受SO问题的启发