Install chesnut using pip.
pip install chesnutThen you can use the Node class in your project.
from chesnut import NodeYou can subclass the Node class to create your custom nodes and add your own attributes and methods.
class CustomNode(Node):
def __init__(self, data, parent=None):
super().__init__(parent=parent)
self.data = data# Creating nodes
root_node = CustomNode(data="Root")
child_node1 = CustomNode(data="Child 1", parent=root_node)
child_node2 = CustomNode(data="Child 2", parent=root_node)
grandchild_node = CustomNode(data="Grandchild", parent=child_node1)For a complete list of methods, see the documentation.
# Finding the root
root = child_node1.root
# Checking if a node is the root
is_root = root_node.is_root # True# Querying children by type
children_of_type = root_node.children_by_type(CustomNode) # List of CustomNode instances
# Querying descendants by type
descendants_of_type = root_node.descendants_by_type(CustomNode) # List of CustomNode instances# Checking if a node has children of a specific type
has_children = root_node.has_children_by_type(CustomNode) # True or False
# Checking if a node has descendants of a specific type
has_descendants = root_node.has_descendants_by_type(CustomNode) # True or FalseBy using the Node class as a base for your custom nodes, you can take advantage of its methods to easily navigate and manipulate your tree structure. This inheritance approach allows you to focus on the specific functionality of your custom nodes while benefiting from the tree-related operations provided by chesnut.
You can run the tests with:
make testCoverage can be checked at function level with:
make coverageLinting can be performed with:
make lintDocumentation is available here. It is generated from the docstrings using pdoc. You can generate this yourself using:
make docsThis will generate the documentation in the docs folder, open docs/index.html in your browser to see the documentation.
This project is licensed under the MIT License - see the LICENSE file for details.