Python attr.validators.optional() Examples
The following are 7
code examples of attr.validators.optional().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
attr.validators
, or try the search function
.
Example #1
Source File: _init_fields.py From related with MIT License | 5 votes |
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
Example #2
Source File: _init_fields.py From related with MIT License | 5 votes |
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)
Example #3
Source File: utils.py From pipenv with MIT License | 5 votes |
def optional_instance_of(cls): # type: (Any) -> _ValidatorType[Optional[_T]] return validators.optional(validators.instance_of(cls))
Example #4
Source File: utils.py From pipenv with MIT License | 5 votes |
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)
Example #5
Source File: utils.py From requirementslib with MIT License | 5 votes |
def optional_instance_of(cls): # type: (Any) -> _ValidatorType[Optional[_T]] return validators.optional(validators.instance_of(cls))
Example #6
Source File: utils.py From requirementslib with MIT License | 5 votes |
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)
Example #7
Source File: task_parameters.py From trains with Apache License 2.0 | 4 votes |
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)