コード例 #1
0
    def deserialise_result(self, task_result: TaskResult):
        """
        Dserialises a TaskResult back into result and state
        
        :param task_result: the TaskResult
        :return: tuple of result and state
        """
        result = _convert_from_proto(task_result.result,
                                     self._known_proto_types,
                                     self._protobuf_converters)
        state = _convert_from_proto(task_result.state, self._known_proto_types,
                                    self._protobuf_converters)

        return result, state
コード例 #2
0
    def test_convert_dict_to_protofbuf(self):
        data = {
            'int': 123,
            'float': 1.23,
            'str': '123',
            'list': [1, 2, 3],
            'dict': {
                'a': 1,
                'b': 2
            },
            'dict_in_list': [1, {
                'a': 1
            }],
            'proto': Address(namespace="tests", type="test", id="id")
        }

        proto = _convert_to_proto(data, self.default_converters)
        reconsituted_data = _convert_from_proto(proto, [Address],
                                                self.default_converters)

        self.assertEqual(reconsituted_data['int'], 123)
        self.assertEqual(reconsituted_data['float'], 1.23)
        self.assertEqual(reconsituted_data['str'], '123')
        self.assertEqual(reconsituted_data['list'], [1, 2, 3])
        self.assertEqual(reconsituted_data['dict']['a'], 1)
        self.assertEqual(reconsituted_data['dict']['b'], 2)
        self.assertEqual(reconsituted_data['dict_in_list'][0], 1)
        self.assertEqual(reconsituted_data['dict_in_list'][1]['a'], 1)
        self.assertTrue(isinstance(reconsituted_data['proto'], Address))
        self.assertEqual(reconsituted_data['proto'].namespace, 'tests')
        self.assertEqual(reconsituted_data['proto'].type, 'test')
        self.assertEqual(reconsituted_data['proto'].id, 'id')
コード例 #3
0
 def test_to_and_from_protobuf(self):
     obj = self.MyType('my_val')
     converters = [self.MyConverter()]
     proto_message = _convert_to_proto(obj, converters)
     python_val = _convert_from_proto(proto_message, [], converters)
     self.assertIsInstance(proto_message, TestMyType)
     self.assertIsInstance(python_val, self.MyType)
     self.assertEqual(python_val.string_field, 'my_val')
コード例 #4
0
    def deserialise_args_and_kwargs(self, request: Any):
        """
        Deserialises Any into Python args and kwargs
        
        :param request: the protobuf message
        :return: tuple of args and kwargs
        """
        request = _convert_from_proto(request, self._known_proto_types,
                                      self._protobuf_converters)

        if isinstance(request, ArgsAndKwargs):
            args = _convert_from_proto(request.args, self._known_proto_types,
                                       self._protobuf_converters)
            kwargs = _convert_from_proto(request.kwargs,
                                         self._known_proto_types,
                                         self._protobuf_converters)
        else:
            args = request
            kwargs = {}

        return args, kwargs
コード例 #5
0
    def from_proto(self, proto: Message, default=None):
        """
        Converts from protobuf to Python
        
        :param proto: the protobuf message
        :param option default: an optional default value to return
        :return: the Python type or default value if deserialisation return None
        """

        result = _convert_from_proto(proto, self._known_proto_types,
                                     self._protobuf_converters)
        return result if result is not None else default
コード例 #6
0
    def deserialise_request(self, task_request: TaskRequest):
        """
        Dserialises a TaskRequest back into args, kwargs and state
        
        :param task_request: the TaskRequest
        :return: tuple of task args, kwargs and state
        """
        args, kwargs = self.deserialise_args_and_kwargs(task_request.request)

        state = _convert_from_proto(task_request.state,
                                    self._known_proto_types,
                                    self._protobuf_converters)
        return args, kwargs, state
コード例 #7
0
    def test_convert_list_to_protofbuf(self):
        data = [Address(namespace="tests", type="test", id="id"), 1, '123']

        proto = _convert_to_proto(data, _generate_default_converters())
        reconsituted_data = _convert_from_proto(proto, [Address],
                                                self.default_converters)

        self.assertTrue(isinstance(reconsituted_data[0], Address))
        self.assertEqual(reconsituted_data[0].namespace, 'tests')
        self.assertEqual(reconsituted_data[0].type, 'test')
        self.assertEqual(reconsituted_data[0].id, 'id')
        self.assertEqual(reconsituted_data[1], 1)
        self.assertEqual(reconsituted_data[2], '123')
コード例 #8
0
 def to_args_and_kwargs(self, request: Any) -> ArgsAndKwargs:
     """
     Unpacks Any into Protobuf ArgsAndKwargs
     
     :param request: the protobuf message
     :return: ArgsAndKwargs
     """
     unpacked = _convert_from_proto(request, self._known_proto_types,
                                    self._protobuf_converters)
     if isinstance(unpacked, ArgsAndKwargs):
         return unpacked
     else:
         args = TupleOfAny()
         args.items.append(request)
         return ArgsAndKwargs(args=args, kwargs=MapOfStringToAny())