本文整理汇总了Python中attr.validators.optional方法的典型用法代码示例。如果您正苦于以下问题:Python validators.optional方法的具体用法?Python validators.optional怎么用?Python validators.optional使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attr.validators
的用法示例。
在下文中一共展示了validators.optional方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: init_default
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import optional [as 别名]
def init_default(required, default, optional_default):
"""
Returns optional default if field is not required and
default was not provided.
:param bool required: whether the field is required in a given model.
:param default: default provided by creator of field.
:param optional_default: default for the data type if none provided.
:return: default or optional default based on inputs
"""
if not required and default == NOTHING:
default = optional_default
return default
示例2: init_validator
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import optional [as 别名]
def init_validator(required, cls, *additional_validators):
"""
Create an attrs validator based on the cls provided and required setting.
:param bool required: whether the field is required in a given model.
:param cls: the expected class type of object value.
:return: attrs validator chained correctly (e.g. optional(instance_of))
"""
validator = validators.instance_of(cls)
if additional_validators:
additional_validators = list(additional_validators)
additional_validators.append(validator)
validator = composite(*additional_validators)
return validator if required else validators.optional(validator)
示例3: optional_instance_of
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import optional [as 别名]
def optional_instance_of(cls):
# type: (Any) -> _ValidatorType[Optional[_T]]
return validators.optional(validators.instance_of(cls))
示例4: make_install_requirement
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import optional [as 别名]
def make_install_requirement(
name, version=None, extras=None, markers=None, constraint=False
):
"""
Generates an :class:`~pip._internal.req.req_install.InstallRequirement`.
Create an InstallRequirement from the supplied metadata.
:param name: The requirement's name.
:type name: str
:param version: The requirement version (must be pinned).
:type version: str.
:param extras: The desired extras.
:type extras: list[str]
:param markers: The desired markers, without a preceding semicolon.
:type markers: str
:param constraint: Whether to flag the requirement as a constraint, defaults to False.
:param constraint: bool, optional
:return: A generated InstallRequirement
:rtype: :class:`~pip._internal.req.req_install.InstallRequirement`
"""
# If no extras are specified, the extras string is blank
from pip_shims.shims import install_req_from_line
extras_string = ""
requirement_string = "{0}".format(name)
if extras:
# Sort extras for stability
extras_string = "[{}]".format(",".join(sorted(extras)))
requirement_string = "{0}{1}".format(requirement_string, extras_string)
if version:
requirement_string = "{0}=={1}".format(requirement_string, str(version))
if markers:
requirement_string = "{0}; {1}".format(requirement_string, str(markers))
return install_req_from_line(requirement_string, constraint=constraint)
示例5: param
# 需要导入模块: from attr import validators [as 别名]
# 或者: from attr.validators import optional [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)