Ejemplo n.º 1
0
    def test_multiple_not_native_python_types(self):
        @lifting.lift_io(encoder_hook=ut.my_cool_encoder,
                         decoder_hook=ut.my_cool_decoder)
        def my_cool_function(x: ut.MyCoolClass, y: ut.MyCoolClass,
                             z: ut.MyCoolClass) -> ut.MyCoolResult:
            return x.mul(), y.mul(), z.mul()

        # # runs as normal
        cool_input = ut.MyCoolClass(2, 3.0)
        expected_cool_result = (
            ut.MyCoolResult(6.0),
            ut.MyCoolResult(6.0),
            ut.MyCoolResult(6.0),
        )
        res = my_cool_function(cool_input, cool_input, cool_input)
        assert res == expected_cool_result

        # cape handler runs on bytes
        cool_input_ser = serde.serialize(cool_input,
                                         cool_input,
                                         cool_input,
                                         encoder=ut.my_cool_encoder)
        cape_handler = my_cool_function.as_cape_handler()
        cool_result_ser = cape_handler(cool_input_ser)
        cool_result = serde.deserialize(cool_result_ser,
                                        decoder=ut.my_cool_decoder)
        assert cool_result == expected_cool_result
Ejemplo n.º 2
0
 def test_lifted_capehandler_multiple_inputs(self):
     lifted_multiple_identity = lifting.lift_io(ut.multiple_identity)
     inputs_ser = serde.serialize(1, 2, z=4)
     result = lifted_multiple_identity.as_cape_handler()(inputs_ser)
     result_deser = serde.deserialize(result)
     assert (1, 2) == result_deser[:2]
     assert 4 == result_deser[2]
Ejemplo n.º 3
0
        def cape_handler(input_bytes):
            try:
                args, kwargs = serde.deserialize(
                    input_bytes, decoder=decoder_hook, as_signature=True
                )
            except ValueError:
                raise ValueError(
                    "Couldn't deserialize the function's input with MessagePack."
                    "Make sure your input is serialized with MessagePack manually or "
                    "by setting use_serdio=True in Cape.run or Cape.invoke"
                )
            _check_inputs_match_signature(self._func, args, kwargs)
            output = self._func(*args, **kwargs)

            output_blob = serde.serialize(output, encoder=encoder_hook)
            return output_blob
Ejemplo n.º 4
0
    def test_module_docstring_example(self):
        @lifting.lift_io(encoder_hook=ut.my_cool_encoder,
                         decoder_hook=ut.my_cool_decoder)
        def my_cool_function(x: ut.MyCoolClass) -> ut.MyCoolResult:
            return x.mul()

        # runs as normal
        cool_input = ut.MyCoolClass(2, 3.0)
        expected_cool_result = ut.MyCoolResult(6.0)
        res = my_cool_function(cool_input)
        assert res == expected_cool_result

        # cape handler runs on bytes
        cool_input_ser = serde.serialize(cool_input,
                                         encoder=ut.my_cool_encoder)
        cape_handler = my_cool_function.as_cape_handler()
        cool_result_ser = cape_handler(cool_input_ser)
        cool_result = serde.deserialize(cool_result_ser,
                                        decoder=ut.my_cool_decoder)
        assert cool_result == expected_cool_result
Ejemplo n.º 5
0
 def test_lifted_capehandler(self, x):
     lifted_identity = lifting.lift_io(ut.identity)
     x_ser = serde.serialize(x)
     result = lifted_identity.as_cape_handler()(x_ser)
     result_deser = serde.deserialize(result)
     assert x == result_deser