An utility class for creating instances of dataclasses
Project description
dataclass_factory
dataclass_factory is a modern way to convert dataclasses or other objects to and from more common types like dicts
Help
See documentation for more details.
TL;DR
Install
pip install dataclass_factory
Use
from dataclasses import dataclass
import dataclass_factory
@dataclass
class Book:
title: str
price: int
author: str = "Unknown author"
data = {
"title": "Fahrenheit 451",
"price": 100,
}
factory = dataclass_factory.Factory()
book: Book = factory.load(data, Book) # Same as Book(title="Fahrenheit 451", price=100)
serialized = factory.dump(book)
Requirements
- python >= 3.6
You can use dataclass_factory
with python 3.6 and dataclass
library installed from pip.
On python 3.7 it has no external dependencies outside of the Python standard library.
Advantages
- No schemas or configuration needed for simple cases. Just create
Factory
and callload
/dump
methods - Speed. It is up to 10 times faster than
marshmallow
anddataclasses.asdict
(see benchmarks) - Automatic name style conversion (e.g.
snake_case
toCamelCase
) - Automatic skipping of "internal use" fields (with leading underscore)
- Enums, typed dicts, tuples and lists are supported out of the box
- Unions and Optionals are supported without need to define them in schema
- Generic dataclasses can be automatically parsed as well
- Cyclic-referenced structures (such as linked-lists or trees) also can be converted
- Validators, custom parser steps are supported.
- Multiple schemas for single type can be provided to support different ways of parsing of the same type
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
adaptix-3.0.0a2.tar.gz
(67.1 kB
view hashes)
Built Distribution
adaptix-3.0.0a2-py3-none-any.whl
(89.7 kB
view hashes)
Close
Hashes for adaptix-3.0.0a2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4d92af0f4e59fd0afe80897e59a1352892ccc53e43a0a5102698a2c96b58ffa2 |
|
MD5 | 9b830e5cb0367b43ed550f98cfd266fb |
|
BLAKE2b-256 | cf89890a35e84d449c1ceecefc4e36dab022cd626479c6e62597ddcb6d06785f |