def test_parse_list(): obj = [{'player': [{'role_id': 1, 'target_x': 23}, {'role_id': 2, 'target_x': 24}]}] proto_obj = CityAois() dict_to_protobuf(obj, proto_obj) assert proto_obj.CAS[0].player[0].role_id == 1 assert proto_obj.CAS[0].player[1].target_x == 24 obj2 = protobuf_to_dict(proto_obj) print (obj2) print (obj)
def test_parse_dict_call_backwards(): obj = {'player':[{'role_id':1,'target_x':23},{'role_id':2,'target_x':24}]} proto_obj = CityAoi() dict_to_protobuf(proto_obj,obj) assert (proto_obj.player[0].role_id == 1) assert (proto_obj.player[1].target_x == 24) obj2 = protobuf_to_dict(proto_obj) print (obj2) print (obj)
def publish(self, geospace, d): d["header"]["air_quality_version"] = "0.0.1" feed = FeedMessage() dict_to_protobuf.dict_to_protobuf(d, feed) raw = feed.SerializeToString() try: res = super().publish(Default.channel, geospace, raw) except MeshRPCException as e: raise
def publish(self, geospace, d): if isinstance(d, dict): d["header"]["gtfs_realtime_version"] = "2.0" feed = FeedMessage() dict_to_protobuf.dict_to_protobuf(d, feed) raw = feed.SerializeToString() else: raw = d try: res = super().publish(Default.topic, geospace, raw) except MeshRPCException as e: raise
def converts_to_proto(value, msg, raise_err=False): """ Boolean response if a dictionary can convert into the proto's schema :param value: <dict> :param msg: <proto object> :param raise_err: <bool> (default false) raise for troubleshooting :return: <bool> whether the dict can covert """ result = True try: dict_to_protobuf.dict_to_protobuf(value, msg) except TypeError as type_error: if raise_err: raise type_error result = False return result
def strip(value, msg): """ Strips all non-essential keys from the value dictionary given the message format protobuf raises ValueError exception if value does not have all required keys :param value: <dict> with arbitrary keys :param msg: <protobuf> with a defined schema :return: NEW <dict> with keys defined by msg, omits any other key """ dict_to_protobuf.dict_to_protobuf(value, msg) try: msg.SerializeToString() #raise error for insufficient input except EncodeError as encode_error: raise ValueError(str(encode_error)) output = dict_to_protobuf.protobuf_to_dict(msg) return output
def make_tensor_proto(data, dtype): tensor_proto = TensorProto() if isinstance(dtype, six.string_types): dtype = dtype_to_number[dtype] dim = [{'size': 1}] values = [data] if hasattr(data, 'shape'): dim = [{'size': dim} for dim in data.shape] values = list(data.reshape(-1)) tensor_proto_dict = { 'dtype': dtype, 'tensor_shape': { 'dim': dim }, number_to_dtype_value[dtype]: values } dict_to_protobuf.dict_to_protobuf(tensor_proto_dict, tensor_proto) return tensor_proto
def test_parse_dict(self): obj = {'player':[{'role_id':1,'target_x':23},{'role_id':2,'target_x':24}]} proto_obj = CityAoi() dict_to_protobuf(obj, proto_obj) self.assertEqual(proto_obj.player[0].role_id, 1) self.assertEqual(proto_obj.player[1].target_x, 24)
def test_parse_dict(self): obj = {"player": [{"role_id": 1, "target_x": 23}, {"role_id": 2, "target_x": 24}]} proto_obj = CityAoi() dict_to_protobuf(obj, proto_obj) self.assertEqual(proto_obj.player[0].role_id, 1) self.assertEqual(proto_obj.player[1].target_x, 24)