本文整理匯總了Python中attr.ib方法的典型用法代碼示例。如果您正苦於以下問題:Python attr.ib方法的具體用法?Python attr.ib怎麽用?Python attr.ib使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類attr
的用法示例。
在下文中一共展示了attr.ib方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: files
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def files(self, tmpdir):
@attr.s
class Files:
auto_config_dir = attr.ib()
config_dir = attr.ib()
local_data_dir = attr.ib()
roaming_data_dir = attr.ib()
return Files(
auto_config_dir=tmpdir / 'auto_config' / APPNAME,
config_dir=tmpdir / 'config' / APPNAME,
local_data_dir=tmpdir / 'data' / APPNAME,
roaming_data_dir=tmpdir / 'roaming-data' / APPNAME,
)
示例2: _parse_split_test_data_str
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def _parse_split_test_data_str():
"""Parse the test data set into a TestCase object to use in tests.
Returns:
A list of TestCase objects with str attributes: inp, keep, no_keep
"""
@attr.s
class TestCase:
inp = attr.ib()
keep = attr.ib()
no_keep = attr.ib()
for line in test_data_str.splitlines():
if not line:
continue
data = line.split('/')
item = TestCase(inp=data[0], keep=data[1].split('|'),
no_keep=data[2].split('|'))
yield item
yield TestCase(inp='', keep=[], no_keep=[])
示例3: test_attrib_equality_attrs_attributes_cmp_equality
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_attrib_equality_attrs_attributes_cmp_equality(x, y):
for i, v1 in enumerate((x, y)):
for j, v2 in enumerate((x, y)):
if i == j:
assume(v1 == v2)
else:
assume(v1 != v2)
@attr.s
class Foo:
a = attr.ib()
b = attr.ib(default=None)
assert attrib_equality(Foo(x), Foo(x))
assert not attrib_equality(Foo(x), Foo(y))
assert attrib_equality(Foo(x), Foo(x), "a")
assert not attrib_equality(Foo(x), Foo(y), "a")
assert attrib_equality(Foo(x), Foo(y), "b")
assert attrib_equality(Foo(x), Foo(x), "a, b")
assert not attrib_equality(Foo(x), Foo(y), "a, b")
assert attrib_equality(Foo(x), Foo(x), ("a", "b"))
assert not attrib_equality(Foo(x), Foo(y), ("a", "b"))
示例4: test_attrib_equality_attrs_attributes_attrs_equality
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_attrib_equality_attrs_attributes_attrs_equality(x, y, z):
for i, v1 in enumerate((x, y, z)):
for j, v2 in enumerate((x, y, z)):
if i == j:
assume(v1 == v2)
else:
assume(v1 != v2)
@attr.s
class Foo:
a = attr.ib()
b = attr.ib(eq=False)
assert attrib_equality(Foo(x, y), Foo(x, z))
assert not attrib_equality(Foo(x, y), Foo(y, z))
assert not attrib_equality(Foo(x, y), Foo(y, y))
示例5: test_attrib_equality_attrs_test
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_attrib_equality_attrs_test(x, y):
for i, v1 in enumerate((x, y)):
for j, v2 in enumerate((x, y)):
if i == j:
assume(v1 == v2)
else:
assume(v1 != v2)
@attr.s
class Bar:
a = attr.ib(eq=True)
@attr.s
class Baz:
a = attr.ib(eq=False)
@attr.s
class Foo:
x = attr.ib()
assert attrib_equality(Foo(Baz(x)), Foo(Baz(y)))
assert not attrib_equality(Foo(Bar(x)), Foo(Bar(y)))
示例6: test_capture_result_with_attrs_of_constants
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_capture_result_with_attrs_of_constants(self):
@attr.s
class TestFoo(object):
x = attr.ib()
y = attr.ib()
graph = tf.compat.v1.get_default_graph()
type_spec, _ = tensorflow_utils.capture_result_from_graph(
TestFoo(tf.constant(1), tf.constant(True)), graph)
self.assertEqual(str(type_spec), '<x=int32,y=bool>')
self.assertIsInstance(type_spec,
computation_types.NamedTupleTypeWithPyContainerType)
self.assertIs(
computation_types.NamedTupleTypeWithPyContainerType.get_container_type(
type_spec), TestFoo)
示例7: test_with_nested_attrs_class
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_with_nested_attrs_class(self):
@attr.s
class TestAttrClass(object):
a = attr.ib()
b = attr.ib()
t = type_conversions.infer_type(TestAttrClass(a=0, b={'x': True, 'y': 0.0}))
self.assertEqual(str(t), '<a=int32,b=<x=bool,y=float32>>')
self.assertIsInstance(t,
computation_types.NamedTupleTypeWithPyContainerType)
self.assertIs(
computation_types.NamedTupleTypeWithPyContainerType.get_container_type(
t), TestAttrClass)
self.assertIs(
computation_types.NamedTupleTypeWithPyContainerType.get_container_type(
t.b), dict)
示例8: Metafunc
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def Metafunc(self, func, config=None) -> python.Metafunc:
# the unit tests of this class check if things work correctly
# on the funcarg level, so we don't need a full blown
# initialization
class FuncFixtureInfoMock:
name2fixturedefs = None
def __init__(self, names):
self.names_closure = names
@attr.s
class DefinitionMock(python.FunctionDefinition):
obj = attr.ib()
_nodeid = attr.ib()
names = fixtures.getfuncargnames(func)
fixtureinfo = FuncFixtureInfoMock(names) # type: Any
definition = DefinitionMock._create(func, "mock::nodeid") # type: Any
return python.Metafunc(definition, fixtureinfo, config)
示例9: test_attrs_recursive
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_attrs_recursive(self) -> None:
@attr.s
class OtherDataObject:
field_c = attr.ib()
field_d = attr.ib()
@attr.s
class SimpleDataObject:
field_a = attr.ib()
field_b = attr.ib()
left = SimpleDataObject(OtherDataObject(1, "a"), "b")
right = SimpleDataObject(OtherDataObject(1, "b"), "b")
lines = callequal(left, right)
assert lines is not None
assert "Matching attributes" not in lines
for line in lines[1:]:
assert "field_b:" not in line
assert "field_c:" not in line
示例10: test_attrs_recursive_verbose
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_attrs_recursive_verbose(self) -> None:
@attr.s
class OtherDataObject:
field_c = attr.ib()
field_d = attr.ib()
@attr.s
class SimpleDataObject:
field_a = attr.ib()
field_b = attr.ib()
left = SimpleDataObject(OtherDataObject(1, "a"), "b")
right = SimpleDataObject(OtherDataObject(1, "b"), "b")
lines = callequal(left, right)
assert lines is not None
assert "field_d: 'a' != 'b'" in lines
示例11: test_attrs_with_attribute_comparison_off
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_attrs_with_attribute_comparison_off(self):
@attr.s
class SimpleDataObject:
field_a = attr.ib()
field_b = attr.ib(**{ATTRS_EQ_FIELD: False}) # type: ignore
left = SimpleDataObject(1, "b")
right = SimpleDataObject(1, "b")
lines = callequal(left, right, verbose=2)
assert lines is not None
assert lines[1].startswith("Matching attributes:")
assert "Omitting" not in lines[1]
assert lines[2] == "['field_a']"
for line in lines[2:]:
assert "field_b" not in line
示例12: test_comparing_two_different_attrs_classes
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_comparing_two_different_attrs_classes(self):
@attr.s
class SimpleDataObjectOne:
field_a = attr.ib()
field_b = attr.ib()
@attr.s
class SimpleDataObjectTwo:
field_a = attr.ib()
field_b = attr.ib()
left = SimpleDataObjectOne(1, "b")
right = SimpleDataObjectTwo(1, "c")
lines = callequal(left, right)
assert lines is None
示例13: ssl_conf
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def ssl_conf(request):
@attr.s
class SSLConf:
@property
def use_ssl(self):
return bool(self.backend_opts)
client_env = attr.ib(factory=dict)
backend_opts = attr.ib(default="")
if not request.param:
yield SSLConf()
else:
ca = trustme.CA()
server_cert = ca.issue_cert("localhost")
with ca.cert_pem.tempfile() as ca_certfile, server_cert.cert_chain_pems[
0
].tempfile() as server_certfile, server_cert.private_key_pem.tempfile() as server_keyfile:
yield SSLConf(
backend_opts=f" --ssl-keyfile={server_keyfile} --ssl-certfile={server_certfile} ",
client_env={"SSL_CAFILE": ca_certfile},
)
示例14: testAttrsFlattenAndUnflatten
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def testAttrsFlattenAndUnflatten(self):
class BadAttr(object):
"""Class that has a non-iterable __attrs_attrs__."""
__attrs_attrs__ = None
@attr.s
class SampleAttr(object):
field1 = attr.ib()
field2 = attr.ib()
field_values = [1, 2]
sample_attr = SampleAttr(*field_values)
self.assertFalse(tree._is_attrs(field_values))
self.assertTrue(tree._is_attrs(sample_attr))
flat = tree.flatten(sample_attr)
self.assertEqual(field_values, flat)
restructured_from_flat = tree.unflatten_as(sample_attr, flat)
self.assertIsInstance(restructured_from_flat, SampleAttr)
self.assertEqual(restructured_from_flat, sample_attr)
# Check that flatten fails if attributes are not iterable
with self.assertRaisesRegex(TypeError, "object is not iterable"):
flat = tree.flatten(BadAttr())
示例15: test_enforces_type
# 需要導入模塊: import attr [as 別名]
# 或者: from attr import ib [as 別名]
def test_enforces_type(self):
"""
The `hash` argument to both attrs and attrib must be None, True, or
False.
"""
exc_args = ("Invalid value for hash. Must be True, False, or None.",)
with pytest.raises(TypeError) as e:
make_class("C", {}, hash=1),
assert exc_args == e.value.args
with pytest.raises(TypeError) as e:
make_class("C", {"a": attr.ib(hash=1)}),
assert exc_args == e.value.args