Rename Etched by Boost.Python

Is it possible to saw (using cPickle) an enumeration that was exposed using Boost.Python? I have successfully pickled other objects using the first method described here , but none of them are suitable for the enumeration type, and the objects are apparently not legible by default.

+5
source share
1 answer

Not like in the module. It has been made clear to me that this is TOTALLY possible, but the way enum_ works, prevents this.

You can get around this on the python side. Somewhere (maybe in the __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 perfectly fit.

+6

All Articles