# 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:
```bash
pip install classy-dataclasses
```
```bash
# 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<ColorSystem>,
- **rgb_value** is a nested dataclass
- **global_color_system** is a static class variable
```python
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
```python
# 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
```python
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](LICENSE) file for details.
---
For more information, please refer to the documentation or the source code.
Raw data
{
"_id": null,
"home_page": "https://github.com/Nobbettt/classy-dataclasses",
"name": "classy-dataclasses",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "dataclass, serializable, deserializable, static class variables, copyable, JSON, nested, resetable, flexible, encode, decode, classy",
"author": "Norbert Laszlo",
"author_email": "hello@norbert.laszlo.com",
"download_url": "https://files.pythonhosted.org/packages/cf/5f/1d6b0ddcd451e6bf15d4676f11d6b9d19c7f39638d740ba784e1d263d98f/classy_dataclasses-1.0.2.tar.gz",
"platform": null,
"description": "# Classy Dataclasses\n\n**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.\n\n## Features \u2705\n\n- **Serialization**: Effortless serialization and deserialization of dataclasses to and from JSON and Python dictionaries. Supports:\n - Supports nested dataclasses and enums\n - Customizable encoders and decoders.\n- **Data storage**: Save dataclasses to JSON files and recreate them from JSON data.\n- **Copyable**: Easily deep-copy dataclasses.\n- **Static variables**: Support for static class variables in dataclasses.\n- **Improved data modelling**: Model your data in a structured way without limitations.\n\n## Installation \ud83d\udee0\ufe0f\n\nTo install Classy Dataclasses, you can use pip:\n\n```bash\npip install classy-dataclasses\n```\n\n```bash\n# Alternatively, using poetry:\npoetry add classy-dataclasses\n```\n\nRequires Python 3.10 or higher.\n\n## Usage \ud83d\udca1\n\nHere\u2019s a quick example of how to use Classy Dataclasses showcasing the main features.\n\n### Data Modelling\n\nNote in the example,\n\n- **color_system** and **global_color_system** are of type Enum<ColorSystem>,\n- **rgb_value** is a nested dataclass\n- **global_color_system** is a static class variable\n\n```python\nfrom classy_dataclasses import ClassyDataclass, classy_field\nfrom dataclasses import dataclass\nfrom enum import Enum\n\ndef deserialize_name(x: str) -> str:\n return x.replace(\" \", \"_\").lower()\n\n\ndef serialize_name(x: str) -> str:\n return x.replace(\"_\", \" \").upper()\n\n\nclass ColorSystem(Enum):\n HEX = \"HEX\"\n RGB = \"RGB\"\n\n\n@dataclass\nclass RGB(ClassyDataclass):\n r: int = classy_field(default=None)\n g: int = classy_field(default=None)\n b: int = classy_field(default=None)\n\n @property\n def is_valid(self) -> bool:\n parts: list[int] = [self.r, self.g, self.b]\n return all([True if p >= 0 and p <= 255 else False for p in parts])\n\n@dataclass\nclass Color(ClassyDataclass):\n name: str = classy_field(default=\"\", decoder=deserialize_name, encoder=serialize_name)\n hex_value: float | None = classy_field(default=None)\n rgb_value: RGB = classy_field(default_factory=lambda: RGB())\n global_color_system: ColorSystem = classy_field(default=ColorSystem.HEX, is_static=True)\n color_system: ColorSystem = classy_field(default=ColorSystem.HEX)\n tags: list[str] = classy_field(default_factory=lambda: [])\n attributes: dict = classy_field(default_factory=lambda: {})\n```\n\n## Deserialization and Load\n\n```python\n# Example data\ncolor_dict: dict = {\n\"name\": \"SKY BLUE\",\n \"hex_value\": \"#1425e0\",\n \"rgb_value\": {\n \"r\": 20,\n \"g\": 37,\n \"b\": 224\n },\n \"global_color_system\": \"HEX\",\n \"color_system\": \"RGB\",\n \"tags\": [\"sky\", \"ocean\"],\n \"attributes\": {\n \"like\": True,\n \"favorite\": False\n }\n}\n\ncolor_json_str: str = json.dumps(color_dict)\n\ncolor_json_path: str = \"path_to_your_file/sky_blue.json\"\n\n# Initialize from dictionary (use this when you have a dictionary representing the dataclass)\ncolor_dataclass: Color = Color.from_dict(color_dict)\n\n# Initialize form JSON string (use this when you have a JSON-formatted string)\ncolor_dataclass: Color = Color.from_json(color_dict)\n\n# Initialize from json file (use this when you have a path to a JSON file)\ncolor_dataclass: Color = Color.load_from_json(color_json_path)\n```\n\n### Serialization and Save\n\n```python\nSerialize to dictionary\ncolor_dict: dict = color_dataclass.to_dict(serialize_fields=True)\n\n# Serialize JSON string\ncolor_dict: str = color_dataclass.to_json(serialize_fields=True)\n```\n\nNote, setting **serialize_fields=False** will convert the dataclass to a dictionary or JSON string without serializing its fields.\n\n## Contributing \u2764\ufe0f\u200d\ud83e\ude79\n\nContributions are welcome! If you have suggestions for improvements or new features, feel free to open an issue or submit a pull request.\n\n## License \ud83d\udcc3\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n\nFor more information, please refer to the documentation or the source code.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Superset of python dataclasses with additional features.",
"version": "1.0.2",
"project_urls": {
"Documentation": "https://github.com/Nobbettt/classy-dataclasses",
"Homepage": "https://github.com/Nobbettt/classy-dataclasses",
"Issues": "https://github.com/Nobbettt/classydataclass/issues",
"Repository": "https://github.com/Nobbettt/classy-dataclasses"
},
"split_keywords": [
"dataclass",
" serializable",
" deserializable",
" static class variables",
" copyable",
" json",
" nested",
" resetable",
" flexible",
" encode",
" decode",
" classy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e83d2a23a0aab78c7bf7cfb9171a6b2fc61db1b77540cda10c0dfdd0431acf8a",
"md5": "2f233141a2ebbc85f4e6e10c2fec2d18",
"sha256": "3d00f0a319543f330ab6fe0ac01a08f94727a3e141ec3ea6a76f27330895a173"
},
"downloads": -1,
"filename": "classy_dataclasses-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2f233141a2ebbc85f4e6e10c2fec2d18",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 7285,
"upload_time": "2024-12-28T20:48:46",
"upload_time_iso_8601": "2024-12-28T20:48:46.416034Z",
"url": "https://files.pythonhosted.org/packages/e8/3d/2a23a0aab78c7bf7cfb9171a6b2fc61db1b77540cda10c0dfdd0431acf8a/classy_dataclasses-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "cf5f1d6b0ddcd451e6bf15d4676f11d6b9d19c7f39638d740ba784e1d263d98f",
"md5": "777dba7ad304a3b9742d1641282e1286",
"sha256": "dee9b46fa05adb62a541ce902321ce0e59738eecf760bfd49a87398728b2c1d3"
},
"downloads": -1,
"filename": "classy_dataclasses-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "777dba7ad304a3b9742d1641282e1286",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 6217,
"upload_time": "2024-12-28T20:48:50",
"upload_time_iso_8601": "2024-12-28T20:48:50.209615Z",
"url": "https://files.pythonhosted.org/packages/cf/5f/1d6b0ddcd451e6bf15d4676f11d6b9d19c7f39638d740ba784e1d263d98f/classy_dataclasses-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-28 20:48:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Nobbettt",
"github_project": "classy-dataclasses",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "classy-dataclasses"
}