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.0a1.tar.gz (3.5 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.0a1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: avoine-0.1.0a1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"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.0a1.tar.gz
Algorithm Hash digest
SHA256 7e38d62bd8b9210792e93e4a94fb97e89db105d0e393d3324efb9d5fd78b2ea9
MD5 f364ca1b3ba63ceb3781f60c08a254f2
BLAKE2b-256 a31c5f7f126e5f258e54243f2b1b4c68a293ca681a01e9e185bfa5f34cc4afda

See more details on using hashes here.

File details

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

File metadata

  • Download URL: avoine-0.1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"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.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 df2e57c114a89b68338f511c154fcaf44a53cd9d3bd156b51591ac0244a7c032
MD5 64d0375aae08c5aa85528337364ad731
BLAKE2b-256 b372646334a399ee40cf77fe391f50567a45614a203480f2fa244bc28b0ac9a3

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