在保持DRY的同时反序列化为对象
项目描述
Desert为dataclasses和attrs类生成序列化模式。编写DRY(“不要重复自己”)的代码有助于避免错误并提高可读性。Desert可以帮助您编写DRY的代码。
安装
pip install desert
或使用Poetry
poetry add desert
用法
一个简单的示例模型了一个Car中的两个Person对象。
from dataclasses import dataclass
# Or using attrs
# from attr import dataclass
from typing import List
import desert
@dataclass
class Person:
name: str
age: int
@dataclass
class Car:
passengers: List[Person]
# Load some simple data types.
data = {'passengers': [{'name': 'Alice', 'age': 21}, {'name': 'Bob', 'age': 22}]}
# Create a schema for the Car class.
schema = desert.schema(Car)
# Load the data.
car = schema.load(data)
assert car == Car(passengers=[Person(name='Alice', age=21), Person(name='Bob', age=22)])
文档
限制
函数中的字符串注释和向前引用不支持。
致谢
这个包最初是作为marshmallow-dataclass的扩展,为了添加对attrs的支持。
2022.09.22 (2022-09-22)
向后不兼容的更改
更新所有项目依赖项并修复所有已弃用警告。已删除Python 3.6支持以允许更新已弃用的依赖项。 #161
更改
现在可以在您的dataclass中使用类型变体泛型,例如使用Sequence或MutableSequence代替List,使用Mapping代替Dict等。
这允许您从dataclass的用户中隐藏实现细节。如果您的dataclass中的一个字段与元组一样工作得很好,您就不再需要强制用户传递一个list以满足类型检查器的要求。
例如,通过使用Mapping或MutableMapping,用户可以将OrderedDict传递给Dict属性,而不会引起MyPy的抱怨。
@dataclass class OldWay: str_list: List[str] num_map: Dict[str, float] # MyPy will reject this even though Marshmallow works just fine. If you use # type-variant generics, MyPy will accept this code. instance = OldClass([], collections.ChainMap(MY_DEFAULTS)) @dataclass class NewWay: str_list: List[str] # Type-invariants still work num_map: MutableMapping[str, float] # Now generics do too
2020.11.18 (2020-11-18)
更改
模式不再复制非字段dataclass属性。感谢@sveinse的报告和测试。[79](https://github.com/python-desert/desert/issues/79)
2020.01.06 (2020-01-06)
更改
在ib()和fields()中支持了附加元数据。感谢@sveinse的报告和测试。[28](https://github.com/python-desert/desert/issues/28)
2020.01.05 (2020-01-05)
更改
添加了对接受self的attr工厂的支持(attr.Factory(..., takes_self=True))。[27](https://github.com/python-desert/desert/issues/27)
2020.01.04 (2020-01-04)
更改
根据[https://docs.pythonlang.cn/3/library/typing.html#typing.Tuple](https://docs.pythonlang.cn/3/library/typing.html#typing.Tuple)添加了对Tuple[int, …]的支持。[16](https://github.com/python-desert/desert/issues/16)感谢@sveinse的报告和测试。
内部使用模块导入。[18](https://github.com/python-desert/desert/issues/18)
Desert版本只需在_version.py中更新。[19](https://github.com/python-desert/desert/issues/19)
修复了doctests。[20](https://github.com/python-desert/desert/issues/20)
2020.01.03
更改
可选字段允许None。[11](https://github.com/python-desert/desert/issues/11)感谢@sveinse的报告和测试。
2019.12.18
更改
改进了未知泛型的错误信息。[10](https://github.com/python-desert/desert/pull/10)
2019.12.10
更改
添加了UnknownType异常,带有更好的错误信息,用于应该具有泛型的类型。[8](https://github.com/python-desert/desert/issues/8)
2019.12.09
更改
接受Marshmallow schema的Meta参数,允许排除未知字段和其他选项。[3](https://github.com/python-desert/desert/pull/3)
2019.11.06 (2019-11-06)
更改
添加twine和wheel开发依赖项。[2](https://github.com/python-desert/desert/issues/2)
2019.11.06 (2019-11-06)
更改
切换到calver
向后不兼容的更改
没有默认值或工厂的非必选字段现在有required=True,因此当缺失时引发marshmallow.exceptions.ValidationError。[1](https://github.com/python-desert/desert/issues/1)
0.1.0 (2019-06-22)
更改
PyPI上的第一个版本。
——
项目详情
下载文件
下载适合您的平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分布
构建的发行版
desert-2022.9.22.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 0f45e098915e16452a269a1da0bcbbf31df0af2173899f1c097fb2e3b0265d89 |
|
MD5 | 99f73c1291f85693ac66f81e6728cf2a |
|
BLAKE2b-256 | e1b3919b5483c66ddae7f0fbbdc04cac9930e163aef9c86641a77585b5d7fda0 |
desert-2022.9.22-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | cad7b6f1936448d26bde403882ec6855786b4d24d38d0ed4400e505ac8c5591f |
|
MD5 | e31f38d134eec3680018973036893701 |
|
BLAKE2b-256 | d748060b6678ede661cfaf4ffaed176b4bae51c88bb915200d56f5bc9d721d9e |