Skip to main content

Tiny serialisation and deserialisation library

Project description

avoine

A tiny serialisation and deserialisation library.

avoine does not implement any serialisation or deserialisation itself. Instead, it relies on the common Python pattern of load and dump functions (for file-like objects) and loads and dumps functions (for string-like objects).

To configure serialisation and deserialisation, use the avoine class:

import json
import tomllib

from avoine import avoine

# define a format for deserialisation (load -> from file, loads -> from string/bytes)
# and serialisation (dump -> from file, dumps -> from string/bytes) and set it as default
avoine.register("json", default=True, load=json.load, loads=json.loads, dump=json.dump, dumps=json.dumps)

# not all methods are required
avoine.register("toml", load=tomllib.load)

# register can be used again to update a format definition
avoine.register("toml", loads=tomllib.loads)

# the default format is what is used when the format is left unspecified
# it can be set or unset on its own
avoine.default = "toml"
avoine.default = None

# and retrieved with
print(avoine.default)

# you can unregister a format too
avoine.unregister("toml", default="json")

# the list of currently-registered formats can be retrieved with
avoine.formats()

# or for a specific method with
avoine.formats("load")

# all configuration can be reset with
avoine.clear()

Then, in your code, you can define dataclasses and add the AvoineBase mixin class, which will allow you to serialise/deserialise them from any registered format anywhere:

from dataclasses import dataclass
from avoine import AvoineBase

@dataclass
class MyStruct(AvoineBase):
    foo: str
    bar: MyInnerStruct

@dataclass
class MyInnerStruct(AvoineBase):
    baz: list[str]
    opt: str | None = None

with open("mystruct.json") as f:
    data = MyStruct.load(f)
    # kwargs for the underlying loader/dumper are passed down to them
    data.dumps(indent=2)

with open("mystruct.toml") as f:
    print(MyStruct.load(f, format="toml"))

Custom formats can be created by writing functions with the following signatures:

def load(fp: SupportsRead[AnyStr], **kwargs) -> dict[str, Any]:
    ...

def loads(s: AnyStr, **kwargs) -> dict[str, Any]:
    ...

def dump(obj: Any, fp: SupportsWrite[AnyStr], **kwargs) -> None:
    ...

def dumps(obj: Any, **kwargs) -> AnyStr:
    ...

Notes:

  • AnyStr can be str, bytes, or both
  • kwargs are optional

If a set of loading/dumping functions for a format do not match these signatures, wrapper functions can be written to transform them to a compatible signature.

License

avoine is in the public domain.

To the extent possible under law, classabbyamp has waived all copyright and related or neighboring rights to this work.

http://creativecommons.org/publicdomain/zero/1.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

avoine-0.1.0a2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

avoine-0.1.0a2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file avoine-0.1.0a2.tar.gz.

File metadata

  • Download URL: avoine-0.1.0a2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Void","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for avoine-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 37cb13bb252a3778419ed123ed543deb1bec7776305f2922fc43085f88fe20f2
MD5 70880c96f08037fb2865aa93977e19ef
BLAKE2b-256 4d05233baf62360796257b394c90c8f6b6910df1d327d893295c8a8204582edf

See more details on using hashes here.

File details

Details for the file avoine-0.1.0a2-py3-none-any.whl.

File metadata

  • Download URL: avoine-0.1.0a2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.13 {"installer":{"name":"uv","version":"0.9.13"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Void","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for avoine-0.1.0a2-py3-none-any.whl
Algorithm Hash digest
SHA256 711924715e51c2c257087e8bbdd673871af4cadca94efd1ec9e2672c7de3c6d3
MD5 a875f369fc0d1c2b11cd8566af1bd692
BLAKE2b-256 b48b4c583c2afb9612d36192f985ec33c11aa4655a845d4d3deadd004abb88ca

See more details on using hashes here.

Supported by

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