本文整理汇总了Python中attr.astuple方法的典型用法代码示例。如果您正苦于以下问题:Python attr.astuple方法的具体用法?Python attr.astuple怎么用?Python attr.astuple使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类attr
的用法示例。
在下文中一共展示了attr.astuple方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simple_example
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_simple_example(self):
t = tskit.NodeTable()
t.add_row(flags=0, time=1, population=2, individual=0, metadata=b"123")
t.add_row(flags=1, time=2, population=3, individual=1, metadata=b"\xf0")
s = str(t)
self.assertGreater(len(s), 0)
self.assertEqual(len(t), 2)
self.assertEqual(attr.astuple(t[0]), (0, 1, 2, 0, b"123"))
self.assertEqual(attr.astuple(t[1]), (1, 2, 3, 1, b"\xf0"))
self.assertEqual(t[0].flags, 0)
self.assertEqual(t[0].time, 1)
self.assertEqual(t[0].population, 2)
self.assertEqual(t[0].individual, 0)
self.assertEqual(t[0].metadata, b"123")
self.assertEqual(t[0], t[-2])
self.assertEqual(t[1], t[-1])
self.assertRaises(IndexError, t.__getitem__, -3)
示例2: test_recurse_property
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_recurse_property(self, cls, tuple_class):
"""
Property tests for recursive astuple.
"""
obj = cls()
obj_tuple = astuple(obj, tuple_factory=tuple_class)
def assert_proper_tuple_class(obj, obj_tuple):
assert isinstance(obj_tuple, tuple_class)
for index, field in enumerate(fields(obj.__class__)):
field_val = getattr(obj, field.name)
if has(field_val.__class__):
# This field holds a class, recurse the assertions.
assert_proper_tuple_class(field_val, obj_tuple[index])
assert_proper_tuple_class(obj, obj_tuple)
示例3: decode_mnemonic
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def decode_mnemonic(mnemonic):
return list(attr.astuple(Share.from_mnemonic(mnemonic)))
示例4: pattern2loc
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def pattern2loc(pattern_list):
results = []
dupset = set()
proc = lambda decl: lsp.Location(uri=path2uri(decl.path), range=decl.range)
for x in pattern_list:
x = proc(x)
key = attr.astuple(x)
if key not in dupset:
dupset.add(key)
results.append(attr.asdict(x))
return results
示例5: test_constructor_with_state_only
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_constructor_with_state_only(self):
ip = measured_process.MeasuredProcess(
_build_initialize_comp(0), count_int32)
state = ip.initialize()
iterations = 10
for _ in range(iterations):
state, result, measurements = attr.astuple(ip.next(state))
self.assertLen(result, 0)
self.assertLen(measurements, 0)
self.assertEqual(state, iterations)
示例6: to_jsonable
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def to_jsonable(self):
return attr.astuple(self)
# Allow HistoryItems to act like tuples for unpacking.
示例7: __iter__
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def __iter__(self):
return iter(attr.astuple(self, recurse=False))
示例8: __eq__
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def __eq__(self, other: "BaseSignedData") -> bool:
if isinstance(other, type(self)):
return attr.astuple(self).__eq__(attr.astuple(other))
return NotImplemented
示例9: __eq__
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def __eq__(self, other: object) -> bool:
if isinstance(other, int):
return self.start.__eq__(other)
if isinstance(other, Chunk):
return attr.astuple(self).__eq__(attr.astuple(other))
raise TypeError
# Properties
示例10: test_shallow
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_shallow(self, C, tuple_factory):
"""
Shallow astuple returns correct dict.
"""
assert tuple_factory([1, 2]) == astuple(
C(x=1, y=2), False, tuple_factory=tuple_factory
)
示例11: test_recurse
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_recurse(self, C, tuple_factory):
"""
Deep astuple returns correct tuple.
"""
assert tuple_factory(
[tuple_factory([1, 2]), tuple_factory([3, 4])]
) == astuple(C(C(1, 2), C(3, 4)), tuple_factory=tuple_factory)
示例12: test_recurse_retain
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_recurse_retain(self, cls, tuple_class):
"""
Property tests for asserting collection types are retained.
"""
obj = cls()
obj_tuple = astuple(
obj, tuple_factory=tuple_class, retain_collection_types=True
)
def assert_proper_col_class(obj, obj_tuple):
# Iterate over all attributes, and if they are lists or mappings
# in the original, assert they are the same class in the dumped.
for index, field in enumerate(fields(obj.__class__)):
field_val = getattr(obj, field.name)
if has(field_val.__class__):
# This field holds a class, recurse the assertions.
assert_proper_col_class(field_val, obj_tuple[index])
elif isinstance(field_val, (list, tuple)):
# This field holds a sequence of something.
expected_type = type(obj_tuple[index])
assert type(field_val) is expected_type
for obj_e, obj_tuple_e in zip(field_val, obj_tuple[index]):
if has(obj_e.__class__):
assert_proper_col_class(obj_e, obj_tuple_e)
elif isinstance(field_val, dict):
orig = field_val
tupled = obj_tuple[index]
assert type(orig) is type(tupled)
for obj_e, obj_tuple_e in zip(
orig.items(), tupled.items()
):
if has(obj_e[0].__class__): # Dict key
assert_proper_col_class(obj_e[0], obj_tuple_e[0])
if has(obj_e[1].__class__): # Dict value
assert_proper_col_class(obj_e[1], obj_tuple_e[1])
assert_proper_col_class(obj, obj_tuple)
示例13: test_filter
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_filter(self, C, tuple_factory):
"""
Attributes that are supposed to be skipped are skipped.
"""
assert tuple_factory([tuple_factory([1])]) == astuple(
C(C(1, 2), C(3, 4)),
filter=lambda a, v: a.name != "y",
tuple_factory=tuple_factory,
)
示例14: test_dicts
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_dicts(self, C, tuple_factory):
"""
If recurse is True, also recurse into dicts.
"""
res = astuple(C(1, {"a": C(4, 5)}), tuple_factory=tuple_factory)
assert tuple_factory([1, {"a": tuple_factory([4, 5])}]) == res
assert isinstance(res, tuple_factory)
示例15: test_lists_tuples_retain_type
# 需要导入模块: import attr [as 别名]
# 或者: from attr import astuple [as 别名]
def test_lists_tuples_retain_type(self, container, C):
"""
If recurse and retain_collection_types are True, also recurse
into lists and do not convert them into list.
"""
assert (1, container([(2, 3), (4, 5), "a"])) == astuple(
C(1, container([C(2, 3), C(4, 5), "a"])),
retain_collection_types=True,
)