treenode-py


Nametreenode-py JSON
Version 1.3.0 PyPI version JSON
download
home_pagehttps://github.com/PivoSteve/treenode-py
Summarytreenode-py (treenode) is a Python library that provides functionality to create and manipulate tree structures.
upload_time2024-05-05 08:08:26
maintainerNone
docs_urlNone
authorSyra
requires_pythonNone
licenseMIT
keywords tree directory file system node structures structure
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # [TreeNode](https://pypi.org/project/treenode-py/)

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:
```python
from treenode import TreeNode
```
### Tree
You can create a tree by initializing a TreeNode object with the root node's name:
```python
tree = TreeNode("name of the root node")
```

### Child Nodes
To add child nodes to the tree, use the `add_child()` method:
```python
child1 = tree.add_child("name of child node 1")
```
You can use the `slash=False` to remove a slash after node:
```python
child2 = tree.add_child("name of child node 2", slash=False)
```

You can also add subchild nodes to existing child nodes:
```python
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:
```python
print(tree)
```
This will be our full code:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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:
```python
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}")
``` 

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PivoSteve/treenode-py",
    "name": "treenode-py",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "tree directory file system node structures structure",
    "author": "Syra",
    "author_email": "horriblebuba@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a2/86/5b7b5dffed159af8e0d4b54ee9cc04a0a475a459ee11693dbcf6c5f1606d/treenode-py-1.3.0.tar.gz",
    "platform": null,
    "description": "# [TreeNode](https://pypi.org/project/treenode-py/)\r\n\r\nTreeNode is a Python library that provides functionality to create and manipulate tree structures.\r\n\r\n------\r\n\r\n## Installation\r\nYou can install TreeNode using pip:\r\n```\r\npip install treenode-py\r\n```\r\n\r\n## Usage\r\nTo use TreeNode, start by importing the TreeNode class from the treenode module:\r\n```python\r\nfrom treenode import TreeNode\r\n```\r\n### Tree\r\nYou can create a tree by initializing a TreeNode object with the root node's name:\r\n```python\r\ntree = TreeNode(\"name of the root node\")\r\n```\r\n\r\n### Child Nodes\r\nTo add child nodes to the tree, use the `add_child()` method:\r\n```python\r\nchild1 = tree.add_child(\"name of child node 1\")\r\n```\r\nYou can use the `slash=False` to remove a slash after node:\r\n```python\r\nchild2 = tree.add_child(\"name of child node 2\", slash=False)\r\n```\r\n\r\nYou can also add subchild nodes to existing child nodes:\r\n```python\r\nsubchild1 = child1.add_child(\"Subchild 1\", slash=False)\r\nsubchild2 = child1.add_child(\"Subchild 2\", slash=False)\r\n```\r\n\r\n### Printing the Tree and Nodes\r\nTo print the tree, simply use the `print()` function:\r\n```python\r\nprint(tree)\r\n```\r\nThis will be our full code:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nchild1 = tree.add_child(\"name of child node 1\")\r\nchild2 = tree.add_child(\"name of child node 2\", slash=False)\r\nsubchild1 = child1.add_child(\"Subchild 1\", slash=False)\r\nsubchild2 = child1.add_child(\"Subchild 2\", slash=False)\r\nprint(tree)\r\n```\r\n\r\nThis will output(check github if you on pypi):\r\n```\r\nRoot/\r\n\u0432\u201d\u045a\u0432\u201d\u0402\u0432\u201d\u0402 Child 1/\r\n\u0432\u201d\u201a   \u0432\u201d\u045a\u0432\u201d\u0402\u0432\u201d\u0402 Subchild 1\r\n\u0432\u201d\u201a   \u0432\u201d\u201d\u0432\u201d\u0402\u0432\u201d\u0402 Subchild 2\r\n\u0432\u201d\u201d\u0432\u201d\u0402\u0432\u201d\u0402 Child 2\r\n```\r\n\r\nYou can retrieve various information about the tree, such as the depth, number of files, number of folders, list of files, and list of folders.\r\n\r\n## Documentation\r\n\r\n### Class Methods\r\n\r\n#### `__init__(self, name, slash=True)`\r\n\r\nInitializes a tree node.\r\n\r\n- `name (str)`: The name of the node.\r\n- `slash (bool, optional)`: Indicates whether the node represents a directory path with `/`. Defaults to `True`.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree1 = TreeNode(\"name of empty tree with directory slash\")\r\ntree2 = TreeNode(\"name of empty tree without directory slash\", slash=False)\r\nprint(tree1)\r\nprint(tree2)\r\n```\r\n\r\n#### `add_child(self, name, slash=True)`\r\n\r\nCreates a child node with the given name and adds it to the current node.\r\n\r\n- `name (str)`: The name of the child node.\r\n- `slash (bool, optional)`: Indicates whether the node represents a directory path with `/`. Defaults to `True`.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfolderchild = tree.add_child(\"name of child node 1\")\r\nfilechild = tree.add_child(\"name of child file\", slash=False)\r\nfilechild_of_folderchild = folderchild.add_child(\"name of subchild file\", slash=False)\r\nprint(tree)\r\n```\r\n\r\n#### `generate_treepath(self, path)`\r\n\r\nGenerates a tree structure for the given directory path.\r\n\r\n- `path (str)`: The directory path.\r\n\r\nReturns:\r\n- `TreeNode`: The root node of the generated tree structure.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\nimport os\r\n\r\npath = input(\"Enter the path to the folder: \")\r\ntree = TreeNode(os.path.basename(path)).generate_treepath(path)\r\nprint(tree)\r\n```\r\n\r\n#### `find_node(self, name)`\r\n\r\nFinds a node with the given name.\r\n\r\n- `name (str)`: The name of the node to find.\r\n\r\nReturns:\r\n- `TreeNode or None`: The node if found, otherwise `None`.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfolder = tree.add_child(\"name of folder\")\r\nfile = folder.add_child(\"name of file\", slash=False)\r\nnode_name = \"name of folder\"\r\nfound_node = tree.find_node(node_name)\r\nprint(tree)\r\nprint(f\"Node '{node_name}' found: \\n{found_node}\")\r\n```\r\n\r\n#### `is_empty(self)`\r\n\r\nChecks if the node is empty (has no children).\r\n\r\nReturns:\r\n- `bool`: `True` if the node is empty, otherwise `False`.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nempty = tree.is_empty()\r\nprint(tree)\r\nprint(f\"Tree is empty?: {empty}\")\r\n```\r\n\r\n#### `get_depth(self)`\r\n\r\nCalculates the depth of the tree.\r\n\r\nReturns:\r\n- `int`: The depth of the tree.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfolder = tree.add_child(\"name of folder\")\r\nfile = folder.add_child(\"name of file\", slash=False)\r\ndepth = tree.get_depth()\r\nprint(tree)\r\nprint(f\"Depth: {depth}\")\r\n```\r\n\r\n#### `get_files(self)`\r\n\r\nRetrieves a list of all files in the tree.\r\n\r\nReturns:\r\n- `list`: A list of file names.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfile = tree.add_child(\"name of file\", slash=False)\r\ngetted = tree.get_folders()\r\nprint(tree)\r\nprint(f\"Files: {getted}\")\r\n``` \r\n\r\n#### `get_folders(self)`\r\n\r\nRetrieves a list of all folders in the tree.\r\n\r\nReturns:\r\n- `list`: A list of folder names.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfolder = tree.add_child(\"name of folder\")\r\ngetted = tree.get_folders()\r\nprint(tree)\r\nprint(f\"Folders: {getted}\")\r\n``` \r\n\r\n#### `count_files(self)`\r\n\r\nCounts the total number of files in the tree.\r\n\r\nReturns:\r\n- `int`: The total number of files.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfile = tree.add_child(\"name of file\", slash=False)\r\ncounted = tree.count_files()\r\nprint(tree)\r\nprint(f\"Files: {counted}\")\r\n``` \r\n\r\n#### `count_folders(self)`\r\n\r\nCounts the total number of folders in the tree.\r\n\r\nReturns:\r\n- `int`: The total number of folders.\r\n\r\nUsage:\r\n```python\r\nfrom treenode import TreeNode\r\n\r\ntree = TreeNode(\"name of tree\")\r\nfolder = tree.add_child(\"name of folder\")\r\ncounted = tree.count_folders()\r\nprint(tree)\r\nprint(f\"Folders: {counted}\")\r\n``` \r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "treenode-py (treenode) is a Python library that provides functionality to create and manipulate tree structures.",
    "version": "1.3.0",
    "project_urls": {
        "Homepage": "https://github.com/PivoSteve/treenode-py",
        "Source": "https://github.com/PivoSteve/treenode-py"
    },
    "split_keywords": [
        "tree",
        "directory",
        "file",
        "system",
        "node",
        "structures",
        "structure"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e580c25e5262cb6b29b05eb12239309e6a264f68a8050bcded3e6cf43ee3a43d",
                "md5": "a24af4be81cf131a31ecfe50edaad6ce",
                "sha256": "e0c58a2f0df512ffcdf651f62886a9ce84ed32cc7e90167f38db9092d5f47c2a"
            },
            "downloads": -1,
            "filename": "treenode_py-1.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a24af4be81cf131a31ecfe50edaad6ce",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5073,
            "upload_time": "2024-05-05T08:08:25",
            "upload_time_iso_8601": "2024-05-05T08:08:25.180609Z",
            "url": "https://files.pythonhosted.org/packages/e5/80/c25e5262cb6b29b05eb12239309e6a264f68a8050bcded3e6cf43ee3a43d/treenode_py-1.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a2865b7b5dffed159af8e0d4b54ee9cc04a0a475a459ee11693dbcf6c5f1606d",
                "md5": "69ecb92f5562a7ecd0a04311ef9b62bd",
                "sha256": "570d6a692acac3cf881c4e75fcfc2d70b177930c41e0a194bdd0f33ba8574aa4"
            },
            "downloads": -1,
            "filename": "treenode-py-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "69ecb92f5562a7ecd0a04311ef9b62bd",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4822,
            "upload_time": "2024-05-05T08:08:26",
            "upload_time_iso_8601": "2024-05-05T08:08:26.761956Z",
            "url": "https://files.pythonhosted.org/packages/a2/86/5b7b5dffed159af8e0d4b54ee9cc04a0a475a459ee11693dbcf6c5f1606d/treenode-py-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-05 08:08:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PivoSteve",
    "github_project": "treenode-py",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "treenode-py"
}
        
Elapsed time: 0.64051s