An utility class for creating instances of dataclasses
Project Links
Meta
Author: A. Tikhonov
Requires Python: >=3.6
Classifiers
Operating System
- OS Independent
Intended Audience
- Developers
License
- OSI Approved :: Apache Software License
Programming Language
- Python :: 3
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
Factoryand callload/dumpmethods - Speed. It is up to 10 times faster than
marshmallowanddataclasses.asdict(see benchmarks) - Automatic name style conversion (e.g.
snake_casetoCamelCase) - 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
2.16
Jul 20, 2022
2.15
Jun 21, 2022
2.14
Feb 02, 2022
2.13
Nov 09, 2021
2.12
Nov 01, 2021
2.11
May 31, 2021
2.10.2
Apr 01, 2021
2.10.1
Feb 18, 2021
2.10
Sep 25, 2020
2.9
Sep 24, 2020
2.8.3
Mar 26, 2020
2.8.2
Mar 21, 2020
2.8.1
Mar 09, 2020
2.7
Jan 07, 2020
2.6
Jan 02, 2020
2.5
Dec 21, 2019
2.4.2
Dec 21, 2019
2.4.1
Sep 12, 2019
2.4
Aug 12, 2019
2.3
Jul 12, 2019
2.2
Jul 04, 2019
2.1
Jun 15, 2019
2.0
Jun 12, 2019
1.1.1
May 26, 2019
1.1.0
May 19, 2019
1.0.5
May 19, 2019
1.0.4
Mar 31, 2019
1.0.3
Jan 07, 2019
1.0.2
Nov 30, 2018
1.0.1
Nov 30, 2018
1.0
Nov 30, 2018
0.8
Oct 07, 2018
0.7
Sep 30, 2018
0.6
Sep 22, 2018
0.5
Sep 15, 2018
0.4
Sep 08, 2018
0.3
Sep 08, 2018
0.2
Sep 08, 2018
0.1
Sep 07, 2018
Wheel compatibility matrix
Files in release
Extras:
None
Dependencies:
dataclasses