API

pynodes is a Python library to manage Nodes classes. A node is an object which is related to other nodes given a tree chart.

class pynodes.Node(name: str, parent: Node | None = None)[source]

Should be subclassed only

add_child(child: Node) None[source]

Add new child node to current instance

Parameters:

child – Node object

static check_lineage(nodes: list[Node]) bool[source]
check if the list of nodes is a straight lineage:

node 1 (ancestor) -> node 2 -> node 3 -> … -> node n (grand children)

classmethod create_random_nodes(type_: str = 'cmd', depth: int = 0) Node[source]

Creates random tree of nodes for testing purpose

get_child(name: str) Node | None[source]

find and returns a child with specified name. None if nothing found

get_sibling(name: str) Node | None[source]

find and returns a sibling with specified name. None if nothing found

has_children() bool[source]

check if Node object has one child at least

has_parent() bool[source]

check if Node object has a parent or not

has_siblings() bool[source]

check if Node object has one sibling at least

is_child(other: str) bool[source]

Check if Node object is a child of the other Node object

is_sibling(other: str) bool[source]

Check if Node object is a sibling of the other Node object

Parameters:

other – Other Node object to be compared with

property level: int

Returns the level of the Node object

property parents: list[Node]

Returns all the ancestors of the Node object

property path: str

Returns a representation of the ancestor lineage of self

pretty_print(option: str = 'default') None[source]

Print children tree from current instance

classmethod reset_stats() None[source]

reset all the ClassVar members

property siblings: list[Node]

Returns all the siblings of the Node object