本文整理汇总了Python中attr.validators方法的典型用法代码示例。如果您正苦于以下问题:Python attr.validators方法的具体用法?Python attr.validators怎么用?Python attr.validators使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attr
的用法示例。
在下文中一共展示了attr.validators方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_noncallable_validators
# 需要导入模块: import attr [as 别名]
# 或者: from attr import validators [as 别名]
def test_noncallable_validators(
self, member_validator, iterable_validator
):
"""
Raise `TypeError` if any validators are not callable.
"""
with pytest.raises(TypeError) as e:
deep_iterable(member_validator, iterable_validator)
value = 42
message = "must be callable (got {value} that is a {type_}).".format(
value=value, type_=value.__class__
)
assert message in e.value.args[0]
assert value == e.value.args[1]
assert message in e.value.msg
assert value == e.value.value
示例2: test_repr_member_and_iterable
# 需要导入模块: import attr [as 别名]
# 或者: from attr import validators [as 别名]
def test_repr_member_and_iterable(self):
"""
Returned validator has a useful `__repr__` when both member
and iterable validators are set.
"""
member_validator = instance_of(int)
member_repr = "<instance_of validator for type <{type} 'int'>>".format(
type=TYPE
)
iterable_validator = instance_of(list)
iterable_repr = (
"<instance_of validator for type <{type} 'list'>>"
).format(type=TYPE)
v = deep_iterable(member_validator, iterable_validator)
expected_repr = (
"<deep_iterable validator for"
" {iterable_repr} iterables of {member_repr}>"
).format(iterable_repr=iterable_repr, member_repr=member_repr)
assert expected_repr == repr(v)
示例3: optional_instance_of
# 需要导入模块: import attr [as 别名]
# 或者: from attr import validators [as 别名]
def optional_instance_of(cls):
# type: (Any) -> _OptionalValidator
"""
Return an validator to determine whether an input is an optional instance of a class.
:return: A validator to determine optional instance membership.
:rtype: :class:`~attr.validators._OptionalValidator`
"""
return attr.validators.optional(attr.validators.instance_of(cls))
示例4: test_success
# 需要导入模块: import attr [as 别名]
# 或者: from attr import validators [as 别名]
def test_success(self):
"""
Succeeds if all wrapped validators succeed.
"""
v = and_(instance_of(int), always_pass)
v(None, simple_attr("test"), 42)
示例5: test_success_member_and_iterable
# 需要导入模块: import attr [as 别名]
# 或者: from attr import validators [as 别名]
def test_success_member_and_iterable(self):
"""
If both the member and iterable validators succeed, nothing happens.
"""
member_validator = instance_of(int)
iterable_validator = instance_of(list)
v = deep_iterable(member_validator, iterable_validator)
a = simple_attr("test")
v(None, a, [42])
示例6: param
# 需要导入模块: import attr [as 别名]
# 或者: from attr import validators [as 别名]
def param(
validator=None,
range=None,
type=None,
desc=None,
metadata=None,
*args,
**kwargs
):
"""
A parameter inside a TaskParameters class.
See TaskParameters for more information.
:param validator: A validator or validators list.
Any validator from attr.validators is applicable.
:param range: The legal values range of the parameter.
A tuple (min_limit, max_limit). None for no limitation.
:param type: The type of the parameter.
Supported types are int, str and float. None to place no limit of the type
:param desc: A string description of the parameter, for future use.
:param metadata: A dictionary metadata of the parameter, for future use.
:param args: Additional arguments to pass to attr.attrib constructor.
:param kwargs: Additional keyword arguments to pass to attr.attrib constructor.
:return: An attr.attrib instance to use with TaskParameters class.
Warning: Do not create an immutable param using args or kwargs. It will cause
connect method of the TaskParameters class to fail.
"""
metadata = metadata or {}
metadata["desc"] = desc
validator = _canonize_validator(validator)
if type:
validator.append(validators.optional(validators.instance_of(type)))
if range:
validator.append(range_validator(*range))
return attr.ib(validator=validator, type=type, metadata=metadata, *args, **kwargs)