Skip to main content

Deserialize and serialize types, in any order

Project description

tressed - Deserialize and serialize types, in any order.

Package version

Tressed is a easy to use pure python library to deserialize and serialize to and from Python types.


Install tressed using pip:

$ pip install tressed

Now we can get going! For simple usecases, you can use the default loader and dumper directly:

>>> from tressed import load, dump
>>> from pprint import pprint
>>>
>>>
>>> value = load([1, [2, 2], [3, 4]], tuple[int, set[float], complex])
>>> pprint(value)
(1, {2.0}, (3+4j))
>>>
>>> pprint(dump(value))
[1, [2.0], [3.0, 4.0]]

For more advanced use cases instantiate a Loader and/or a Dumper:

>>> from dataclasses import dataclass, field
>>> from pprint import pprint
>>> 
>>> from tressed import Loader
>>> from tressed.alias import to_camel
>>> 
>>> @dataclass
... class SomeDataclass:
...     some_field: str
...     some_default_field: str = "bar"
...     some_default_factory_field: list[int] = field(default_factory=lambda: [1, 2, 3])
...     other_field: tuple[int, str] = field(metadata={"alias": "OTHER"}, kw_only=True)
...     
>>> raw_value = {
...     "someField": "foo",
...     "OTHER": (2, "humbug"),
... }
... 
>>> loader = Loader(alias_fn=to_camel)
>>> value = loader.load(raw_value, SomeDataclass)
>>> pprint(value)
SomeDataclass(some_field='foo',
              some_default_field='bar',
              some_default_factory_field=[1, 2, 3],
              other_field=(2, 'humbug'))
>>>
>>> from tressed import Dumper
>>> from tressed.alias import to_pascal
>>>
>>> dumper = Dumper(alias_fn=to_pascal)
>>> pprint(dumper.dump(value))
{'OTHER': [2, 'humbug'],
 'SomeDefaultFactoryField': [1, 2, 3],
 'SomeDefaultField': 'bar',
 'SomeField': 'foo'}

Features

Supported type forms

Tressed uses the proposed PEP 747 TypeForm combined with mypy 1.19 to provide accurate types for the Loader.load function.

The following type forms are supported out of the box:

  • bool
  • int
  • float
  • str
  • complex
  • tuple[T1, ..., Tn], typing.Tuple[T1, ..., Tn]
  • tuple[T, ...], typing.Tuple[T, ...] (a.k.a homogeneous tuple)
  • typing.NamedTuple
  • list[T], typing.List[T]
  • set[T], typing.Set[T]
  • frozenset[T], typing.FrozenSet[T]
  • dict[K, V], typing.Dict[K, V]
  • typing.Literal[L1, .., Ln]
  • typing.TypeAliasType
  • T | None, typing.Optional[T]
  • T1 | .. | Tn, typing.Union[T1, .., Tn] (Untagged Union)
  • Annotated[T1 | ... | Tn, Discriminator(...)] (Discriminated Union)
  • enum.Enum
  • typing.TypedDict, typing_extensions.TypedDict (including support for PEP 728 closed and extra_items)
  • dataclasses.dataclass
  • typing.NewType[T]
  • ipaddress.{IPv4Address, IPv6Address, IPv4Interface, IPv6Interface, IPv4Network, IPv6Network}
  • uuid.UUID
  • pathlib.Path
  • datetime.{date,datetime,time} (ISO 8601 format, using datetime.{date,datetime,time}.fromisoformat)

It is easy to add support for custom types as needed when creating a loader.

Supported source types

Data can be loaded from the following basic types, matching types used by common serialization formats.

Additionally data can be loaded from argparse namespaces to support easily loading arguments into data types for simple CLI applications.

  • int
  • float
  • bool
  • str
  • list
  • dict
  • argparse.Namespace

Installation

Using pip:

Or using uv:

uv add tressed

Then just import tressed and get coding.

import tressed

Goals

  • Provide easy serialization and deserialization to and from built-in and standard library types.
  • Provide easy way to add support for custom types.
  • Provide easy yet powerful support for serializaiton and deserialization aliases.
  • Pure python, no additional runtime dependencies beyond the standard library.
  • Do as little work as possible to be suitable for CLIs, for example using lazy imports as much as possible.

Stretch goals

  • Add first class support for concurrent loading and dumping, with support for asyncio and threading, for example deserializing a list of M objects on N threads.
  • Use code generation internally similarly to dataclasses. Instead of doing metaprogramming each time, generate an actual function for the type that does (de)serialization. Interpret the first time, compile the second, using source or ast module, and then reuse the compiled function.

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

tressed-0.0.4.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

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

tressed-0.0.4-py3-none-any.whl (23.5 kB view details)

Uploaded Python 3

File details

Details for the file tressed-0.0.4.tar.gz.

File metadata

  • Download URL: tressed-0.0.4.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tressed-0.0.4.tar.gz
Algorithm Hash digest
SHA256 26d6f21ac1f6984f488f0920f80a382de396b5618b9782b7ca84759062e536a0
MD5 98f955d06af8b3bfd6bacab6873f8dc6
BLAKE2b-256 25727e2340a55fdbb8a73e59621effc07c25809c40d8c9936eb28d10df30072e

See more details on using hashes here.

File details

Details for the file tressed-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: tressed-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 23.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tressed-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d3694fc22c8a5a2b2c47bb5c54673a8ce68d9b02b643ec73da07bf4d2b616bc4
MD5 425567962a23529b0d51e46fe9cda14c
BLAKE2b-256 89507b205e2817c0336737976055cbcf315978d79a10d65c9da7898a05f46df6

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