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
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
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 Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dataclass_factory-2.16-py3-none-any.whl.
File metadata
- Download URL: dataclass_factory-2.16-py3-none-any.whl
- Upload date:
- Size: 29.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.24.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d01e73d40b8f74051df822f21d8dc8bbf2754b11670cd1c477357e8b21323ed
|
|
| MD5 |
439c9e4e4459b81fe83ed03b08b736df
|
|
| BLAKE2b-256 |
f16650b9f5d8a0e9fbe5469c5b8a7f198511fff9959347fc2443531d651b21d7
|