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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
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"
}
]