当前位置: 首页>>代码示例>>Python>>正文


Python cattr.Converter方法代码示例

本文整理汇总了Python中cattr.Converter方法的典型用法代码示例。如果您正苦于以下问题:Python cattr.Converter方法的具体用法?Python cattr.Converter怎么用?Python cattr.Converter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cattr的用法示例。


在下文中一共展示了cattr.Converter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: deserialize

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def deserialize(
    *, data: str, structure: TStructure, converter: cattr.Converter
) -> TDeserializeReturn:
    """Translate API data into models.
    """
    try:
        data = json.loads(data)
    except json.JSONDecodeError as ex:
        raise DeserializeError(f"Bad json {ex}")
    try:
        response: TDeserializeReturn = converter.structure(  # type: ignore
            data, structure
        )
    except (TypeError, AttributeError) as ex:
        raise DeserializeError(f"Bad data {ex}")
    return response 
开发者ID:looker-open-source,项目名称:sdk-codegen,代码行数:18,代码来源:serialize.py

示例2: test_structuring_hetero_tuples

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_hetero_tuples(converter, list_of_vals_and_types):
    # type: (Converter, List[Any]) -> None
    """Test structuring heterogenous tuples."""
    types = tuple(e[1] for e in list_of_vals_and_types)
    vals = [e[0] for e in list_of_vals_and_types]
    t = Tuple[types]

    converted = converter.structure(vals, t)

    assert isinstance(converted, tuple)

    for x, y in zip(vals, converted):
        assert x == y

    for x, y in zip(types, converted):
        assert isinstance(y, x) 
开发者ID:Tinche,项目名称:cattrs,代码行数:18,代码来源:test_structure.py

示例3: test_structuring_primitive_union_hook

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_primitive_union_hook(converter, ints):
    # type: (Converter, List[int]) -> None
    """Registering a union loading hook works."""

    def structure_hook(val, cl):
        """Even ints are passed through, odd are stringified."""
        return val if val % 2 == 0 else unicode(val)

    converter.register_structure_hook(Union[unicode, int], structure_hook)

    converted = converter.structure(ints, List[Union[unicode, int]])

    for x, y in zip(ints, converted):
        if x % 2 == 0:
            assert x == y
        else:
            assert unicode(x) == y 
开发者ID:Tinche,项目名称:cattrs,代码行数:19,代码来源:test_structure.py

示例4: create_cattrs_converter

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def create_cattrs_converter():
    converter = Converter()
    converter.register_structure_hook(bool, _structure_bool)
    converter.register_structure_hook(string_type, _structure_string)
    converter.register_structure_hook(Model, _structure_schematics)
    converter.register_structure_hook(BaseType, _structure_basetype)
    converter.register_structure_hook(datetime, _structure_datetime)
    converter.register_unstructure_hook(Model, _unstructure_schematics)
    converter.register_unstructure_hook(datetime, _unstructure_datetime)
    converter.register_unstructure_hook(BaseType, _unstructure_basetype)
    return converter 
开发者ID:toumorokoshi,项目名称:transmute-core,代码行数:13,代码来源:converter.py

示例5: test_structuring_primitives

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_primitives(converter, primitive_and_type):
    # type: (Converter, Any) -> None
    """Test just structuring a primitive value."""
    val, t = primitive_and_type
    assert converter.structure(val, t) == val
    assert converter.structure(val, Any) == val 
开发者ID:Tinche,项目名称:cattrs,代码行数:8,代码来源:test_structure.py

示例6: test_structuring_seqs

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_seqs(converter, seq_and_type):
    # type: (Converter, Any) -> None
    """Test structuring sequence generic types."""
    iterable, t = seq_and_type
    converted = converter.structure(iterable, t)
    for x, y in zip(iterable, converted):
        assert x == y 
开发者ID:Tinche,项目名称:cattrs,代码行数:9,代码来源:test_structure.py

