Skip to main content

Python library for building and traversing trees

Project description

DTree: Python Library for Building and Traversing Trees

DTree is a Python library for building and traversing trees. It provides a simple and efficient way to create and manipulate tree data structures.

Features

  • Create trees from dictionaries, lists, and tuples
  • Traverse trees using generators
  • Support for different tree themes (ASCII, Unicode)
  • Customizable tree rendering and themes

Installation

To install DTree, run the following command:

pip install pydtree

Usage

Here is an example of how to create and traverse a tree using DTree:

from dtree import tree

# Create a tree from a dictionary
data = {
    'root': {
        'child1': 'value1',
        'child2': 'value2',
        'child3': {
            'grandchild1': 'value3',
            'grandchild2': 'value4'
        }
    }
}

# Traverse the tree using a generator
for line in tree('my_tree', data):
    print(line)

This will output the following tree structure:

my_tree:
`-- root
    +-- child1: value1
    +-- child2: value2
    `-- child3
        +-- grandchild1: value3
        `-- grandchild2: value4

Themes

DTree supports different tree themes, including ASCII and Unicode. You can customize the theme by passing a theme parameter to the tree function:

from dtree import tree, themes

for line in tree(
        'my_tree',
        data,
        theme=themes.Unicode
):
    print(line)

This will output the following tree structure using Unicode characters:

my_tree:
└── root
    ├── child1: value1
    ├── child2: value2
    └── child3
        ├── grandchild1: value3
        └── grandchild2: value4

Custom Themes

You can also create your own custom themes by defining a Theme object. Here is an example:

from dtree.types import Theme
from dtree import tree

MyTheme = Theme(
    vertical='|   ',
    branch='+-- ',
    corner='`-- ',
    tab='    '
)

data = {...}

for line in tree(
        'my_tree',
        data,
        theme=MyTheme
):
    print(line)

This will output the tree structure using your custom theme.

Customization

You can customize the tree rendering by passing a render function to the tree function:

from dtree import tree

def custom_render(key, value):
    return f"{key} ({value})"

for line in tree(
        'my_tree',
        data,
        render=custom_render
):
    print(line)

This will output the following tree structure with custom rendering:

my_tree:
+-- child1 (value1)
+-- child2 (value2)
`-- child3 (value3)

Contributing

If you'd like to contribute to DTree, please fork the repository and submit a pull request.

License

DTree is licensed under the MIT License.

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

pydtree-1.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydtree-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pydtree-1.0.1.tar.gz.

File metadata

  • Download URL: pydtree-1.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for pydtree-1.0.1.tar.gz
Algorithm Hash digest
SHA256 e8f3fb17016e420a97392c81b1b331c123e3fddecebf6774bd2460644fc7c949
MD5 ab3fc1e6817d7e271b499183f546d14d
BLAKE2b-256 f30a4318d43c39a6fbdc476d27c7810821777837e2e6cba1b4d1a403100c33fe

See more details on using hashes here.

File details

Details for the file pydtree-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pydtree-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for pydtree-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e7d2da1b55eedeec1cd3a5883bf971a81d125807cf1b3255d7cf933ae076707
MD5 822900175280e99034d9408f4f0b7b12
BLAKE2b-256 4733c852ad3392dc4231e4ab2914bec6a9a1681a8fd03ea04b8164c9cb39c2a8

See more details on using hashes here.

Supported by

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