本文整理汇总了Python中attr.NOTHING属性的典型用法代码示例。如果您正苦于以下问题:Python attr.NOTHING属性的具体用法?Python attr.NOTHING怎么用?Python attr.NOTHING使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类attr
的用法示例。
在下文中一共展示了attr.NOTHING属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ChildField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def ChildField(cls, default=NOTHING, required=True, repr=True, cmp=True,
key=None):
"""
Create new child field on a model.
:param cls: class (or name) of the model to be related.
:param default: any object value of type cls
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
converter = converters.to_child_field(cls)
validator = _init_fields.init_validator(
required, object if isinstance(cls, str) else cls
)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp, metadata=dict(key=key))
示例2: DateField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def DateField(formatter=types.DEFAULT_DATE_FORMAT, default=NOTHING,
required=True, repr=True, cmp=True, key=None):
"""
Create new date field on a model.
:param formatter: date formatter string (default: "%Y-%m-%d")
:param default: any date or string that can be converted to a date value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, date)
converter = converters.to_date_field(formatter)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp,
metadata=dict(formatter=formatter, key=key))
示例3: DateTimeField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def DateTimeField(formatter=types.DEFAULT_DATETIME_FORMAT, default=NOTHING,
required=True, repr=True, cmp=True, key=None):
"""
Create new datetime field on a model.
:param formatter: datetime formatter string (default: "ISO_FORMAT")
:param default: any datetime or string that can be converted to a datetime
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, datetime)
converter = converters.to_datetime_field(formatter)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp,
metadata=dict(formatter=formatter, key=key))
示例4: TimeField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def TimeField(formatter=types.DEFAULT_TIME_FORMAT, default=NOTHING,
required=True, repr=True, cmp=True, key=None):
"""
Create new time field on a model.
:param formatter: time formatter string (default: "%H:%M:%S")
:param default: any time or string that can be converted to a time value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, time)
converter = converters.to_time_field(formatter)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, cmp=cmp,
metadata=dict(formatter=formatter, key=key))
示例5: IntegerField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def IntegerField(default=NOTHING, required=True, repr=True, cmp=True,
key=None):
"""
Create new int field on a model.
:param default: any integer value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, int)
return attrib(default=default, converter=converters.int_if_not_none,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key))
示例6: MappingField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def MappingField(cls, child_key, default=NOTHING, required=True, repr=False,
key=None):
"""
Create new mapping field on a model.
:param cls: class (or name) of the model to be related in Sequence.
:param child_key: key field on the child object to be used as the map key.
:param default: any mapping type
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, OrderedDict())
converter = converters.to_mapping_field(cls, child_key)
validator = _init_fields.init_validator(required, types.TypedMapping)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, metadata=dict(key=key))
示例7: RegexField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def RegexField(regex, default=NOTHING, required=True, repr=True, cmp=True,
key=None):
"""
Create new str field on a model.
:param regex: regex validation string (e.g. "[^@]+@[^@]+" for email)
:param default: any string value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, string_types,
validators.regex(regex))
return attrib(default=default, converter=converters.str_if_not_none,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key))
示例8: SequenceField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def SequenceField(cls, default=NOTHING, required=True, repr=False, key=None):
"""
Create new sequence field on a model.
:param cls: class (or name) of the model to be related in Sequence.
:param default: any TypedSequence or list
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, [])
converter = converters.to_sequence_field(cls)
validator = _init_fields.init_validator(required, types.TypedSequence)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, metadata=dict(key=key))
示例9: SetField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def SetField(cls, default=NOTHING, required=True, repr=False, key=None):
"""
Create new set field on a model.
:param cls: class (or name) of the model to be related in Set.
:param default: any TypedSet or set
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, set())
converter = converters.to_set_field(cls)
validator = _init_fields.init_validator(required, types.TypedSet)
return attrib(default=default, converter=converter, validator=validator,
repr=repr, metadata=dict(key=key))
示例10: URLField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def URLField(default=NOTHING, required=True, repr=True, cmp=True, key=None):
"""
Create new UUID field on a model.
:param default: any value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
cls = ParseResult
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, cls)
return attrib(default=default, converter=converters.str_to_url,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key))
示例11: UUIDField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def UUIDField(default=NOTHING, required=False, repr=True, cmp=True, key=None):
"""
Create new UUID field on a model.
:param default: any value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
cls = UUID
default = _init_fields.init_default(required, default, uuid4)
validator = _init_fields.init_validator(required, cls)
return attrib(default=default, converter=converters.str_to_uuid,
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key))
示例12: DecimalField
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def DecimalField(default=NOTHING, required=True, repr=True, cmp=True,
key=None):
"""
Create new decimal field on a model.
:param default: any decimal value
:param bool required: whether or not the object is invalid if not provided.
:param bool repr: include this field should appear in object's repr.
:param bool cmp: include this field in generated comparison.
:param string key: override name of the value when converted to dict.
"""
default = _init_fields.init_default(required, default, None)
validator = _init_fields.init_validator(required, Decimal)
return attrib(default=default, converter=lambda x: Decimal(x),
validator=validator, repr=repr, cmp=cmp,
metadata=dict(key=key))
示例13: _preprocess_typehint
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def _preprocess_typehint(self, typehint, kwargs, field_name, target):
# while this seems contradictory to this converter, we need to
# ignore attrs specific actions when a container type, e.g. a List[T],
# contains a non-attrs manufactured type because this is recursively
# called during schema generation.
# however those types will still require an entry in the registry
# is handled outside this converter
if not _is_attrs(target):
return
attr = _get_attr_from_attrs(target.__attrs_attrs__, field_name)
if attr.default != NOTHING:
# default to optional even if the typehint isn't
# but don't override the user saying otherwise
kwargs.setdefault("required", False)
kwargs.setdefault("missing", missing)
示例14: _get
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def _get(self, environ, metadata, prefix, name):
# Delayed loading.
if self._cfg is None and self._env_name is not None:
log.debug("looking for env var '%s'." % (self._env_name,))
self._cfg = _load_ini(
environ.get(self._env_name, self._env_default)
)
ce = metadata[CNF_KEY]
ic = metadata[CNF_INI_SECRET_KEY]
section = ic.section
if ce.name is not None:
var = ce.name
else:
var = "_".join((prefix + (name,)))
try:
log.debug("looking for '%s' in section '%s'." % (var, section))
return _SecretStr(self._cfg.get(section, var))
except NoOptionError:
if isinstance(ce.default, attr.Factory):
return attr.NOTHING
elif ce.default is not RAISE:
return ce.default
raise MissingSecretError(var)
示例15: _create_hyp_class
# 需要导入模块: import attr [as 别名]
# 或者: from attr import NOTHING [as 别名]
def _create_hyp_class(attrs_and_strategy):
"""
A helper function for Hypothesis to generate attrs classes.
The result is a tuple: an attrs class, and a tuple of values to
instantiate it.
"""
def key(t):
return t[0].default is not NOTHING
attrs_and_strat = sorted(attrs_and_strategy, key=key)
attrs = [a[0] for a in attrs_and_strat]
for i, a in enumerate(attrs):
a.counter = i
vals = tuple((a[1]) for a in attrs_and_strat)
return st.tuples(
st.just(
make_class("HypClass", OrderedDict(zip(gen_attr_names(), attrs)))
),
st.tuples(*vals),
)