我如何在python中腌制一个嵌套的类?
我有一个嵌套类:
class WidgetType(object): class FloatType(object): pass class TextType(object): pass
..和一个引用嵌套类类型的对象(不是它的一个实例)
class ObjectToPickle(object): def __init__(self): self.type = WidgetType.TextType
尝试序列化ObjectToPickle类的实例会导致:
PicklingError:Can not pickle <class'setmanager.app.site.widget_data_types.TextType'>
有没有办法在python中腌制嵌套类?
pickle模块试图从模块中获取TextType类。 但是因为这个类是嵌套的,所以它不起作用。 jasonjs的建议将起作用。 这里是pickle.py中的行,负责错误信息:
try:
__import__(module)
mod = sys.modules[module]
klass = getattr(mod, name)
except (ImportError, KeyError, AttributeError):
raise PicklingError(
"Can't pickle %r: it's not found as %s.%s" %
(obj, module, name))
klass = getattr(mod, name)
在嵌套类的情况下不起作用。 为了演示如何在酸洗实例之前尝试添加这些行:
import sys
setattr(sys.modules[__name__], 'TextType', WidgetType.TextType)
此代码将TextType作为属性添加到模块中。 酸洗应该很好。 我不建议你使用这个黑客。
我知道这是一个非常古老的问题,但我从来没有明确地看到这个问题的令人满意的解决方案,除了重构结构代码的明显的,最可能是正确的答案之外。
不幸的是,它并不总是可行的做这样的事情,在这种情况下,作为最后的手段,也可以腌制的被另一个类中定义的类的实例。
__reduce__
函数的python文档声明您可以返回
可调用的对象,将被调用来创建该对象的初始版本。 元组的下一个元素将为此可调用对象提供参数。
因此,您所需要的只是一个可以返回相应类的实例的对象。 这个类本身必须是可选择的(因此,必须在__main__
级别生存),并且可以像下面这样简单:
class _NestedClassGetter(object):
"""
When called with the containing class as the first argument,
and the name of the nested class as the second argument,
returns an instance of the nested class.
"""
def __call__(self, containing_class, class_name):
nested_class = getattr(containing_class, class_name)
# return an instance of a nested_class. Some more intelligence could be
# applied for class construction if necessary.
return nested_class()
所有剩下的就是在__reduce__
方法中返回适当的参数:
class WidgetType(object):
class FloatType(object):
def __reduce__(self):
# return a class which can return this class when called with the
# appropriate tuple of arguments
return (_NestedClassGetter(), (WidgetType, self.__class__.__name__, ))
结果是一个嵌套的类,但实例可以被pickle(需要进一步的工作来转储/加载__state__
信息,但是根据__reduce__
文档,这是相对简单的)。
这种相同的技术(只需修改代码)可以应用于深度嵌套类。
一个完整的例子:
import pickle
class ParentClass(object):
class NestedClass(object):
def __init__(self, var1):
self.var1 = var1
def __reduce__(self):
state = self.__dict__.copy()
return (_NestedClassGetter(),
(ParentClass, self.__class__.__name__, ),
state,
)
class _NestedClassGetter(object):
"""
When called with the containing class as the first argument,
and the name of the nested class as the second argument,
returns an instance of the nested class.
"""
def __call__(self, containing_class, class_name):
nested_class = getattr(containing_class, class_name)
# make an instance of a simple object (this one will do), for which we can change the
# __class__ later on.
nested_instance = _NestedClassGetter()
# set the class of the instance, the __init__ will never be called on the class
# but the original state will be set later on by pickle.
nested_instance.__class__ = nested_class
return nested_instance
if __name__ == '__main__':
orig = ParentClass.NestedClass(var1=['hello', 'world'])
pickle.dump(orig, open('simple.pickle', 'w'))
pickled = pickle.load(open('simple.pickle', 'r'))
print type(pickled)
print pickled.var1
我最后要说的是记住其他答案所说的话:
如果你有能力这样做,考虑重新考虑你的代码,以避免嵌套类首先。
如果你使用dill
而不是pickle
,它会起作用。
>>> import dill
>>>
>>> class WidgetType(object):
... class FloatType(object):
... pass
... class TextType(object):
... pass
...
>>> class ObjectToPickle(object):
... def __init__(self):
... self.type = WidgetType.TextType
...
>>> x = ObjectToPickle()
>>>
>>> _x = dill.dumps(x)
>>> x_ = dill.loads(_x)
>>> x_
<__main__.ObjectToPickle object at 0x10b20a250>
>>> x_.type
<class '__main__.TextType'>
在这里获取莳萝:https://github.com/uqfoundation/dill
链接地址: http://www.djcxy.com/p/64821.html