2

我在使用attrs/cattrs将自定义数据类转换为 JSON 格式时遇到问题。我的班级有以下形式:

import attr
from datetime import datetime
from typing import Tuple, Set, Dict, FrozenSet

@attr.s(auto_attribs=True)
class B:
    w: Set[datetime] = attr.ib()
    x: Set[str] = attr.ib()
    y: Set['A'] = attr.ib()
    z: Set[Tuple[datetime, str]] = attr.ib(factory=set)


@attr.s(auto_attribs=True, cmp=False)
class A:
    a: str = attr.ib()
    b: FrozenSet[Team] = attr.ib()
    c: FrozenSet[Tuple[datetime, str]] = attr.ib(factory=frozenset)
    d: Dict[Tuple[str, str], float] = attr.ib(factory=dict)

我面临的问题是,当我尝试转换为dictvia cattrs.unstructure ,反之亦然 viacattrs.structure时,cattrs会显示一个错误,告诉我应该使用钩子:

import cattr

# Create instance of b
b_instance = B(...)
...
data = cattr.unstructure(b_instance)
print()
print(data)
print()
restored = cattr.structure(data, B)
assert b_instance == restored
ValueError: Unsupported type: <class 'datetime.datetime'>. Register a structure hook for it.

我注册了一个钩子来datetimestr给定的格式转换为:

import cattr
from datetime import datetime

time_format = '%Y-%m-%d %H:%M:%S'
cattr.register_unstructure_hook(datetime, lambda dt: dt.strftime(time_format))
cattr.register_structure_hook(datetime, lambda s, _: datetime.strptime(s, time_format))

但后来我收到以下错误:

TypeError: strptime() argument 1 must be str, not datetime.datetime

我需要能够使用格式为的日期时间从对象实例转换为 JSON 并返回%Y-%m-%d %H:%M:%S。我怎样才能做到这一点?

4

1 回答 1

0

问题在于 Set 类型。如果它存在于类型中,则cattr.unstructure无法序列化它。用元组替换它,事情就会解决:

from typing import Tuple, Set
from datetime import datetime

import cattr
import attr
import pytest


TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
COMPLEX_TYPE = Tuple[Tuple[str, datetime]]
COMPLEX_TYPE_BAD = Set[Tuple[str, datetime]]

cattr.register_unstructure_hook(
    datetime, lambda dt: datetime.strftime(dt, format=TIME_FORMAT)
)
cattr.register_structure_hook(
    datetime, lambda dt_str, _: datetime.strptime(dt_str, TIME_FORMAT)
)


def test_cattrs_io_for_a():
    @attr.s(auto_attribs=True)
    class A:
        ds: datetime = attr.ib(factory=datetime.utcnow)

    a = A()
    a_ser = cattr.unstructure(a)
    a_des = cattr.structure(a_ser, A)
    assert isinstance(a_des, A)


def test_cattrs_io_for_b():
    @attr.s(auto_attribs=True)
    class B:
        ds: COMPLEX_TYPE = attr.ib(factory=set)

    b = B((("a", datetime.utcnow()), ("b", datetime.utcnow())))
    b_ser = cattr.unstructure(b)
    b_des = cattr.structure(b_ser, B)
    assert isinstance(b_des, B)


def test_cattrs_io_for_b_bad():
    @attr.s(auto_attribs=True)
    class B:
        ds: COMPLEX_TYPE_BAD = attr.ib(factory=set)

    b = B({("a", datetime.utcnow()), ("b", datetime.utcnow())})
    b_ser = cattr.unstructure(b)
    with pytest.raises(TypeError):
        b_des = cattr.structure(b_ser, B)
        assert isinstance(b_des, B)

这看起来像是 cattrs 中的一个问题https://github.com/Tinche/cattrs/issues/48

将在 cattrs 的下一个版本中修复。或者你可以从master分支构建它

于 2019-03-19T06:48:36.567 回答