使用生成的字段值反序列化类

2024-05-15 18:01:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一门课,像:

class Pathology:
    """
    Represents a pathology, which is initialized with a name and description.
    """

    def __init__(self: str, name: str, description: str):
        self.id = str(uuid.uuid4())
        self.name = name
        self.description = description
        self.phases = []

    def to_json(self):
        return jsonpickle.encode(self, make_refs=False, unpicklable=False)

在这个类中,我不希望用户为id传递值,我总是希望在构造时生成它

从JSON反序列化时,我希望执行以下操作:

    with open('data/test_case_1.json', 'r') as test_case_1_file:
        test_case_1 = test_case_1_file.read()

    # parse file
    obj = jsonpickle.decode(test_case_1)
    assert pathology == Pathology(**obj)

但是,我遇到了一个错误TypeError: __init__() got an unexpected keyword argument 'id'

我怀疑这是因为init构造函数没有可用的id字段

什么是Python式的方式来支持这种行为


Tags: nametestselfidjsoninitdefwith
1条回答
网友
1楼 · 发布于 2024-05-15 18:01:49

In this class, I do not ever want a user to pass in a value for id, I always wish to generated it upon construction.

基于上述期望的结果,我的建议是将id定义为(只读)property。将其定义为属性的好处是,它不会被视为实例属性,而且巧合的是,它不会通过构造函数接受值;主要的缺点是它不会显示在类的__repr__值(假设我们使用从dataclasses获得的生成值)或dataclasses.asdicthelper函数中

我还在实现中添加了一些额外的更改(希望更好):

  • 将类重新声明为dataclass,我个人更喜欢这样,因为它减少了一些样板代码,例如__init__构造函数,或者需要定义一个__eq__方法(后者通过==检查两个类对象是否相等)。dataclasses模块还提供了一个有用的asdict函数,我们可以在序列化过程中使用它

  • 通过json模块使用内置JSON(反)序列化。做出这个决定的部分原因是我个人从未使用过jsonpickle模块,我对酸洗的工作原理只有基本的了解。我觉得将类对象转换为JSON或从JSON转换为类对象更自然,而且在任何情况下都可能表现得更好

  • 添加一个from_json_filehelper方法,我们可以使用它从本地文件路径加载一个新的类对象

import json
import uuid
from dataclasses import dataclass, asdict, field, fields
from functools import cached_property
from typing import List


@dataclass
class Pathology:
    """
    Represents a pathology, which is initialized with a name and description.
    """
    name: str
    description: str
    phases: List[str] = field(init=False, default_factory=list)

    @cached_property
    def id(self) -> str:
        return str(uuid.uuid4())

    def to_json(self):
        return json.dumps(asdict(self))

    @classmethod
    def from_json_file(cls, file_name: str):

        # A list of only the fields that can be passed in to the constructor.
        # Note: maybe it's worth caching this for repeated runs.
        init_fields = tuple(f.name for f in fields(cls) if f.init)

        if not file_name.endswith('.json'):
            file_name += '.json'

        with open(file_name, 'r') as in_file:
            test_case_1 = json.load(in_file)

        # parse file
        return cls(**{k: v for k, v in test_case_1.items() if k in init_fields})

下面是我整理的一些快速代码,以确认一切都如预期的那样:

def main():
    p1 = Pathology('my-name', 'my test description.')
    print('P1:', p1)
    p_id = p1.id
    print('P1 -> id:', p_id)
    assert p1.id == p_id, 'expected id value to be cached'

    print('Serialized JSON:', p1.to_json())

    # Save JSON to file
    with open('my_file.json', 'w') as out_file:
        out_file.write(p1.to_json())

    # De-serialize object from file
    p2 = Pathology.from_json_file('my_file')

    print('P2:', p2)

    # assert both objects are same
    assert p2 == p1

    # IDs should be unique, since it's automatically generated each time (we
    # don't pass in an ID to the constructor or store it in JSON file)
    assert p1.id != p2.id, 'expected IDs to be unique'


if __name__ == '__main__':
    main()

相关问题 更多 >