Skip to main content

No project description provided

Project description

AnySerde

AnySerde is a Python package that simplifies the task of converting Python variables to and from serializable data. Whether you need to serialize Python objects to JSON, YAML, or other common formats, or deserialize data from these formats back into Python objects, AnySerde has got you covered.

The main difference from other serialization libraries is that AnySerde does not require any changes to your existing codebase such as adding decorators or adding parent classes.

Installation

You can easily install AnySerde using pip:

pip install any_serde

More package details on the PyPI page:

Usage

Using AnySerde is straightforward. Here's a quick overview of how to get started:

Converting Python Objects to Data

To convert a Python variable to a data format (e.g., JSON or YAML), you can use the to_data function. Here's an example with a dataclass:

from dataclasses import dataclass
import yaml
from any_serde import to_data

@dataclass
class SampleDataclass:
    foo: int
    bar: str
    baz: dict[str, list[bool]]

sample = SampleDataclass(
    foo=2,
    bar="some text",
    baz={"0": [], "8": [True, False, False]},
)
sample_data = to_data(SampleDataclass, sample)

with open("sample.yaml", "wt") as f:
    yaml.dump(sample_data, f)

This example dumps the data as yaml to a file, but you could easily use json or xml or any other format you want.

Converting Data to Python Objects

Conversely, if you have data in a specific format and want to convert it back to a Python object, you can use the from_data function. Here's a continuation of the previous example that turns the yaml file back into a python variable:

from any_serde import from_data

with open("sample.yaml", "rt") as f:
    sample_data = yaml.load(f, Loader=yaml.SafeLoader)

sample = from_data(SampleDataclass, sample_data)

Again, you can replace 'json' with the desired format.

Supported Object Types

Right now, AnySerde can convert these types to data:

  1. All python primitives except complex
  2. dictionaries
  3. lists
  4. sets
  5. tuples
  6. union types (but be careful of ambiguous serialization)
  7. Dataclasses
  8. Enums

As of writing this, AnySerde plans to support these types (but doesn't yet):

  1. NamedTuple
  2. Path
  3. Datetime
  4. all other builtin python types
  5. user-defined types (with user-defined to_data and from_data methods)

Contributing

Contributions to AnySerde are welcome! If you find a bug, have a feature request, or want to contribute code, please open an issue or submit a pull request on the GitHub repository.

License

AnySerde is available for any use (including commercial) and is distributed under the Apache License 2.0. See the LICENSE file for more details.

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

any_serde-0.2.18.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

any_serde-0.2.18-py3-none-any.whl (28.2 kB view details)

Uploaded Python 3

File details

Details for the file any_serde-0.2.18.tar.gz.

File metadata

  • Download URL: any_serde-0.2.18.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for any_serde-0.2.18.tar.gz
Algorithm Hash digest
SHA256 3c7db55631882326360e39f389c31d873956057aaec07acd4e6c45b259a163c8
MD5 856beb5bb59a68f9cdd6034085430fa4
BLAKE2b-256 a64b243e4e07f4a8699c5e5c36ff2a77f578aa9b3dc687ccab4e5cf425823d42

See more details on using hashes here.

File details

Details for the file any_serde-0.2.18-py3-none-any.whl.

File metadata

  • Download URL: any_serde-0.2.18-py3-none-any.whl
  • Upload date:
  • Size: 28.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for any_serde-0.2.18-py3-none-any.whl
Algorithm Hash digest
SHA256 a7f5c5a04e96598e8bdcfbe7c1aa14fafcdc68636d325fe2d6bbe4f29d1726f1
MD5 399b8494603221e8fa65f958a4dc6140
BLAKE2b-256 e9bf94d7bde9de842cc44ad3442410e07ec9c1bee4d34c2a325b1aacc917d5cd

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