library to work with tree and lists
Project description
Tree Parsing
Documentation: https://tree-parsing.readthedocs.io/en/latest/
Source Code: https://github.com/scjorge/tree-parsing
This library lets you work with trees and lists.
So you can:
- Make a tree when you have all nodes in the list
- Convert the Tree to lists of nodes
- Customize how to generate 'flow key', 'children key'
- Do something for each node
Install
Installation is as simple:
With pip
pip install tree-parsing
With Poetry
poetry add tree-parsing
Exemplos
Tree From List
import json
from tree_parsing import Tree
list_tree = [
{"id": 1, "parent": 0},
{"id": 2, "parent": 1},
{"id": 3, "parent": 1},
{"id": 4, "parent": 2},
{"id": 5, "parent": 0},
]
tr = Tree(id_key="id", parent_key="parent", parent_start="0", child_key="children")
tree = tr.tree_from_list(list_tree)
print(json.dumps(tree, indent=4))
Output:
[
{
"id": 1,
"parent": 0,
"flow": "1",
"children": [
{
"id": 2,
"parent": 1,
"flow": "1-1",
"children": [
{
"id": 4,
"parent": 2,
"flow": "1-1-1"
}
]
},
{
"id": 3,
"parent": 1,
"flow": "1-2"
}
]
},
{
"id": 5,
"parent": 0,
"flow": "2"
}
]
List From Tree
import json
from tree_parsing import Tree
my_tree = [
{
"id": 1,
"parent": 0,
"flow": "1",
"children": [
{
"id": 2,
"parent": 1,
"flow": "1-1",
"children": [
{
"id": 4,
"parent": 2,
"flow": "1-1-1"
}
]
},
{
"id": 3,
"parent": 1,
"flow": "1-2"
}
]
},
{
"id": 5,
"parent": 0,
"flow": "2"
}
]
tr = Tree(id_key="id", parent_key="parent", child_key="children")
list_tree = tr.list_from_tree(my_tree)
print(json.dumps(list_tree, indent=1))
Output:
[
{
"id": 1,
"parent": 0,
"flow": "1"
},
{
"id": 2,
"parent": 1,
"flow": "1-1"
},
{
"id": 4,
"parent": 2,
"flow": "1-1-1"
},
{
"id": 3,
"parent": 1,
"flow": "1-2"
},
{
"id": 5,
"parent": 0,
"flow": "2"
}
]
Do something on the node
import json
from typing import Dict
from tree_parsing import Tree
list_tree = [
{"id": 1, "parent": 0},
{"id": 2, "parent": 1},
{"id": 3, "parent": 1},
{"id": 4, "parent": 2},
{"id": 5, "parent": 0},
]
class MyTree(Tree):
def new_node(self, node: Dict) -> None:
if node['id'] == 2:
node['new_key'] = 'new value'
tr = MyTree(id_key="id", parent_key="parent", parent_start="0", child_key="children")
tree = tr.tree_from_list(list_tree)
print(json.dumps(tree, indent=4))
Output:
[
{
"id": 1,
"parent": 0,
"flow": "1",
"children": [
{
"id": 2,
"parent": 1,
"new_key": "new value",
"flow": "1-1",
"children": [
{
"id": 4,
"parent": 2,
"flow": "1-1-1"
}
]
},
{
"id": 3,
"parent": 1,
"flow": "1-2"
}
]
},
{
"id": 5,
"parent": 0,
"flow": "2"
}
]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tree_parsing-0.1.1.tar.gz
(4.1 kB
view details)
Built Distribution
File details
Details for the file tree_parsing-0.1.1.tar.gz
.
File metadata
- Download URL: tree_parsing-0.1.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.10.1 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 212c543354b206df9f2db3a9a859c07cca156437e8856d4083cf3fd66634b78d |
|
MD5 | 87176beaafc6a24b689dcfc05f723af7 |
|
BLAKE2b-256 | bdf092adc92bea5bc9633d27bf9778e88d5f7dd076cf3a432e69ce0cdc1d9407 |
File details
Details for the file tree_parsing-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: tree_parsing-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.0 CPython/3.10.1 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 180dbd83700f757bd52399d39d1997806f7a01e4195614224ff79172ecc7470b |
|
MD5 | c868560e2baea4e790d745a449b5c63b |
|
BLAKE2b-256 | 215daa7325aea49b447719c4977808626949f0b2099358d05ebc9f4a4b30f3b8 |