gpt4 book ai didi

python - pickle Boost.Python 公开的枚举

转载 作者:太空狗 更新时间:2023-10-30 01:03:56 26 4
gpt4 key购买 nike

是否有可能 pickle (使用 cPickle)已使用 Boost.Python 公开的枚举?我已经使用描述的第一种方法成功 pickle 了其他对象 here ,但这些似乎都不适用于枚举类型,而且默认情况下对象似乎不可 pickle 。

最佳答案

不像模块中那样。我被告知这是应该可能的,但是 enum_ 语句的工作方式阻止了这种情况。

您可以在 python 端解决这个问题。在某处(可能在 __init__.py 文件中)做这样的事情:

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()

这将使一切都变得很好。

关于python - pickle Boost.Python 公开的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3214969/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com