本文整理汇总了Python中anytree.NodeMixin方法的典型用法代码示例。如果您正苦于以下问题:Python anytree.NodeMixin方法的具体用法?Python anytree.NodeMixin怎么用?Python anytree.NodeMixin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类anytree
的用法示例。
在下文中一共展示了anytree.NodeMixin方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_dict_exporter_mixin
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_dict_exporter_mixin():
"""Dict Exporter."""
class MyClass(NodeMixin):
def __init__(self, foo, parent=None):
super(MyClass, self).__init__()
self.foo = foo
self.parent = parent
root = MyClass('root')
s0 = MyClass('s0', parent=root)
s0b = MyClass('s0b', parent=s0)
s0a = MyClass('s0a', parent=s0)
s1 = MyClass('s1', parent=root)
s1a = MyClass('s1a', parent=s1)
s1b = MyClass('s1b', parent=s1)
s1c = MyClass('s1c', parent=s1)
s1ca = MyClass('s1ca', parent=s1c)
exporter = DictExporter()
eq_(exporter.export(root),
{'foo': 'root', 'children': [
{'foo': 's0', 'children': [
{'foo': 's0b'},
{'foo': 's0a'}
]},
{
'foo': 's1', 'children': [
{'foo': 's1a'},
{'foo': 's1b'},
{'foo': 's1c', 'children': [
{'foo': 's1ca'}
]}
]}
]}
)
示例2: test_node_parent_error
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_node_parent_error():
"""Node Parent Error."""
with assert_raises(TreeError, "Parent node 'parent' is not of type 'NodeMixin'."):
Node("root", "parent")
示例3: test_node_children_type
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_node_children_type():
root = Node("root")
with assert_raises(TreeError, "Cannot add non-node object 'string'. It is not a subclass of 'NodeMixin'."):
root.children = ["string"]
示例4: test_parent
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_parent():
"""Parent attribute."""
foo = NodeMixin()
assert foo.parent is None
示例5: test_any_node_parent_error
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_any_node_parent_error():
"""Any Node Parent Error."""
with assert_raises(TreeError, "Parent node 'r' is not of type 'NodeMixin'."):
AnyNode("r")
示例6: test_tuple
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_tuple():
"""Tuple as parent."""
with assert_raises(TreeError, "Parent node (1, 0, 3) is not of type 'NodeMixin'."):
Node((0, 1, 2), parent=(1, 0, 3))
示例7: test_tuple_as_children
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def test_tuple_as_children():
"""Tuple as children."""
n = Node('foo')
with assert_raises(TreeError, "Cannot add non-node object (0, 1, 2). It is not a subclass of 'NodeMixin'."):
n.children = [(0, 1, 2)]
示例8: __new__
# 需要导入模块: import anytree [as 别名]
# 或者: from anytree import NodeMixin [as 别名]
def __new__(cls, *args, **kwargs):
for attr in SPECIAL_METHODS:
setattr(cls, attr, functools.partial(prevent_access, attr))
instance = super(NodeMixin, cls).__new__(cls)
return instance