SuperNode#

class supernodes.nodes.SuperNode(name: Optional[Union[str, int, Hashable]] = None, value: Optional[Any] = None, id: Optional[Any] = None, children: Optional[list] = None, function: Optional[Callable] = None, child_name_if_true: Optional[Union[str, int, Hashable]] = None, child_name_if_false: Optional[Union[str, int, Hashable]] = None, **other_attrs)[source]#

Bases: object

A node used to create a tree data structure.

name#

The name of the node.

Type:

str, int, hashable, default = None

value#

The data stored in a node.

Type:

Any, default = None

id#

An attribute that should be unique for every node. None of the nodes in the whole tree should to have the same id attribute.

Type:

Any, default = None

other_attrs#

Here you can store additional attributes.

Type:

dict

children#

A list of children nodes.

Type:

list

function#

A function to be called when running the tree as a decision tree. This attribute can also an inequality as a string. For example: “x >= 10” or “x[1] < 9”. Slicing is currently not supported.

Type:

Callable, str

child_name_if_true#

If this node was run in a binary tree, this attribute will determine which child to be chosen next. If the return value of the function is True, the child with the name that is same as child_name_if_true will be chosen. See run_as_binary_tree() for more information.

Type:

str, int, hashable

child_name_if_false#

Specified the child to be chosen if function returns False. See run_as_binary_tree() for more information.

Type:

str, int, hashable

Examples

Importing this class:

>>> from supernodes import SuperNode

Making a simple node:

>>> node = SuperNode()

Making a node with name, value, and id:

>>> node = SuperNode(name="main node", value=30, id="node-0")
>>> node
(name=main node, value: int, id=node-0)

Changing the name of the node:

>>> node.name = "new name"

Adding children:

>>> child_1 = SuperNode(name="child-1")
>>> child_2 = SuperNode(name="child-2")
>>> node.append(child_1)
>>> node.append(child_2)
>>> node
(name=new name, value: int, id=node-0)
|__ (name=child-1)
|__ (name=child-2)

Methods#