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.2.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.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydtree-1.0.2.tar.gz
Algorithm Hash digest
SHA256 c085948bea92745f97d97337eea616b2a3d7315d52e5e0a1a3f62ef703a9827c
MD5 5647e9021bc58b27da17eb73e924e54a
BLAKE2b-256 c4ed246fe93ca5f00d088b1d7c7295e73c63d308c088373f200e91bef9fcee89

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pydtree-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 add75bc778671ab215ddbde1520a2467084023a004f78fe06e5d709b9592c581
MD5 4f70a46607bf6f24d74535f10d770381
BLAKE2b-256 d170fca907b586c5809e9b46a0eeef3d8724d33a1694b2a284faf17574856fb3

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