Python anytree.NodeMixin() Examples

The following are 8 code examples of anytree.NodeMixin(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module anytree , or try the search function .
Example #1
Source File: test_dictexporter.py    From anytree with Apache License 2.0 5 votes vote down vote up
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'}
                    ]}
                ]}
        ]}
        ) 
Example #2
Source File: test_node.py    From anytree with Apache License 2.0 5 votes vote down vote up
def test_node_parent_error():
    """Node Parent Error."""
    with assert_raises(TreeError, "Parent node 'parent' is not of type 'NodeMixin'."):
        Node("root", "parent") 
Example #3
Source File: test_node.py    From anytree with Apache License 2.0 5 votes vote down vote up
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"] 
Example #4
Source File: test_node.py    From anytree with Apache License 2.0 5 votes vote down vote up
def test_parent():
    """Parent attribute."""
    foo = NodeMixin()
    assert foo.parent is None 
Example #5
Source File: test_node.py    From anytree with Apache License 2.0 5 votes vote down vote up
def test_any_node_parent_error():
    """Any Node Parent Error."""

    with assert_raises(TreeError, "Parent node 'r' is not of type 'NodeMixin'."):
        AnyNode("r") 
Example #6
Source File: test_node.py    From anytree with Apache License 2.0 5 votes vote down vote up
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)) 
Example #7
Source File: test_node.py    From anytree with Apache License 2.0 5 votes vote down vote up
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)] 
Example #8
Source File: test_special_methods_access.py    From anytree with Apache License 2.0 5 votes vote down vote up
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