本文整理匯總了Python中attr.name方法的典型用法代碼示例。如果您正苦於以下問題:Python attr.name方法的具體用法?Python attr.name怎麽用?Python attr.name使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類attr
的用法示例。
在下文中一共展示了attr.name方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: is_color_code
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def is_color_code(instance, attribute, value):
"""
A validator that raises a :exc:`ValueError` if attribute value
is not valid color code.
Value considered as valid color code if it starts with # char
followed by hexadecimal.
"""
err = "{attr} should be a valid color code".format(attr=attribute.name)
if not value.startswith("#"):
raise ValueError(err)
if len(value) != 7:
raise ValueError(err)
try:
int(value[1:], 16)
except ValueError:
raise ValueError(err)
示例2: __call__
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def __call__(self, inst, attr, value):
out_lower = self.bounds[0] is not None and (
value < self.bounds[0] if self.closed[0] else value <= self.bounds[0]
)
out_upper = self.bounds[1] is not None and (
value > self.bounds[1] if self.closed[1] else value >= self.bounds[1]
)
if np.any(out_lower) or np.any(out_upper):
common_msg = f"out of bounds {self.bounds_str}"
if np.isscalar(value):
msg = f"Value {value} of variable '{attr.name}' is " + common_msg
else:
msg = f"Found value(s) in array '{attr.name}' " + common_msg
raise ValueError(msg)
示例3: _asdict
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def _asdict(self):
ret = thaw(attr.asdict(self, filter=(lambda attr, val: (
(val and val != attr.default or (attr.name in self._RENDER_WHITELIST)) and
attr.name not in self._RENDER_BLACKLIST
))))
if self.env_prefixes.mapping:
ret['env_prefixes'] = dict(self.env_prefixes.mapping)
if self.env_suffixes.mapping:
ret['env_suffixes'] = dict(self.env_suffixes.mapping)
return ret
示例4: __call__
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def __call__(self, inst, attr, value):
if value not in self.choices:
raise ValueError("{attr} should be one of {choice}".format(
attr=attr.name, choice=self.choices))
示例5: test_container_is_not_of_expected_type_raises_TypeError
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def test_container_is_not_of_expected_type_raises_TypeError(
items, types, message
):
validator = type_validator()
attr = MagicMock()
attr.name = "Smth"
attr.type = types
with pytest.raises(ValueError) as error:
validator(None, attr, items)
repr_msg = "<{}>".format(message)
assert repr_msg == repr(error.value)
示例6: test_raises_when_container_is_empty_and_empty_ok_is_false
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def test_raises_when_container_is_empty_and_empty_ok_is_false():
# GIVEN
items = []
validator = type_validator(empty_ok=False)
attr = MagicMock()
attr.name = "Smth"
attr.type = str
# WHEN
with pytest.raises(ValueError) as error:
validator(None, attr, items)
# THEN
assert "Smth can not be empty and must be str"
"(got None)" == str(error.value)
示例7: not_bool
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def not_bool(inst, attr, value):
if isinstance(value, bool):
raise TypeError(
"'{name}' must not be a bool (got {value!r})"
.format(name=attr.name, value=value)
)
示例8: is_non_negative
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def is_non_negative(inst, attr, value):
if value < 0:
raise ValueError(
"'{name}' must be non-negative (got {value!r})"
.format(name=attr.name, value=value)
)
示例9: sequence_of
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def sequence_of(validator, allow_empty=False):
if isinstance(validator, list):
validator = and_(*validator)
def validate(inst, attr, value):
is_seq(inst, attr, value)
if not allow_empty and not value:
raise ValueError(
"'{name}' cannot be empty".format(name=attr.name, value=value)
)
for i, item in enumerate(value):
try:
validator(inst, attr, item)
except (ValueError, TypeError):
# now that we know it's invalid, let's re-do the validation
# with a better attribute name. Since we have to copy data,
# we only do it when we know we have to raise an exception.
item_attr = copy.copy(attr)
object.__setattr__(item_attr, 'name', '{}[{}]'.format(attr.name, i))
try:
validator(inst, item_attr, item)
except Exception as exc:
six.raise_from(exc, None)
else:
# if we somehow got here, raise original exception
raise
return validate
示例10: __attrs_post_init__
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import name [as 別名]
def __attrs_post_init__(self):
object.__setattr__(self, 'cmd', tuple(self.cmd))
# if cmd is empty, then remove all values except for the few that actually
# apply with a null command.
if not self.cmd:
_keep_fields = ('name', 'cmd')
for attrib in attr.fields(self.__class__):
if attrib.name in _keep_fields:
continue
# cribbed from attr/_make.py; the goal is to compute the attribute's
# default value.
if isinstance(attrib.default, attr.Factory):
if attrib.default.takes_self:
val = attrib.default.factory(self)
else:
val = attrib.default.factory()
else:
val = attrib.default
if attrib.converter:
val = attrib.converter(val)
object.__setattr__(self, attrib.name, val)
object.__setattr__(self, 'cost', None)
return
if self.ok_ret is not self.ALL_OK:
object.__setattr__(self, 'ok_ret', frozenset(self.ok_ret))
object.__setattr__(self, 'env', freeze(self.env))
# Ensure that output placeholders don't have ambiguously overlapping names.
placeholders = set()
collisions = set()
ns_str = None
for itm in self.cmd:
if isinstance(itm, OutputPlaceholder):
key = itm.namespaces, itm.name
if key in placeholders:
ns_str = '.'.join(itm.namespaces)
if itm.name is None:
collisions.add("{} unnamed".format(ns_str))
else:
collisions.add("{} named {!r}".format(ns_str, itm.name))
else:
placeholders.add(key)
if collisions:
raise ValueError(
'Found conflicting Placeholders: {!r}. Please give these placeholders'
' unique "name"s and access them like `step_result.{}s[name]`.'
.format(list(collisions), ns_str))
# Used with to indicate that all retcodes values are acceptable.