當前位置: 首頁>>代碼示例>>Python>>正文


Python validators.optional方法代碼示例

本文整理匯總了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 
開發者ID:genomoncology,項目名稱:related,代碼行數:16,代碼來源:_init_fields.py

示例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) 
開發者ID:genomoncology,項目名稱:related,代碼行數:17,代碼來源:_init_fields.py

示例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)) 
開發者ID:pypa,項目名稱:pipenv,代碼行數:5,代碼來源:utils.py

示例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) 
開發者ID:pypa,項目名稱:pipenv,代碼行數:38,代碼來源:utils.py

示例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) 
開發者ID:allegroai,項目名稱:trains,代碼行數:50,代碼來源:task_parameters.py


注:本文中的attr.validators.optional方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。