腌制Boost.Python暴露的枚举 [英] Pickling an enum exposed by Boost.Python
问题描述
是否可以(通过cPickle腌制)Boost.Python公开的枚举?我已经使用描述的第一种方法成功腌制了其他对象 ,但这似乎都不适用于枚举类型,默认情况下,这些对象似乎也不是可腌制的.
Is it possible to pickle (using cPickle) an enum that has been exposed with Boost.Python? I have successfully pickled other objects using the first method described here, but none of that seems to apply for an enum type, and the objects don't seem to be pickleable by default.
推荐答案
与模块中不同.据了解,这是可能的,但是enum_语句的工作方式可以防止这种情况.
Not as they are in the module. I am given to understand that this is SUPPOSED to be possible, but the way the enum_ statement works prevents this.
您可以在python方面解决此问题.在某个地方(可能在__init__.py文件中)执行以下操作:
You can work around this on the python side. Somewhere (probably in a __init__.py file) do something like this:
import yourmodule
def isEnumType(o):
return isinstance(o, type) and issubclass(o,int) and not (o is int)
def _tuple2enum(enum, value):
enum = getattr(yourmodule, enum)
e = enum.values.get(value,None)
if e is None:
e = enum(value)
return e
def _registerEnumPicklers():
from copy_reg import constructor, pickle
def reduce_enum(e):
enum = type(e).__name__.split('.')[-1]
return ( _tuple2enum, ( enum, int(e) ) )
constructor( _tuple2enum)
for e in [ e for e in vars(yourmodule).itervalues() if isEnumType(e) ]:
pickle(e, reduce_enum)
_registerEnumPicklers()
这将使所有泡菜都很好.
This will make everything pickle just fine.
这篇关于腌制Boost.Python暴露的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!