def pb2val(pb): whichone = pb.WhichOneof("v") if whichone == 'shape_val': shape_ = Shape.__new__(Shape) shape_.__setstate__(getattr(pb, whichone)) return shape_ elif whichone == 'type_val': return BaseType.pb2cls(getattr(pb, whichone))[0] elif whichone == 'list_val': _list = [pb2val(_pb) for _pb in pb.list_val.v] return np.asarray(_list) if getattr(pb.list_val, 'numpy', False) else _list elif whichone == 'set_val': return set([pb2val(_pb) for _pb in pb.set_val.v]) elif whichone == 'tuple_val': return tuple([pb2val(_pb) for _pb in pb.tuple_val.v]) elif whichone == 'nts_val': return TypeShape.from_pb(pb.nts_val) elif whichone == 'dict_val': # return dict([(elem.name, pb2val(elem.v)) for elem in pb.dict_val.vs]) return dict([pb2val(elem) for elem in pb.dict_val.v]) else: attr = str(whichone) if attr != 'None': return getattr(pb, attr) return None
def __setstate__(self, state): if isinstance(state, str) or isinstance(state, bytes): _proto_ob = TypeShapeProto() _proto_ob.ParseFromString(state) elif isinstance(state, TypeShapeProto): _proto_ob = state else: return self.dtype = BaseType.pb2cls(_proto_ob.dtype_val) self.shape = Shape.__new__(Shape) self.shape.__setstate__(_proto_ob.shape_val)
def from_pb(cls, nts_proto): dtype = BaseType.pb2cls(nts_proto.dtype_val)[0] shape = Shape.__new__(Shape) shape.__setstate__(nts_proto.shape_val) result = TypeShape(dtype, shape) return result