From c21a0d8f1fd44affea8f588767cf0de584dbb50f Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Thu, 20 Feb 2025 09:41:35 -0500 Subject: [PATCH] Tweak formatting --- .../python/converters-and-encryption.mdx | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/docs/develop/python/converters-and-encryption.mdx b/docs/develop/python/converters-and-encryption.mdx index 23a74f6d44..a7473b07bb 100644 --- a/docs/develop/python/converters-and-encryption.mdx +++ b/docs/develop/python/converters-and-encryption.mdx @@ -42,7 +42,10 @@ If you only need to change the encoding performed on your payloads -- by adding The `PayloadCodec` needs to implement `encode()` and `decode()` functions at a minimum. These should loop through all of a Workflow's payloads, perform all of your necessary marshaling, compression, or encryption steps in order, and set an `"encoding"` metadata field. -Here is an example that marshals and then compresses a payload using Python's [cramjam](https://github.com/milesgranger/cramjam) library to provide `snappy` compression: + +In this example the `encode` method marshals and then compresses a payload using Python's [cramjam](https://github.com/milesgranger/cramjam) library to provide `snappy` compression. +The `decode()` function implements the `encode()` logic in reverse: + ```python import cramjam @@ -60,19 +63,15 @@ class EncryptionCodec(PayloadCodec): ) for p in payloads ] -``` -The `decode()` function should implement the `encode()` logic in reverse. - -```python -async def decode(self, payloads: Iterable[Payload]) -> List[Payload]: - ret: List[Payload] = [] - for p in payloads: - if p.metadata.get("encoding", b"").decode() != "binary/snappy": - ret.append(p) - continue - ret.append(Payload.FromString(bytes(cramjam.snappy.decompress(p.data)))) - return ret + async def decode(self, payloads: Iterable[Payload]) -> List[Payload]: + ret: List[Payload] = [] + for p in payloads: + if p.metadata.get("encoding", b"").decode() != "binary/snappy": + ret.append(p) + continue + ret.append(Payload.FromString(bytes(cramjam.snappy.decompress(p.data)))) + return ret ``` This example verifies that an encoded payload matches the `binary/snappy` filetype -- i.e., that it was encoded using the same custom `encode()` function -- and if so, performs decompression followed by unmarshaling.