本文整理汇总了Python中attr.Attribute方法的典型用法代码示例。如果您正苦于以下问题:Python attr.Attribute方法的具体用法?Python attr.Attribute怎么用?Python attr.Attribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attr
的用法示例。
在下文中一共展示了attr.Attribute方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_attribute_section
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def add_attribute_section(process, placeholder="{{attributes}}"):
data_type = ":class:`attr.Attribute`"
fmt_vars = []
for vname, var in variables_dict(process).items():
var_header = f"{vname} : {data_type}"
var_content = textwrap.indent(var_details(var, max_line_length=62), " " * 4)
fmt_vars.append(f"{var_header}\n{var_content}")
fmt_section = textwrap.indent(
"Attributes\n" "----------\n" + "\n".join(fmt_vars), " " * 4
)
current_doc = process.__doc__ or ""
if placeholder in current_doc:
new_doc = current_doc.replace(placeholder, fmt_section[4:])
else:
new_doc = f"{current_doc.rstrip()}\n\n{fmt_section}\n"
return new_doc
示例2: __setattr__
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def __setattr__(self, name, value):
"""Mimic attr.s(frozen=True) behaviour but allow for attributes
to be initialized after class instantiation.
Useful when you would like a class to be immutable after a
certain action, such as a save to a database.
Any attributes created with ``attr.ib(init=False)`` or are
initially set to ``None`` in ``__init__()`` are allowed to have
their values be set once after initialization. Any other
attributes with initial values set are immediately frozen upon
initialization.
**Note**: Obviously, this doesn't stop anyone from setting the
uninitialized attributes before you've set it yourself.
Hopefully, you've got responsibile users.
Raises:
:class:`attr.exceptions.FronzenInstanceError`: if a frozen
attribute is set
"""
current_value = getattr(self, name, None)
if current_value is None or isinstance(current_value, attr.Attribute):
super().__setattr__(name, value)
else:
raise attr.exceptions.FrozenInstanceError()
示例3: create_attribute
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def create_attribute():
return attr.Attribute(
name='x',
default=None,
validator=None,
repr=True,
cmp=None,
eq=True,
order=False,
hash=True,
init=True)
示例4: _expand_responses_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _expand_responses_validator(self, _: "attr.Attribute[str]", value: str) -> None:
if value in ("all", ""):
return
raw_codes = value.split(",")
for raw_code in raw_codes:
try:
int(raw_code)
except ValueError:
raise ValueError(
"expandResponses must be either 'all' or "
f"comma-separated list of http codes, got '{raw_code}'"
)
# noinspection PyUnresolvedReferences
示例5: _json_sample_expand_level_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _json_sample_expand_level_validator(
self, _: "attr.Attribute[Union[int, str]]", value: Union[int, str]
) -> None:
if isinstance(value, str) and value != "all":
raise ValueError(
f"jsonSampleExpandLevel must be either 'all' or integer, got '{value}'"
)
示例6: _bg_color_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _bg_color_validator(self, _: "attr.Attribute[str]", value: str) -> None:
if not HEX_COLOR_REGEX.match(value):
raise ValueError("bg_color must be valid HEX color")
# noinspection PyUnresolvedReferences
示例7: _text_color_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _text_color_validator(self, _: "attr.Attribute[str]", value: str) -> None:
if not HEX_COLOR_REGEX.match(value):
raise ValueError("text_color must be valid HEX color")
# noinspection PyUnresolvedReferences
示例8: _nav_bg_color_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _nav_bg_color_validator(
self, _: "attr.Attribute[Optional[str]]", value: Optional[str]
) -> None:
if value is not None and not HEX_COLOR_REGEX.match(value):
raise ValueError("nav_bg_color must be valid HEX color")
示例9: _nav_hover_bg_color_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _nav_hover_bg_color_validator(
self, _: "attr.Attribute[Optional[str]]", value: Optional[str]
) -> None:
if value is not None and not HEX_COLOR_REGEX.match(value):
raise ValueError("nav_hover_bg_color must be valid HEX color")
示例10: _nav_hover_text_color_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _nav_hover_text_color_validator(
self, _: "attr.Attribute[Optional[str]]", value: Optional[str]
) -> None:
if value is not None and not HEX_COLOR_REGEX.match(value):
raise ValueError("nav_hover_text_color must be valid HEX color")
示例11: _nav_accent_color_validator
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _nav_accent_color_validator(
self, _: "attr.Attribute[Optional[str]]", value: Optional[str]
) -> None:
if value is not None and not HEX_COLOR_REGEX.match(value):
raise ValueError("nav_accent_color must be valid HEX color")
示例12: _get_attr_from_attrs
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def _get_attr_from_attrs(attrs: Iterable[Attribute], name: str) -> Attribute:
attrs = [a for a in attrs if a.name == name]
return attrs[0]
示例13: simple_attr
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def simple_attr(
name,
default=NOTHING,
validator=None,
repr=True,
eq=True,
hash=None,
init=True,
converter=None,
kw_only=False,
inherited=False,
):
"""
Return an attribute with a name and no other bells and whistles.
"""
return Attribute(
name=name,
default=default,
validator=validator,
repr=repr,
cmp=None,
eq=eq,
hash=hash,
init=init,
converter=converter,
kw_only=kw_only,
inherited=inherited,
)
示例14: get_units
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def get_units(field: attr.Attribute) -> str:
return field.metadata.get(UNIT_SUFFIX, "")
示例15: test_add_attribute_section
# 需要导入模块: import attr [as 别名]
# 或者: from attr import Attribute [as 别名]
def test_add_attribute_section():
# For testing, autodoc is set to False to avoid redundancy
expected = """My process
Attributes
----------
var1 : :class:`attr.Attribute`
A variable
Variable properties:
- type : ``variable``
- intent : ``in``
- dimensions : ('x',)
var2 : :class:`attr.Attribute`
No description given
Variable properties:
- type : ``variable``
- intent : ``in``
- dimensions : ()
"""
assert add_attribute_section(WithoutPlaceHolder).strip() == expected.strip()
assert add_attribute_section(WithPlaceholder).strip() == expected.strip()