def test_ok(self): Tag = t.NamedTuple('Tag', [('name', str)]) registry = load.GenericRegistry({ t.List: load.list_loader, }) | load.PrimitiveRegistry({ Tag: compose(Tag, '<{}>'.format), }) # simple case loader = registry(t.List[Tag]) assert loader(['hello', 5, 'there' ]) == [Tag('<hello>'), Tag('<5>'), Tag('<there>')] # recursive case loader = registry(t.List[t.List[Tag]]) assert loader([['hello', 9, 'there'], [], ['another', 'list'] ]) == [[Tag('<hello>'), Tag('<9>'), Tag('<there>')], [], [Tag('<another>'), Tag('<list>')]]
def test_ok(self): Tag = t.NamedTuple('Tag', [('name', str)]) registry = load.PrimitiveRegistry({Tag: compose(Tag, '<{}>'.format) }) | load.get_optional_loader loader = registry(t.Optional[Tag]) assert loader(None) is None assert loader(5) == Tag('<5>')
def registry(): return load.PrimitiveRegistry({ int: int, float: float, bool: { 'true': True, 'false': False }.__getitem__, str: str, type(None): identity, object: identity, datetime: load.parse_iso8601, }) | load.GenericRegistry({ t.List: load.list_loader, t.Set: load.set_loader, }) | load.get_optional_loader
def test_unsupported_type(self): Tag = t.NamedTuple('Tag', [('name', str)]) registry = load.GenericRegistry({ t.List: load.list_loader, }) | load.PrimitiveRegistry({ Tag: compose(Tag, '<{}>'.format), }) with pytest.raises(load.UnsupportedType): registry(t.Set[Tag]) with pytest.raises(load.UnsupportedType): registry(t.List[str]) with pytest.raises(load.UnsupportedType): registry(object)
"""deserialization tools""" import typing as t from datetime import datetime from functools import partial from toolz import flip from valuable import load from . import types registry = load.PrimitiveRegistry({ datetime: partial(flip(datetime.strptime), '%Y-%m-%dT%H:%M:%SZ'), **{c: c for c in [int, float, bool, str, types.Issue.State]} }) | load.GenericRegistry({ t.List: load.list_loader }) | load.get_optional_loader | load.AutoDataclassRegistry()
from valuable import load from . import types from toolz import compose def page_loader(subloaders, value): loader, = subloaders objects_key, = (k for k in value if k != 'response_metadata' and k != 'ok') try: next_cursor = value['response_metadata']['next_cursor'] except KeyError: next_cursor = None return types.Page(list(map(loader, value[objects_key])), next_cursor=next_cursor) registry = load.MultiRegistry( load.PrimitiveRegistry({ datetime: compose(datetime.utcfromtimestamp, float), int: int, float: float, bool: bool, str: str, }), load.GenericRegistry({ t.List: load.list_loader, types.Page: page_loader, }), load.AutoDataclassRegistry(), )
def test_not_found(self): registry = load.PrimitiveRegistry({int: round}) with pytest.raises(load.UnsupportedType): registry(str)
def test_found(self): registry = load.PrimitiveRegistry({int: round}) loader = registry(int) assert loader(3.4) == 3
import typing as t from datetime import datetime from functools import partial from toolz import compose, flip, valmap from valuable import load, xml from . import types registry = load.PrimitiveRegistry({ bool: dict(true=True, false=False).__getitem__, datetime: partial(flip(datetime.strptime), '%Y-%m-%dT%H:%M:%S%z'), str: str.strip, **{ c: c for c in [ int, float, types.Journey.Status, types.Journey.Component.Status ] } }) | load.GenericRegistry({ t.List: load.list_loader, }) | load.get_optional_loader | load.DataclassRegistry({ types.Station: { **valmap( xml.textgetter, { 'code': 'Code', 'type': 'Type', 'country': 'Land', 'uic': 'UICCode',