verysimpletree API

Warning

This documentation is still under construction!

exception verysimpletree.tree.ChildNotFoundError[source]

Bases: TreeException

exception verysimpletree.tree.TreeException[source]

Bases: Exception

exception verysimpletree.tree.TreeReferenceError[source]

Bases: TreeException, AttributeError

class verysimpletree.tree.TestTree(name: str = '', *args: Any, **kwargs: Any)[source]

Bases: Tree[Any]

add_string_child(name: str) TestTree[source]
class verysimpletree.tree.Tree(content: Any = None, *args: Any, **kwargs: Any)[source]

Bases: ABC, Generic[T]

An abstract lightweight tree class for managing tree structures in MusicXML and musicscore packages.

add_child(child: T) T[source]

Tree method

Check and add child to list of children. Child’s parent is set to self.

Parameters:

child

Returns:

child

Return type:

Tree

classmethod create_tree_from_list(tree_list_representation: list[list[Any]], represented_attribute_names: list[str]) T[source]
filter_nodes(key: Callable[[T], Any], return_value: Any) list[T][source]

Tree method

>>> root.filter_nodes(lambda node: node.get_level(), 2)
[grandchild1, grandchild2, grandchild3]
get_children() list[T][source]

Tree method

Returns:

list of added children.

get_children_of_type(type_: type) list[T][source]

Tree method

Returns:

list of added children of type.d

Return type:

list[Tree]

get_distance(reference: T | None = None) int[source]
>>> root.get_distance()
0
>>> greatgrandchild1.get_distance()
3
>>> greatgrandchild1.get_distance(child2)
2
get_farthest_leaf() T[source]
>>> root.get_farthest_leaf()
greatgrandchild1
get_layer(level: int, key: Callable[[T], Any] | None = None) Any[source]

Tree method

Parameters:
  • level – layer number where 0 is the root.

  • key – An optional callable for each node in the layer.

Returns:

All nodes on this level. The leaves of branches which are shorter than the given level will be repeated on this and all following layers.

Return type:

list

get_leaves(key: Callable[[T], Any] | None = None) list[Union[Any, list[Any]]][source]

Tree method

Parameters:

key – An optional callable to be called on each leaf.

Returns:

nested list of leaves or values of key(leaf) for each leaf

>>> root.get_leaves()
[child1, [[greatgrandchild1, greatgrandchild2], grandchild2], child3, [grandchild3]]
get_level() int[source]

Tree

Returns:

0 for root, 1, 2 etc. for each layer of children

Return type:

nonnegative int

>>> root.get_level()
0
>>> child1.get_level()
1
>>> grandchild1.get_level()
2
>>> greatgrandchild1.get_level()
3
get_list_representation(key: Callable[[Tree[T]], Any]) list[list[Any]][source]
get_number_of_layers() int[source]
>>> root.get_number_of_layers()
3
get_parent() T | None[source]

Tree method

Returns:

parent. None for root.

Return type:

Tree

get_position_in_tree() str[source]

Tree method

Returns:

0 for root. 1, 2, … for layer 1. Other layers: x.y.z…. Example: 3.2.2 => third child of secod child of second child of the root.

Return type:

str

>>> print(root.get_tree_representation(key=lambda node: node.get_position_in_tree()))
└── 0
    ├── 1
    ├── 2
    │   ├── 2.1
    │   │   ├── 2.1.1
    │   │   └── 2.1.2
    │   └── 2.2
    ├── 3
    └── 4
        └── 4.1
get_reversed_path_to_root() list[T][source]

Tree method

Returns:

path from self upwards through all ancestors up to the root.

>>> greatgrandchild1.get_reversed_path_to_root()
[greatgrandchild1, grandchild1, child2, root]
get_root() T[source]

Tree method

Returns:

root (upmost node of a tree which has no parent)

Return type:

Tree

>>> greatgrandchild1.get_root() == root
True
>>> child4.get_root() == root
True
>>> root.get_root() == root
True
get_self_with_key(key: Callable[[T], Any] | None = None) Any[source]
get_tree_representation(key: Callable[[T], Any] | None = None, space: int = 3) str[source]

Tree method

Parameters:

key – An optional callable if None string(node) is called.

Returns:

a representation of all nodes as string in tree form.

>>> print(root.get_tree_representation())
└── root
    ├── child1
    ├── child2
    │   ├── grandchild1
    │   │   ├── greatgrandchild1
    │   │   └── greatgrandchild2
    │   └── grandchild2
    ├── child3
    └── child4
        └── grandchild3
iterate_leaves() Iterator[T][source]

Tree method

Returns:

A generator iterating over all leaves.

remove(child: T) None[source]

Tree method

Child’s parent will be set to None and child will be removed from list of children.

Parameters:

child

Returns:

None

remove_children() None[source]

Tree method

Calls remove() on all children.

Returns:

None

replace_child(old: T, new: T, index: int = 0) None[source]

Tree method

Parameters:
  • old – child or function

  • new – child

  • index – index of old child in the list of its appearances

Returns:

None

traverse() Iterator[T][source]

Tree method

Traverse all tree nodes.

Returns:

generator

property content: Any
property is_first_child: bool
property is_last_child: bool
>>> t = TestTree('root')
>>> for node in t.traverse():
...    if node.name in ['root', 'child4', 'grandchild2', 'grandchild3', 'greatgrandchild1']:
...        assert node.is_last_child
...    else:
...        assert not node.is_last_child
property is_leaf: bool

Tree property

Returns:

True if self has no children. False if self has one or more children.

Return type:

bool

property is_root: bool

Tree property

Returns:

True if self has no parent, else False.

Return type:

bool

property next: T | None

Tree property

Returns:

next sibling. None if this is the last current child of the parent.

Return type:

Tree

property previous: T | None

Tree property

Returns:

previous sibling. None if this is the first child of the parent.

Return type:

Tree

property up: T | None

Tree property

Returns:

get_parent()

Return type:

Tree

class verysimpletree.tree.TreeRepresentation(tree: ~verysimpletree.tree.Tree[~typing.Any], key: ~typing.Callable[[~verysimpletree.tree.Tree[~typing.Any]], ~typing.Any] = <function TreeRepresentation.<lambda>>, space: int = 3)[source]

Bases: object

get_representation() str[source]
>>> rep = TreeRepresentation(tree=root, key=lambda node: node.get_position_in_tree())
>>> print(rep.get_representation())
└── 0
    ├── 1
    ├── 2
    │   ├── 2.1
    │   │   ├── 2.1.1
    │   │   └── 2.1.2
    │   └── 2.2
    ├── 3
    └── 4
        └── 4.1

>>> rep = TreeRepresentation(tree=root, key=lambda node: node.get_position_in_tree(), space=1)
>>> print(rep.get_representation())
└ 0
  ├ 1
  ├ 2
  │ ├ 2.1
  │ │ ├ 2.1.1
  │ │ └ 2.1.2
  │ └ 2.2
  ├ 3
  └ 4
    └ 4.1
property key: Callable[[Tree[Any]], Any]
property space: int
property tree: Tree[Any]