def test_structure_tuple(cl_and_vals): """Test loading from a tuple, by registering the loader.""" converter = Converter() cl, vals = cl_and_vals converter.register_structure_hook(cl, converter.structure_attrs_fromtuple) obj = cl(*vals) dumped = astuple(obj) loaded = converter.structure(dumped, cl) assert obj == loaded
def test_structure_union_explicit(cl_and_vals_a, cl_and_vals_b): """Structuring of manually-disambiguable unions works.""" converter = Converter() cl_a, vals_a = cl_and_vals_a cl_b, vals_b = cl_and_vals_b def dis(obj, _): return converter.structure(obj, cl_a) converter.register_structure_hook(Union[cl_a, cl_b], dis) inst = cl_a(*vals_a) assert inst == converter.structure(converter.unstructure(inst), Union[cl_a, cl_b])
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
from datetime import date, datetime from typing import List, Dict from typing import Optional, Union import attr 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, _: datetime.fromisoformat(ts)) converter.register_unstructure_hook(date, lambda dt: dt.isoformat()) converter.register_structure_hook(date, lambda ts, _: date.fromisoformat(ts)) converter.register_structure_hook( Optional[Union[date, datetime]], lambda ts, _: date.fromisoformat(ts) if len(ts) == 10 else datetime.fromisoformat(ts), ) format_version = 4 @attr.s(frozen=True, auto_attribs=True) class Person: # Overview page name: str party: Optional[str] begin: Optional[date] = None end: Optional[date] = None original_id: Optional[int] = None
from cattr.converters import Converter from simple_smartsheet import config from simple_smartsheet import exceptions from simple_smartsheet import utils from simple_smartsheet.types import IndexesType, IndexesKeysType if TYPE_CHECKING: from simple_smartsheet.smartsheet import Smartsheet # noqa: F401 from simple_smartsheet.models.extra import Result logger = logging.getLogger(__name__) converter = Converter() converter.register_structure_hook(datetime, lambda ts, _: ts) converter.register_structure_hook(IndexesKeysType, lambda x, _: x) converter.register_structure_hook(IndexesType, lambda x, _: x) converter.register_structure_hook(Union[float, str, datetime, None], lambda ts, _: ts) class Schema(marshmallow.Schema): class Meta: unknown = utils.get_unknown_field_handling(config.DEBUG) @marshmallow.post_dump def remove_none(self, data): return {key: value for key, value in data.items() if value is not None} class CoreSchema(Schema):