跳转到主要内容

在保持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)])

文档

https://desert.readthedocs.io/

限制

函数中的字符串注释和向前引用不支持。

致谢

2022.09.22 (2022-09-22)

向后不兼容的更改

  • 更新所有项目依赖项并修复所有已弃用警告。已删除Python 3.6支持以允许更新已弃用的依赖项。 #161

更改

  • 现在可以在您的dataclass中使用类型变体泛型,例如使用SequenceMutableSequence代替List,使用Mapping代替Dict等。

    这允许您从dataclass的用户中隐藏实现细节。如果您的dataclass中的一个字段与元组一样工作得很好,您就不再需要强制用户传递一个list以满足类型检查器的要求。

    例如,通过使用MappingMutableMapping,用户可以将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

    #140


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 (66.7 kB 查看哈希值)

上传时间 源码

构建的发行版

desert-2022.9.22-py3-none-any.whl (10.9 kB 查看哈希值)

上传时间 Python 3

由以下组织支持

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误日志StatusPageStatusPage状态页面