Skip to main content

Chesnut Logo docs | pip

`Chesnut` is a lightweight tree class implementation in Python that offers a simple approach to search tree structures. By inheriting from the `Node` class, you can create your custom nodes and leverage its methods for efficient tree navigation.

Installation

Install chesnut using pip.

pip install chesnut

Then you can use the Node class in your project.

from chesnut import Node

Usage

Creating Custom Nodes

You 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

Building a Tree

# 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)

Using Node Attributes

For a complete list of methods, see the documentation.

Finding Nodes

# Finding the root
root = child_node1.root

# Checking if a node is the root
is_root = root_node.is_root  # True

Querying Nodes

# 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 Node Relationships

# 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 False

By 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.

Testing

You can run the tests with:

make test

Coverage can be checked at function level with:

make coverage

Linting can be performed with:

make lint

Documentation

Documentation is available here. It is generated from the docstrings using pdoc. You can generate this yourself using:

make docs

This will generate the documentation in the docs folder, open docs/index.html in your browser to see the documentation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for chesnut 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chesnut 1.0.1
File Size Uploaded
chesnut-1.0.1.tar.gz 6.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for chesnut 1.0.1
File Interpreter ABI Platform
chesnut-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.9 kB

Release files / chesnut-1.0.1.tar.gz

Download URL chesnut-1.0.1.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
bc8c605046fc815cc4c3760059a5f2e49ae5fc2301cc27da887189735a606b7a
BLAKE2b-256 checksum
How to use checksums
96361d107beb4ece395dadaf95cd08b26219be8535469bec294c747d4ed52b7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / chesnut-1.0.1-py3-none-any.whl

Download URL chesnut-1.0.1-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6aad7ffa4c5d463d5754f75f7142b6490e80027c78e51e81bbb61cf706b3832
BLAKE2b-256 checksum
How to use checksums
78e69f71a7ae2d9a32d592fa56d46eb5273ba8fbd85b9aaa3b7ca26bfeac44ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page