Пример #1
0
def test_unstructure_hooks(cl_and_vals):
    """
    Unstructure hooks work.
    """
    converter = Converter()
    cl, vals = cl_and_vals
    inst = cl(*vals)

    converter.register_unstructure_hook(cl, lambda _: "test")

    assert converter.unstructure(inst) == "test"
Пример #2
0
from datetime import date, datetime
from typing import List, Dict
from typing import Optional, Union

import attr
import dateutil.parser
from cattr.converters import Converter
from dateutil import tz

converter = Converter()
converter.register_unstructure_hook(datetime, lambda dt: dt.isoformat())
converter.register_structure_hook(datetime,
                                  lambda ts, _: dateutil.parser.parse(ts))
converter.register_unstructure_hook(date, lambda dt: dt.isoformat())
converter.register_structure_hook(date,
                                  lambda ts, _: dateutil.parser.parse(ts))
converter.register_structure_hook(
    Union[date, datetime],
    lambda ts, _: dateutil.parser.parse(ts).date()
    if len(ts) == 10 else dateutil.parser.parse(ts),
)

format_version = 2


@attr.s(frozen=True, auto_attribs=True)
class Person:
    name: str
    party: Optional[str]
    begin: Optional[date] = None
    end: Optional[date] = None