Skip to main content

Superset of python dataclasses with additional features.

Project description

Classy Dataclasses

Classy Dataclasses is a Python library that extends the functionality of Python's built-in dataclasses, making it easier to serialize, deserialize, manage nested structures, and more.

Features ✅

  • Serialization: Effortless serialization and deserialization of dataclasses to and from JSON and Python dictionaries. Supports:
    • Supports nested dataclasses and enums
    • Customizable encoders and decoders.
  • Data storage: Save dataclasses to JSON files and recreate them from JSON data.
  • Copyable: Easily deep-copy dataclasses.
  • Static variables: Support for static class variables in dataclasses.
  • Improved data modelling: Model your data in a structured way without limitations.

Installation 🛠️

To install Classy Dataclasses, you can use pip:

pip install classy-dataclasses
# Alternatively, using poetry:
poetry add classy-dataclasses

Requires Python 3.10 or higher.

Usage 💡

Here’s a quick example of how to use Classy Dataclasses showcasing the main features.

Data Modelling

Note in the example,

  • color_system and global_color_system are of type Enum,
  • rgb_value is a nested dataclass
  • global_color_system is a static class variable
from classy_dataclasses import ClassyDataclass, classy_field
from dataclasses import dataclass
from enum import Enum

def deserialize_name(x: str) -> str:
    return x.replace(" ", "_").lower()


def serialize_name(x: str) -> str:
    return x.replace("_", " ").upper()


class ColorSystem(Enum):
    HEX = "HEX"
    RGB = "RGB"


@dataclass
class RGB(ClassyDataclass):
    r: int = classy_field(default=None)
    g: int = classy_field(default=None)
    b: int = classy_field(default=None)

    @property
    def is_valid(self) -> bool:
        parts: list[int] = [self.r, self.g, self.b]
        return all([True if p >= 0 and p <= 255 else False for p in parts])

@dataclass
class Color(ClassyDataclass):
    name: str = classy_field(default="", decoder=deserialize_name, encoder=serialize_name)
    hex_value: float | None = classy_field(default=None)
    rgb_value: RGB = classy_field(default_factory=lambda: RGB())
    global_color_system: ColorSystem = classy_field(default=ColorSystem.HEX, is_static=True)
    color_system: ColorSystem = classy_field(default=ColorSystem.HEX)
    tags: list[str] = classy_field(default_factory=lambda: [])
    attributes: dict = classy_field(default_factory=lambda: {})

Deserialization and Load

# Example data
color_dict: dict = {
"name": "SKY BLUE",
  "hex_value": "#1425e0",
  "rgb_value": {
    "r": 20,
    "g": 37,
    "b": 224
  },
  "global_color_system": "HEX",
  "color_system": "RGB",
  "tags": ["sky", "ocean"],
  "attributes": {
    "like": True,
    "favorite": False
  }
}

color_json_str: str = json.dumps(color_dict)

color_json_path: str = "path_to_your_file/sky_blue.json"

# Initialize from dictionary (use this when you have a dictionary representing the dataclass)
color_dataclass: Color = Color.from_dict(color_dict)

# Initialize form JSON string (use this when you have a JSON-formatted string)
color_dataclass: Color = Color.from_json(color_dict)

# Initialize from json file (use this when you have a path to a JSON file)
color_dataclass: Color = Color.load_from_json(color_json_path)

Serialization and Save

Serialize to dictionary
color_dict: dict = color_dataclass.to_dict(serialize_fields=True)

# Serialize JSON string
color_dict: str = color_dataclass.to_json(serialize_fields=True)

Note, setting serialize_fields=False will convert the dataclass to a dictionary or JSON string without serializing its fields.

Contributing ❤️‍🩹

Contributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.

License 📃

This project is licensed under the MIT License - see the LICENSE file for details.


For more information, please refer to the documentation or the source code.

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

classy_dataclasses-1.0.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

classy_dataclasses-1.0.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file classy_dataclasses-1.0.2.tar.gz.

File metadata

  • Download URL: classy_dataclasses-1.0.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.9 Darwin/24.1.0

File hashes

Hashes for classy_dataclasses-1.0.2.tar.gz
Algorithm Hash digest
SHA256 dee9b46fa05adb62a541ce902321ce0e59738eecf760bfd49a87398728b2c1d3
MD5 777dba7ad304a3b9742d1641282e1286
BLAKE2b-256 cf5f1d6b0ddcd451e6bf15d4676f11d6b9d19c7f39638d740ba784e1d263d98f

See more details on using hashes here.

File details

Details for the file classy_dataclasses-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: classy_dataclasses-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.11.9 Darwin/24.1.0

File hashes

Hashes for classy_dataclasses-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3d00f0a319543f330ab6fe0ac01a08f94727a3e141ec3ea6a76f27330895a173
MD5 2f233141a2ebbc85f4e6e10c2fec2d18
BLAKE2b-256 e83d2a23a0aab78c7bf7cfb9171a6b2fc61db1b77540cda10c0dfdd0431acf8a

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