Skip to main content

Python Generate the data tree recursively

Project description

Tree Builder

Input

 lst = [
     {"id": 1, "pid": 0, "name": "Tree - 1"},
     {"id": 2, "pid": 1, "name": "Tree - 1 - 1"},
     {"id": 3, "pid": 1, "name": "Tree - 1 - 2"},
     {"id": 4, "pid": 2, "name": "Tree - 1 - 1- 1"},
     {"id": 5, "pid": 0, "name": "Tree - 2"}
 ]

Usage

pip install data2tree -i  https://pypi.org/simple/
from TreeBuilder.builder import BuildTree
tree = BuildTree(record_key="id", parent_record_key="pid").build_tree(lst)
print(tree)

Notes

Parameter Type required Description
record_key String True record id
parent_record_key String True parent_record id

Output

[
    {
        "id": 1,
        "pid": 0,
        "name": "Tree - 1",
        "child": [
            {
                "id": 2,
                "pid": 1,
                "name": "Tree - 1 - 1",
                "child": [
                    {
                        "id": 4,
                        "pid": 2,
                        "name": "Tree - 1 - 1- 1"
                    }]
            },
            {
                "id": 3,
                "pid": 1,
                "name": "Tree - 1 - 2"
            }
        ]
    },
    {
        "id": 5,
        "pid": 0,
        "name": "Tree - 2"
    }
]

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

data2tree-1.0.2.tar.gz (2.0 kB view hashes)

Uploaded Source

Built Distribution

data2tree-1.0.2-py3-none-any.whl (2.6 kB view hashes)

Uploaded Python 3

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