Skip to content

Lightweight library for simple dataclass serialization and deserialization to dict and JSON.

Notifications You must be signed in to change notification settings

Rub1kCube/rich-dataclass

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rich-dataclass

A minimalist library for developers who want to avoid writing extra boilerplate. It provides simple functionality for serializing and deserializing dataclass to dict and json. This library does not compete with pydantic or marshmallow, it just offers a lightweight and convenient way to work with dataclass.

Main Features

RichDataclassMixin

To add serialization and deserialization functionality to your dataclass, simply inherit from RichDataclassMixin.

Mixin methods:

  • from_dict - creates a dataclass from a dictionary.
  • from_json - creates a dataclass from JSON. You can pass a string, file path, or file-like object.
  • as_dict - converts a dataclass to a dictionary.
  • as_json - converts a dataclass to JSON.

The from_* methods also work with nested dataclasses thanks to dacite:

from dataclasses import dataclass
from rich_dataclass import RichDataclassMixin

@dataclass
class NestedOne:
    bar1: str

@dataclass
class NestedTwo:
    bar2: str

@dataclass
class Example(RichDataclassMixin):
    bar3: NestedOne | NestedTwo

example_dataclass = Example.from_dict({"bar3": {"bar2": "example"}})  # Example(bar3=NestedTwo(bar2='example'))
example_dataclass.as_dict()                                           # {'bar3': {'bar2': 'example'}}

Tools

  • rich_dataclass_from_dict_list — converts a list of dictionaries into a list of dataclass instances inheriting RichDataclassMixin. You can pass multiple dataclass types if the dictionaries have different structures.
  • rich_dataclass_to_dict_list - converts a list of dataclass instances into a list of dictionaries.

About

Lightweight library for simple dataclass serialization and deserialization to dict and JSON.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages