Simple creation of data classes from dictionaries.
Project description
This module simplifies creation of data classes (PEP 557) from dictionaries.
Installation
To install dacite, simply use pip (or pipenv):
$ pip install dacite
Requirements
Minimum Python version supported by dacite is 3.6.
Data classes will be available in Python 3.7 as a part of the standard library, but you can use dataclasses module now - it’s available as an external package from PyPI. It will be installed automatically as a dacite dependence.
Quick start
from dataclasses import dataclass
from dacite import from_dict
@dataclass
class User:
name: str
age: int
is_active: bool
data = {
'name': 'john',
'age': 30,
'is_active': True,
}
user = from_dict(data_class=User, data=data)
assert user == User(name='john', age=30, is_active=True)
Features
Dacite supports following features:
nested structures
(basic) types checking
optional fields (i.e. typing.Optional)
unions
collections
values casting and transformation
remapping of fields names
Motivation
Passing plain dictionaries as a data container between your functions or methods isn’t a good practice. Of course you can always create your custom class instead, but this solution is an overkill if you only want to merge a few fields within a single object.
Fortunately Python has a good solution to this problem - data classes. Thanks to @dataclass decorator you can easily create a new custom type with a list of given fields in a declarative manner. Data classes support type hints by design.
However, even if you are using data classes, you have to create their instances somehow. In many such cases, your input is a dictionary - it can be a payload from a HTTP request or a raw data from a database. If you want to convert those dictionaries into data classes, dacite is your best friend.
This library was originally created to simplify creation of type hinted data transfer objects (DTO) which can cross the boundaries in the application architecture.
Usage
Dacite is based on a single function - dacite.from_dict. This function takes 3 parameters:
data_class - data class type
data - dictionary of input data
config (optional) - configuration of the creation process, instance of dacite.Config class
Configuration is a (data) class with following fields:
remap
flattened
prefixed
cast
transform
The examples below show all features of from_dict function and usage of all Config parameters.
Nested structures
You can pass a data with nested dictionaries and it will create a proper result.
@dataclass
class A:
x: str
y: int
@dataclass
class B:
a: A
data = {
'a': {
'x': 'test',
'y': 1,
}
}
result = from_dict(data_class=B, data=data)
assert result == B(a=A(x='test', y=1))
Optional fields
Whenever your data class has a Optional field and you will not provide input data for this field, it will take the None value.
from typing import Optional
@dataclass
class A:
x: str
y: Optional[int]
data = {
'x': 'test',
}
result = from_dict(data_class=A, data=data)
assert result == A(x='test', y=None)
Unions
If your field can accept multiple types, you should use Union. Dacite will try to match data with provided types one by one. If none will match, it will raise UnionMatchError exception.
from typing import Union
@dataclass
class A:
x: str
@dataclass
class B:
y: int
@dataclass
class C:
u: Union[A, B]
data = {
'u': {
'y': 1,
},
}
result = from_dict(data_class=C, data=data)
assert result == C(u=B(y=1))
Collections
Dacite supports fields defined as collections. It works for both - basic types and data classes.
@dataclass
class A:
x: str
y: int
@dataclass
class B:
a_list: List[A]
data = {
'a_list': [
{
'x': 'test1',
'y': 1,
},
{
'x': 'test2',
'y': 2,
}
],
}
result = from_dict(data_class=B, data=data)
assert result == B(a_list=[A(x='test1', y=1), A(x='test2', y=2)])
Remapping
If your input data key does not match with a data class field name, you can use Config.remap argument to handle this case. You have to pass dictionary with a following mapping: {'data_class_field': 'input_field'}
@dataclass
class A:
x: str
data = {
'y': 'test',
}
result = from_dict(data_class=A, data=data, config=Config(remap={'x': 'y'}))
assert result == A(x='test')
Flattened
You often receive a flat structure which you want to convert to something more sophisticated. In this case you can use Config.flattened argument. You have to pass list of flattened fields.
@dataclass
class A:
x: str
y: int
@dataclass
class B:
a: A
z: float
data = {
'x': 'test',
'y': 1,
'z': 2.0,
}
result = from_dict(data_class=B, data=data, config=Config(flattened=['a']))
assert result == B(a=A(x='test', y=1), z=2.0)
Prefixed
Sometimes your data is prefixed rather than nested. To handle this case, you have to use Config.prefixed argument, just pass a following mapping: {'data_class_field': 'prefix'}
@dataclass
class A:
x: str
y: int
@dataclass
class B:
a: A
z: float
data = {
'a_x': 'test',
'a_y': 1,
'z': 2.0,
}
result = from_dict(data_class=B, data=data, config=Config(prefixed={'a': 'a_'}))
assert result == B(a=A(x='test', y=1), z=2.0)
Casting
Input values are not casted by default. If you want to use field type information to transform input value from one type to another, you have to pass given field name as an element of the Config.cast argument list.
@dataclass
class A:
x: str
data = {
'x': 1,
}
result = from_dict(data_class=A, data=data, config=Config(cast=['x']))
assert result == A(x='1')
Transformation
You can use Config.transform argument if you want to transform the input data into the new value. You have to pass a following mapping: {'data_class_field': callable}, where callable is a Callable[[Any], Any].
@dataclass
class A:
x: str
data = {
'x': 'TEST',
}
result = from_dict(data_class=A, data=data, config=Config(transform={'x': str.lower}))
assert result == A(x='test')
Exceptions
Whenever something goes wrong, from_dict will raise adequate exception. There are a few of them:
WrongTypeError - raised when a type of a input value does not match with a type of a data class field
MissingValueError - raised when you don’t provide a value for a required field
InvalidConfigurationError - raised when you provide a invalid value (a field name or a input data key) for a configuration
UnionMatchError - raised when provided data does not match any type of Union
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
Built Distribution
File details
Details for the file dacite-0.0.14.tar.gz
.
File metadata
- Download URL: dacite-0.0.14.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a065c1096bd48330846796a1b07c80d1c1335f775a68fc7cb9f96359e9b8a833 |
|
MD5 | 092be3a4f1ca540d654743c058cf5c9d |
|
BLAKE2b-256 | 53678ccfabbcd80a69b1822fe49260d95113f1b1ee5351a858a74af8a46eaa84 |
Provenance
File details
Details for the file dacite-0.0.14-py3-none-any.whl
.
File metadata
- Download URL: dacite-0.0.14-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b84248507bedc5d8c72897e9ce14f856f90598b74ef6a5d0468f2d97d298bb79 |
|
MD5 | 65b15ffac3dafddd8a9d6f206be7edb9 |
|
BLAKE2b-256 | 9bf5ace76b16debd413f09864bce655532c9866c178b41456a389397e9df8f42 |