Skip to main content

一个 Python 包,通过通用接口帮助数据类的序列化和反序列化,同时确保在许多情况下正确输入和处理解析的数据。

项目描述

Mooss - 序列化

PyPI 版本

  

PyPI 状态

派皮版本

  

PyPI 下载月份

  

GitHub 问题

  

PyPI 许可证

⚠️这个包是一个正在进行的工作,它不适合也不可靠的任何应用程序⚠️

一个 Python 包,通过通用接口帮助数据类的序列化和反序列化

同时还确保正确输入和处理解析的数据。

创建这个包是因为我经常发现自己需要反序列化稍微复杂的嵌套数据类

值类型,并且因为我发现的所有其他解决方案要么过于臃肿,要么无法正常使用我所拥有的。

它绝不是其他软件包的替代品,但在处理稍微复杂的数据时就足够了

结构。

设置

要求

  • Python 3.9 或更新版本。    (CPython 和 PyPy 都受支持!)

安装

运行以下命令之一来安装软件包:

python -m pip install --upgrade mooss-serialize

pip install --upgrade mooss-serialize

用法

为了使用这个包,你只需要创建一个扩展提供的ISerializable接口的类

也有dataclass装饰器,添加一些具有所需类型的变量注释,然后使用

提供类方法来轻松序列化和反序列化它。

有关更多信息,请参见下面的示例

创建类

以下类对其变量具有更复杂和流畅的类型,这将有助于说明主要的

这个包相对于oneliners和其他更简单的反序列化器的优势。

from dataclasses import dataclass

from typing import Optional, Union



from mooss.serialize.interface import ISerializable



@dataclass

class Address(ISerializable):

    country: str

    city: str

    zip_code: Optional[int]

    # TODO: Implement non-serializable fields  (has_multiple_mailboxes: bool)

    street: str = "Unknown"



@dataclass

class Person(ISerializable):

    name: str

    address: Union[Address, str, None]

准备原始数据

我们正在准备一个表示非反序列化数据的字典。

# Representing the 'Person' and 'Address' classes.

# The 'zip_code' field can be removed and will be 'None' since it uses the 'Optional' annotation.

data_person_full: dict = {

    "name": "John Smith",

    "address": {

        "country": "Belgium",

        "city": "Brussels",

        "zip_code": 1000,

        "street": "Rue de la Tribune",

    },

}



# Only representing the 'Person' class and replacing the 'Address' class by a string.

data_person_simple: dict = {

    "name": "John Smith",

    "address": "Rue de la Tribune, 1000 Brussels, Belgium"

}

from_json在下一步使用时,此数据也可以表示为 JSON 字符串。

解析数据

person_full = Person.from_dict(data_person_full)



print(person_full)

其他参数

和方法from_dictfrom_json几个参数可以帮助你影响它的反应和处理方式

具体情况视您的要求而定。

<summary>Click here to expand list of all the available parameters</summary>

This information is also available in the methods' docstring.

<table>

    <tr>

        <td><b>Parameter</b></td>

        <td><b>Type</b></td>

        <td><b>Description</b></td>

        <td><b>Default</b></td>

    </tr>

    <tr>

        <td><code>data_dict</code></td>

        <td><code>dict</code></td>

        <td>Data to be deserialized</td>

        <td><i>Required</i></td>

    </tr>

    <tr>

        <td><code>data_json</code></td>

        <td><code>dict</code></td>

        <td>Data to be parsed into a dict and be deserialized</td>

        <td><i>Required</i></td>

    </tr>

    <tr>

        <td><code>allow_unknown</code></td>

        <td><code>bool</code></td>

        <td>Allow unknown fields to be processed instead of raising a <code>ValueError</code> exception,

如果 . 其他参数将决定它们的用途True

        <td><code>False</code></td>

    </tr>

    <tr>

        <td><code>add_unknown_as_is</code></td>

        <td><code>bool</code></td>

        <td>Adds unknown fields/values as-is in the final class if <code>allow_unknown</code> is also <code>True</code>.</td>

        <td><code>False</code></td>

    </tr>

    <tr>

        <td><code>allow_as_is_unknown_overloading</code></td>

        <td><code>bool</code></td>

        <td>Allow unknown fields/values to overload existing class attributes.</td>

        <td><code>False</code></td>

    </tr>

    <tr>

        <td><code>allow_missing_required</code></td>

        <td><code>bool</code></td>

        <td>TODO</td>

        <td><code>False</code></td>

    </tr>

    <tr>

        <td><code>allow_missing_nullable</code></td>

        <td><code>bool</code></td>

        <td>TODO</td>

        <td><code>False</code></td>

    </tr>

    <tr>

        <td><code>add_unserializable_as_dict</code></td>

        <td><code>bool</code></td>

        <td>TODO</td>

        <td><code>False</code></td>

    </tr>

    <tr>

        <td><code>validate_type</code></td>

        <td><code>bool</code></td>

        <td>Enables a strict type check between the class' serializable fields and the given data.</td>

        <td><code>True</code></td>

    </tr>

    <tr>

        <td><code>parsing_depth</code></td>

        <td><code>int</code></td>

        <td>The recursive depth to which the deserialization process will go.<br>(<code>-1</code> means infinite)</td>

        <td><code>-1</code></td>

    </tr>

    <tr>

        <td><code>do_deep_copy</code></td>

        <td><code>bool</code></td>

        <td>Performs a deep copy of the given 'data_dict' to prevent modifications from affecting other variables

那可能会参考它。

        <td><code>False</code></td>

    </tr>

</table>

类型注释

由于dataclass任何扩展的类都需要装饰器ISerializable,因此这些方法可以很容易地检测到。

并验证给定数据的不同类型,这反过来可以帮助您减少检查量

必须对最终的反序列化数据执行。

之所以使用这种方法,是因为互联网上提供的许多单线和小帮手并不

实施这种类型的检查,通常会给您留下可能无效类型的数据,或者只是不是的数据

在嵌套的可反序列化类的情况下正确反序列化。

需要注意的是,如果给出正确的参数,也可以支持和复制未定义的字段

到方法,但默认情况下不会这样做以防止静默错误和重载现有属性!

支持的类型

这些类型应涵盖此包的 99% 的用例,但是,如果您希望使用

不受支持的类型,您始终可以通过使用该Any类型来跳过对给定字段的类型检查。

  • 原语:

str, int, float,bool

  • 简单套装:

list, dict, tuple,set

  • 组合集*:

list[...], dict[...], tuple[...],set[...]

  • 变量类型*:

Union, Optional,Any

*:对方括号之间可以包含的内容有一些限制。

限制

由于我没有时间实施正确的方法来实现这些限制

支持奇怪和不寻常的数据类型。

[...] 如果您想处理这些问题,您可以自己添加对它的支持,也可以使用专门的笨重的

包裹。

  • 混合复杂类型:list[ISerializable,primitive]

    • 基元和集合的混合应该可以工作,但最好不要使用。

更具体和罕见的类型

在某些时候应该支持下面列出的类型和实用程序,但由于它并不紧急,

它们的实施没有固定的时间表。

可能支持的类型和实用程序列表:

Set, Collection, NamedTuple, NewType, Mapping, Sequence,TypeVarIterable.

贡献

如果你想了解更多关于如何为这个包做贡献的信息,你应该参考develop.md

执照

无牌

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

mooss-serialize-0.0.5.tar.gz (18.3 kB 查看哈希)

已上传 source

内置分布

mooss_serialize-0.0.5-py3-none-any.whl (11.3 kB 查看哈希)

已上传 py3