bigtree

🌱 BaseNode¢

Classes:

BaseNode([parent,Β children])

BaseNode extends any Python class to a tree node.

class bigtree.node.basenode.BaseNode(parent: T | None = None, children: List[T] | None = None, **kwargs: Any)ΒΆ

Bases: object

BaseNode extends any Python class to a tree node. Nodes can have attributes if they are initialized from Node, dictionary, or pandas DataFrame.

Nodes can be linked to each other with parent and children setter methods, or using bitshift operator with the convention parent_node >> child_node or child_node << parent_node.

>>> from bigtree import Node, print_tree
>>> root = Node("a", age=90)
>>> b = Node("b", age=65)
>>> c = Node("c", age=60)
>>> d = Node("d", age=40)
>>> root.children = [b, c]
>>> d.parent = b
>>> print_tree(root, attr_list=["age"])
a [age=90]
β”œβ”€β”€ b [age=65]
β”‚   └── d [age=40]
└── c [age=60]
>>> from bigtree import Node
>>> root = Node("a", age=90)
>>> b = Node("b", age=65)
>>> c = Node("c", age=60)
>>> d = Node("d", age=40)
>>> root >> b
>>> root >> c
>>> d << b
>>> print_tree(root, attr_list=["age"])
a [age=90]
β”œβ”€β”€ b [age=65]
β”‚   └── d [age=40]
└── c [age=60]

Directly passing parent argument.

>>> from bigtree import Node
>>> root = Node("a")
>>> b = Node("b", parent=root)
>>> c = Node("c", parent=root)
>>> d = Node("d", parent=b)

Directly passing children argument.

>>> from bigtree import Node
>>> d = Node("d")
>>> c = Node("c")
>>> b = Node("b", children=[d])
>>> a = Node("a", children=[b, c])

BaseNode Creation

Node can be created by instantiating a BaseNode class or by using a dictionary. If node is created with dictionary, all keys of dictionary will be stored as class attributes.

>>> from bigtree import Node
>>> root = Node.from_dict({"name": "a", "age": 90})

BaseNode Attributes

These are node attributes that have getter and/or setter methods.

Get and set other BaseNode

  1. parent: Get/set parent node

  2. children: Get/set child nodes

Get other BaseNode

  1. ancestors: Get ancestors of node excluding self, iterator

  2. descendants: Get descendants of node excluding self, iterator

  3. leaves: Get all leaf node(s) from self, iterator

  4. siblings: Get siblings of self

  5. left_sibling: Get sibling left of self

  6. right_sibling: Get sibling right of self

Get BaseNode configuration

  1. node_path: Get tuple of nodes from root

  2. is_root: Get indicator if self is root node

  3. is_leaf: Get indicator if self is leaf node

  4. root: Get root node of tree

  5. depth: Get depth of self

  6. max_depth: Get maximum depth from root to leaf node

BaseNode Methods

These are methods available to be performed on BaseNode.

Constructor methods

  1. from_dict(): Create BaseNode from dictionary

BaseNode methods

  1. describe(): Get node information sorted by attributes, return list of tuples

  2. get_attr(attr_name: str): Get value of node attribute

  3. set_attrs(attrs: dict): Set node attribute name(s) and value(s)

  4. go_to(node: Self): Get a path from own node to another node from same tree

  5. append(node: Self): Add child to node

  6. extend(nodes: List[Self]): Add multiple children to node

  7. copy(): Deep copy self

  8. sort(): Sort child nodes


Attributes:

ancestors

Get iterator to yield all ancestors of self, does not include self

children

Get child nodes

depth

Get depth of self, indexing starts from 1

descendants

Get iterator to yield all descendants of self, does not include self

is_leaf

Get indicator if self is leaf node

is_root

Get indicator if self is root node

leaves

Get iterator to yield all leaf nodes from self

left_sibling

Get sibling left of self

max_depth

Get maximum depth from root to leaf node

node_path

Get tuple of nodes starting from root

parent

Get parent node

parents

Do not allow parents attribute to be accessed

right_sibling

Get sibling right of self

root

Get root node of tree

siblings

Get siblings of self

Methods:

append(other)

Add other as child of self

copy()

Deep copy self; clone self

describe([exclude_attributes,Β exclude_prefix])

Get node information sorted by attribute name, returns list of tuples

extend(others)

Add others as children of self

from_dict(input_dict)

Construct node from dictionary, all keys of dictionary will be stored as class attributes Input dictionary must have key name if not Node will not have any name

get_attr(attr_name[,Β default_value])

Get value of node attribute Returns default value if attribute name does not exist

go_to(node)

Get path from current node to specified node from same tree

set_attrs(attrs)

Set node attributes

sort(**kwargs)

Sort children, possible keyword arguments include key=lambda node: node.name, reverse=True

property ancestors: Iterable[T]ΒΆ

Get iterator to yield all ancestors of self, does not include self

Returns:

(Iterable[Self])

append(other: T) NoneΒΆ

Add other as child of self

Parameters:

other (Self) – other node, child to be added

property children: Tuple[T, ...]ΒΆ

Get child nodes

Returns:

(Tuple[Self, …])

copy() TΒΆ

Deep copy self; clone self

