Skip to main content

treenode-py (treenode) is a Python library that provides functionality to create and manipulate tree structures.

Project description

TreeNode

TreeNode is a Python library that provides functionality to create and manipulate tree structures.


Installation

You can install TreeNode using pip:

pip install treenode-py

Usage

To use TreeNode, start by importing the TreeNode class from the treenode module:

from treenode import TreeNode

Tree

You can create a tree by initializing a TreeNode object with the root node's name:

tree = TreeNode("name of the root node")

Child Nodes

To add child nodes to the tree, use the add_child() method:

child1 = tree.add_child("name of child node 1")

You can use the slash=False to remove a slash after node:

child2 = tree.add_child("name of child node 2", slash=False)

You can also add subchild nodes to existing child nodes:

subchild1 = child1.add_child("Subchild 1", slash=False)
subchild2 = child1.add_child("Subchild 2", slash=False)

Printing the Tree and Nodes

To print the tree, simply use the print() function:

print(tree)

This will be our full code:

from treenode import TreeNode

tree = TreeNode("name of tree")
child1 = tree.add_child("name of child node 1")
child2 = tree.add_child("name of child node 2", slash=False)
subchild1 = child1.add_child("Subchild 1", slash=False)
subchild2 = child1.add_child("Subchild 2", slash=False)
print(tree)

This will output(check github if you on pypi):

Root/
в”њв”Ђв”Ђ Child 1/
в”‚   в”њв”Ђв”Ђ Subchild 1
в”‚   в””в”Ђв”Ђ Subchild 2
в””в”Ђв”Ђ Child 2

You can retrieve various information about the tree, such as the depth, number of files, number of folders, list of files, and list of folders.

Documentation

Class Methods

__init__(self, name, slash=True)

Initializes a tree node.

  • name (str): The name of the node.
  • slash (bool, optional): Indicates whether the node represents a directory path with /. Defaults to True.

Usage:

from treenode import TreeNode

tree1 = TreeNode("name of empty tree with directory slash")
tree2 = TreeNode("name of empty tree without directory slash", slash=False)
print(tree1)
print(tree2)

add_child(self, name, slash=True)

Creates a child node with the given name and adds it to the current node.

  • name (str): The name of the child node.
  • slash (bool, optional): Indicates whether the node represents a directory path with /. Defaults to True.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
folderchild = tree.add_child("name of child node 1")
filechild = tree.add_child("name of child file", slash=False)
filechild_of_folderchild = folderchild.add_child("name of subchild file", slash=False)
print(tree)

generate_treepath(self, path)

Generates a tree structure for the given directory path.

  • path (str): The directory path.

Returns:

  • TreeNode: The root node of the generated tree structure.

Usage:

from treenode import TreeNode
import os

path = input("Enter the path to the folder: ")
tree = TreeNode(os.path.basename(path)).generate_treepath(path)
print(tree)

find_node(self, name)

Finds a node with the given name.

  • name (str): The name of the node to find.

Returns:

  • TreeNode or None: The node if found, otherwise None.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
file = folder.add_child("name of file", slash=False)
node_name = "name of folder"
found_node = tree.find_node(node_name)
print(tree)
print(f"Node '{node_name}' found: \n{found_node}")

is_empty(self)

Checks if the node is empty (has no children).

Returns:

  • bool: True if the node is empty, otherwise False.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
empty = tree.is_empty()
print(tree)
print(f"Tree is empty?: {empty}")

get_depth(self)

Calculates the depth of the tree.

Returns:

  • int: The depth of the tree.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
file = folder.add_child("name of file", slash=False)
depth = tree.get_depth()
print(tree)
print(f"Depth: {depth}")

get_files(self)

Retrieves a list of all files in the tree.

Returns:

  • list: A list of file names.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
file = tree.add_child("name of file", slash=False)
getted = tree.get_folders()
print(tree)
print(f"Files: {getted}")

get_folders(self)

Retrieves a list of all folders in the tree.

Returns:

  • list: A list of folder names.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
getted = tree.get_folders()
print(tree)
print(f"Folders: {getted}")

count_files(self)

Counts the total number of files in the tree.

Returns:

  • int: The total number of files.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
file = tree.add_child("name of file", slash=False)
counted = tree.count_files()
print(tree)
print(f"Files: {counted}")

count_folders(self)

Counts the total number of folders in the tree.

Returns:

  • int: The total number of folders.

Usage:

from treenode import TreeNode

tree = TreeNode("name of tree")
folder = tree.add_child("name of folder")
counted = tree.count_folders()
print(tree)
print(f"Folders: {counted}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treenode-py-1.3.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

treenode_py-1.3.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file treenode-py-1.3.0.tar.gz.

File metadata

  • Download URL: treenode-py-1.3.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for treenode-py-1.3.0.tar.gz
Algorithm Hash digest
SHA256 570d6a692acac3cf881c4e75fcfc2d70b177930c41e0a194bdd0f33ba8574aa4
MD5 69ecb92f5562a7ecd0a04311ef9b62bd
BLAKE2b-256 a2865b7b5dffed159af8e0d4b54ee9cc04a0a475a459ee11693dbcf6c5f1606d

See more details on using hashes here.

File details

Details for the file treenode_py-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: treenode_py-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for treenode_py-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0c58a2f0df512ffcdf651f62886a9ce84ed32cc7e90167f38db9092d5f47c2a
MD5 a24af4be81cf131a31ecfe50edaad6ce
BLAKE2b-256 e580c25e5262cb6b29b05eb12239309e6a264f68a8050bcded3e6cf43ee3a43d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page