Skip to main content

Automatic serialization of function inputs and outputs using MessagePack.

Project description

Serdio

Automatic serialization of function inputs and outputs using MessagePack.

Why Serdio?

Remotely executing Python code is complicated. A common pattern is to wrap the code into a function that maps serialized data to serialized data, however this leads to heavy amounts of boilerplate related to serialization of native and custom Python types.

Serdio makes this easy by handling this serialization boilerplate for you as much as possible. For any custom types, it only requires the user to provide a encoder/decoder helpers that break the types down into Python-native components. An example can be found below.

Installation

pip install serdio

Usage

Basic

Here, we use Serdio to lift a function that performs some simple math on native Python numbers.

@serdio.lift_io
def my_cool_function(x: int, y: float, b: float = 1.0) -> float:
    z = x * y
    z += b
    return z

bytes_handler: Callable[bytes, bytes] = my_cool_function.as_bytes_handler()

z = my_cool_function(2, 3.0)
assert z == 7.0

Now we can use the bytes_handler function on Serdio-encoded bytes:

xyb_bytes = serdio.serialize(2, 3.0, b=2.0)
zbytes = bytes_handler(xyb_bytes)

z = serdio.deserialize(zbytes)
assert z == 8.0

Using Serdio with Custom Types

In this example, we reproduce the above example with custom types MyCoolClass and MyCoolResult, instead of native Python numbers. To give Serdio a little guidance, we provide helper functions that can convert our custom types into Python native values and back (my_cool_encoder and my_cool_decoder).

The resulting function can operate on our custom types, while Serdio automatically applies the encoder/decoder helpers to function inputs and outputs.

@dataclasses.dataclass
class MyCoolResult:
    cool_result: float

    def shift(self, other: float) -> MyCoolResult:
        return MyCoolResult(self.cool_result + other)

@dataclasses.dataclass
class MyCoolClass:
    cool_int: float
    cool_float: int

    def mul(self) -> MyCoolResult:
        return MyCoolResult(self.cool_int * self.cool_float)

def my_cool_encoder(x):
    if dataclasses.is_dataclass(x):
        return {
            "__type__": x.__class__.__name__,
            "fields": dataclasses.asdict(x)
        }
    return x

def my_cool_decoder(obj):
    if "__type__" in obj:
        obj_type = obj["__type__"]
        if obj_type == "MyCoolClass":
            return MyCoolClass(**obj["fields"])
        elif obj_type == "MyCoolResult":
            return MyCoolResult(**obj["fields"])
    return obj

@serdio.lift_io(encoder_hook=my_cool_encoder, decoder_hook=my_cool_decoder)
def my_cool_function(a: MyCoolClass, b: float = 1.0) -> MyCoolResult:
    x: MyCoolResult = a.mul()
    return x.shift(b)

my_handler = my_cool_function.as_bytes_handler()

a = MyCoolClass(2, 3.0)
ab_bytes = serdio.serialize(a, b=2.0, encoder=my_cool_function.encoder)
c_bytes = my_handler(ab_bytes)
c = serdio.deserialize(c_bytes, my_cool_function.decoder)

assert c == my_cool_function(a, b=2.0)
print(c.cool_result)
# 8.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

serdio-1.1.0.tar.gz (18.6 kB view details)

Uploaded Source

Built Distribution

serdio-1.1.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file serdio-1.1.0.tar.gz.

File metadata

  • Download URL: serdio-1.1.0.tar.gz
  • Upload date:
  • Size: 18.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for serdio-1.1.0.tar.gz
Algorithm Hash digest
SHA256 44cc7fdac4dd782f43bb15dc384e7951711f704994e31b1f3e958dfaa23b0668
MD5 ac91d77b2d67233208ec260c61114c78
BLAKE2b-256 b4bf38bec30714d26ac2f755eff21f4ffb84bd02cb59eb6f6bc67b70c586654f

See more details on using hashes here.

File details

Details for the file serdio-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: serdio-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for serdio-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2be15cf357b95d47f5c7c0fe703ff305d2d422e21b8f8848d22d795e94a2ad2b
MD5 262637795ac9dc12577e68e531d6b27a
BLAKE2b-256 fb97ad67ed909477efdb4af57a2d089ee3eef8e7b63230c946febf6587571da5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page