>>> from bigtree.node.node import Node
>>> a = Node('a')
>>> a_copy = a.copy()
Returns:

(Self)

property depth: intΒΆ

Get depth of self, indexing starts from 1

Returns:

(int)

property descendants: Iterable[T]ΒΆ

Get iterator to yield all descendants of self, does not include self

Returns:

(Iterable[Self])

describe(exclude_attributes: List[str] = [], exclude_prefix: str = '') List[Tuple[str, Any]]ΒΆ

Get node information sorted by attribute name, returns list of tuples

>>> from bigtree.node.node import Node
>>> a = Node('a', age=90)
>>> a.describe()
[('_BaseNode__children', []), ('_BaseNode__parent', None), ('_sep', '/'), ('age', 90), ('name', 'a')]
>>> a.describe(exclude_prefix="_")
[('age', 90), ('name', 'a')]
>>> a.describe(exclude_prefix="_", exclude_attributes=["name"])
[('age', 90)]
Parameters:
  • exclude_attributes (List[str]) – list of attributes to exclude

  • exclude_prefix (str) – prefix of attributes to exclude

Returns:

(List[Tuple[str, Any]])

extend(others: List[T]) NoneΒΆ

Add others as children of self

Parameters:

others (Self) – other nodes, children to be added

classmethod from_dict(input_dict: Dict[str, Any]) BaseNodeΒΆ

Construct node from dictionary, all keys of dictionary will be stored as class attributes Input dictionary must have key name if not Node will not have any name

>>> from bigtree import Node
>>> a = Node.from_dict({"name": "a", "age": 90})
Parameters:

input_dict (Dict[str, Any]) – dictionary with node information, key: attribute name, value: attribute value

Returns:

(BaseNode)

get_attr(attr_name: str, default_value: Any | None = None) AnyΒΆ

Get value of node attribute Returns default value if attribute name does not exist

>>> from bigtree.node.node import Node
>>> a = Node('a', age=90)
>>> a.get_attr("age")
90
Parameters:
  • attr_name (str) – attribute name

  • default_value (Any) – default value if attribute does not exist, defaults to None

Returns:

(Any)

go_to(node: T) Iterable[T]ΒΆ

Get path from current node to specified node from same tree

>>> from bigtree import Node, print_tree
>>> a = Node(name="a")
>>> b = Node(name="b", parent=a)
>>> c = Node(name="c", parent=a)
>>> d = Node(name="d", parent=b)
>>> e = Node(name="e", parent=b)
>>> f = Node(name="f", parent=c)
>>> g = Node(name="g", parent=e)
>>> h = Node(name="h", parent=e)
>>> print_tree(a)
a
β”œβ”€β”€ b
β”‚   β”œβ”€β”€ d
β”‚   └── e
β”‚       β”œβ”€β”€ g
β”‚       └── h
└── c
    └── f
>>> d.go_to(d)
[Node(/a/b/d, )]
>>> d.go_to(g)
[Node(/a/b/d, ), Node(/a/b, ), Node(/a/b/e, ), Node(/a/b/e/g, )]
>>> d.go_to(f)
[Node(/a/b/d, ), Node(/a/b, ), Node(/a, ), Node(/a/c, ), Node(/a/c/f, )]
Parameters:

node (Self) – node to travel to from current node, inclusive of start and end node

Returns:

(Iterable[Self])

property is_leaf: boolΒΆ

Get indicator if self is leaf node

Returns:

(bool)

property is_root: boolΒΆ

Get indicator if self is root node

Returns:

(bool)

property leaves: Iterable[T]ΒΆ

Get iterator to yield all leaf nodes from self

Returns:

(Iterable[Self])

property left_sibling: TΒΆ

Get sibling left of self

Returns:

(Self)

property max_depth: intΒΆ

Get maximum depth from root to leaf node

Returns:

(int)

property node_path: Iterable[T]ΒΆ

Get tuple of nodes starting from root

Returns:

(Iterable[Self])

property parent: T | NoneΒΆ

Get parent node

Returns:

(Optional[Self])

property parents: NoneΒΆ

Do not allow parents attribute to be accessed

property right_sibling: TΒΆ

Get sibling right of self

Returns:

(Self)

property root: TΒΆ

Get root node of tree

Returns:

(Self)

set_attrs(attrs: Dict[str, Any]) NoneΒΆ

Set node attributes

>>> from bigtree.node.node import Node
>>> a = Node('a')
>>> a.set_attrs({"age": 90})
>>> a
Node(/a, age=90)
Parameters:

attrs (Dict[str, Any]) – attribute dictionary, key: attribute name, value: attribute value

property siblings: Iterable[T]ΒΆ

Get siblings of self

Returns:

(Iterable[Self])

sort(**kwargs: Any) NoneΒΆ

Sort children, possible keyword arguments include key=lambda node: node.name, reverse=True

>>> from bigtree import Node, print_tree
>>> a = Node('a')
>>> c = Node("c", parent=a)
>>> b = Node("b", parent=a)
>>> print_tree(a)
a
β”œβ”€β”€ c
└── b
>>> a.sort(key=lambda node: node.name)
>>> print_tree(a)
a
β”œβ”€β”€ b
└── c