示例7: test_stringifying_sets

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_stringifying_sets(converter, set_and_type):
    # type: (Converter, Any) -> None
    """Test structuring generic sets and converting the contents to str."""
    set_, input_set_type = set_and_type

    input_set_type.__args__ = (unicode,)
    converted = converter.structure(set_, input_set_type)
    assert len(converted) == len(set_)
    for e in set_:
        assert str(e) in converted 
开发者ID:Tinche,项目名称:cattrs,代码行数:12,代码来源:test_structure.py

示例8: test_structuring_dicts

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_dicts(converter, dict_and_type):
    # type: (Converter, Any) -> None
    d, t = dict_and_type

    converted = converter.structure(d, t)

    assert converted == d
    assert converted is not d 
开发者ID:Tinche,项目名称:cattrs,代码行数:10,代码来源:test_structure.py

示例9: test_structuring_dicts_opts

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_dicts_opts(converter, dict_and_type, data):
    # type: (Converter, Any, Any) -> None
    """Structure dicts, but with optional primitives."""
    d, t = dict_and_type
    assume(not is_bare(t))
    t.__args__ = (t.__args__[0], Optional[t.__args__[1]])
    d = {k: v if data.draw(booleans()) else None for k, v in d.items()}

    converted = converter.structure(d, t)

    assert converted == d
    assert converted is not d 
开发者ID:Tinche,项目名称:cattrs,代码行数:14,代码来源:test_structure.py

示例10: test_stringifying_dicts

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_stringifying_dicts(converter, dict_and_type):
    # type: (Converter, Any) -> None
    d, t = dict_and_type

    converted = converter.structure(d, Dict[unicode, unicode])

    for k, v in d.items():
        assert converted[str(k)] == str(v) 
开发者ID:Tinche,项目名称:cattrs,代码行数:10,代码来源:test_structure.py

示例11: test_structuring_optional_primitives

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_optional_primitives(converter, primitive_and_type):
    # type: (Converter, Any) -> None
    """Test structuring Optional primitive types."""
    val, type = primitive_and_type

    assert converter.structure(val, Optional[type]) == val
    assert converter.structure(None, Optional[type]) is None 
开发者ID:Tinche,项目名称:cattrs,代码行数:9,代码来源:test_structure.py

示例12: test_structuring_lists_of_opt

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_lists_of_opt(converter, list_and_type):
    # type: (Converter, List[Any]) -> None
    """Test structuring lists of Optional primitive types."""
    l, t = list_and_type

    l.append(None)
    args = t.__args__

    is_optional = args[0] is Optional or (
        is_union_type(args[0])
        and len(args[0].__args__) == 2
        and args[0].__args__[1] is NoneType
    )

    if not is_bare(t) and (
        args[0] not in (Any, unicode, str) and not is_optional
    ):
        with raises((TypeError, ValueError)):
            converter.structure(l, t)

    optional_t = Optional[args[0]]
    # We want to create a generic type annotation with an optional
    # type parameter.
    t = change_type_param(t, optional_t)

    converted = converter.structure(l, t)

    for x, y in zip(l, converted):
        assert x == y

    t.__args__ = args 
开发者ID:Tinche,项目名称:cattrs,代码行数:33,代码来源:test_structure.py

示例13: test_structuring_enums

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_enums(converter, data, enum):
    # type: (Converter, Any, Any) -> None
    """Test structuring enums by their values."""
    val = data.draw(sampled_from(list(enum)))

    assert converter.structure(val.value, enum) == val 
开发者ID:Tinche,项目名称:cattrs,代码行数:8,代码来源:test_structure.py

示例14: test_structuring_unsupported

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def test_structuring_unsupported(converter):
    # type: (Converter) -> None
    """Loading unsupported classes should throw."""
    with raises(ValueError):
        converter.structure(1, Converter)
    with raises(ValueError):
        converter.structure(1, Union[int, unicode]) 
开发者ID:Tinche,项目名称:cattrs,代码行数:9,代码来源:test_structure.py

示例15: converter

# 需要导入模块: import cattr [as 别名]
# 或者: from cattr import Converter [as 别名]
def converter():
    return Converter() 
开发者ID:Tinche,项目名称:cattrs,代码行数:4,代码来源:conftest.py


注:本文中的cattr.Converter方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。