tree-parsing


Nametree-parsing JSON
Version 0.1.1 PyPI version JSON
download
home_pagehttps://github.com/scjorge/tree-parsing
Summarylibrary to work with tree and lists
upload_time2023-05-22 01:54:33
maintainerJorge Silva
docs_urlNone
authorJorge Silva
requires_python>=3.8,<4.0
licenseMIT
keywords node tree json json-schema hints parsin parsing workflow
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Tree Parsing

[![CI](https://github.com/scjorge/tree-parsing/workflows/CI/badge.svg?event=push)](https://github.com/scjorge/tree-parsing/actions)
[![codecov](https://codecov.io/gh/scjorge/tree-parsing/branch/master/graph/badge.svg?token=0HF8XRJDV1)](https://codecov.io/gh/scjorge/tree-parsing)
[![pypi](https://img.shields.io/pypi/v/tree-parsing)](https://pypi.org/project/tree-parsing/)
[![pypi](https://img.shields.io/pypi/pyversions/tree-parsing)](https://pypi.org/project/tree-parsing/)
[![license](https://img.shields.io/pypi/l/tree-parsing)](https://github.com/scjorge/tree-parsing/blob/master/LICENSE)


<p align="center">
    <img src="https://raw.githubusercontent.com/scjorge/tree-parsing/master/docs/assets/logo.png" width='200'/>
</p>

---

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 

```{.py3 linenums=1}
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

```{.py3 linenums=1}
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

```{.py3 linenums=1}
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"
    }
]
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/scjorge/tree-parsing",
    "name": "tree-parsing",
    "maintainer": "Jorge Silva",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "jorgesilva.ti@hotmail.com",
    "keywords": "node,tree,json,json-schema,hints,parsin,parsing,workflow",
    "author": "Jorge Silva",
    "author_email": "jorgesilva.ti@hotmail.com",
    "download_url": "https://files.pythonhosted.org/packages/bd/f0/92adc92bea5bc9633d27bf9778e88d5f7dd076cf3a432e69ce0cdc1d9407/tree_parsing-0.1.1.tar.gz",
    "platform": null,
    "description": "# Tree Parsing\n\n[![CI](https://github.com/scjorge/tree-parsing/workflows/CI/badge.svg?event=push)](https://github.com/scjorge/tree-parsing/actions)\n[![codecov](https://codecov.io/gh/scjorge/tree-parsing/branch/master/graph/badge.svg?token=0HF8XRJDV1)](https://codecov.io/gh/scjorge/tree-parsing)\n[![pypi](https://img.shields.io/pypi/v/tree-parsing)](https://pypi.org/project/tree-parsing/)\n[![pypi](https://img.shields.io/pypi/pyversions/tree-parsing)](https://pypi.org/project/tree-parsing/)\n[![license](https://img.shields.io/pypi/l/tree-parsing)](https://github.com/scjorge/tree-parsing/blob/master/LICENSE)\n\n\n<p align=\"center\">\n    <img src=\"https://raw.githubusercontent.com/scjorge/tree-parsing/master/docs/assets/logo.png\" width='200'/>\n</p>\n\n---\n\nDocumentation: https://tree-parsing.readthedocs.io/en/latest/\n\nSource Code: https://github.com/scjorge/tree-parsing\n\n---\n\n\nThis library lets you work with trees and lists.\n\nSo you can:\n\n- Make a tree when you have all nodes in the list\n- Convert the Tree to lists of nodes\n- Customize how to generate 'flow key', 'children key'\n- Do something for each node\n\n\n## Install\nInstallation is as simple:\n\n### With pip\n\n```\npip install tree-parsing\n```\n\n### With Poetry\n\n```\npoetry add tree-parsing\n```\n\n## Exemplos\n\n### Tree From List \n\n```{.py3 linenums=1}\nimport json\n\nfrom tree_parsing import Tree\n\n\nlist_tree = [\n    {\"id\": 1, \"parent\": 0},\n    {\"id\": 2, \"parent\": 1},\n    {\"id\": 3, \"parent\": 1},\n    {\"id\": 4, \"parent\": 2},\n    {\"id\": 5, \"parent\": 0},\n]\n\ntr = Tree(id_key=\"id\", parent_key=\"parent\", parent_start=\"0\", child_key=\"children\")\ntree = tr.tree_from_list(list_tree)\nprint(json.dumps(tree, indent=4))\n```\n\nOutput:\n\n```\n[\n    {\n        \"id\": 1,\n        \"parent\": 0,\n        \"flow\": \"1\",\n        \"children\": [\n            {\n                \"id\": 2,\n                \"parent\": 1,\n                \"flow\": \"1-1\",\n                \"children\": [\n                    {\n                        \"id\": 4,\n                        \"parent\": 2,\n                        \"flow\": \"1-1-1\"\n                    }\n                ]\n            },\n            {\n                \"id\": 3,\n                \"parent\": 1,\n                \"flow\": \"1-2\"\n            }\n        ]\n    },\n    {\n        \"id\": 5,\n        \"parent\": 0,\n        \"flow\": \"2\"\n    }\n]\n```\n\n### List From Tree\n\n```{.py3 linenums=1}\nimport json\n\nfrom tree_parsing import Tree\n\n\nmy_tree = [\n    {\n        \"id\": 1,\n        \"parent\": 0,\n        \"flow\": \"1\",\n        \"children\": [\n            {\n                \"id\": 2,\n                \"parent\": 1,\n                \"flow\": \"1-1\",\n                \"children\": [\n                    {\n                        \"id\": 4,\n                        \"parent\": 2,\n                        \"flow\": \"1-1-1\"\n                    }\n                ]\n            },\n            {\n                \"id\": 3,\n                \"parent\": 1,\n                \"flow\": \"1-2\"\n            }\n        ]\n    },\n    {\n        \"id\": 5,\n        \"parent\": 0,\n        \"flow\": \"2\"\n    }\n]\n\ntr = Tree(id_key=\"id\", parent_key=\"parent\", child_key=\"children\")\nlist_tree = tr.list_from_tree(my_tree)\nprint(json.dumps(list_tree, indent=1))\n```\n\nOutput:\n\n```\n[\n {\n  \"id\": 1,\n  \"parent\": 0,\n  \"flow\": \"1\"\n },\n {\n  \"id\": 2,\n  \"parent\": 1,\n  \"flow\": \"1-1\"\n },\n {\n  \"id\": 4,\n  \"parent\": 2,\n  \"flow\": \"1-1-1\"\n },\n {\n  \"id\": 3,\n  \"parent\": 1,\n  \"flow\": \"1-2\"\n },\n {\n  \"id\": 5,\n  \"parent\": 0,\n  \"flow\": \"2\"\n }\n]\n```\n\n### Do something on the node\n\n```{.py3 linenums=1}\nimport json\nfrom typing import Dict\n\nfrom tree_parsing import Tree\n\n\nlist_tree = [\n    {\"id\": 1, \"parent\": 0},\n    {\"id\": 2, \"parent\": 1},\n    {\"id\": 3, \"parent\": 1},\n    {\"id\": 4, \"parent\": 2},\n    {\"id\": 5, \"parent\": 0},\n]\n\n\nclass MyTree(Tree):\n    def new_node(self, node: Dict) -> None:\n        if node['id'] == 2:\n            node['new_key'] = 'new value'\n        \n\ntr = MyTree(id_key=\"id\", parent_key=\"parent\", parent_start=\"0\", child_key=\"children\")\ntree = tr.tree_from_list(list_tree)\nprint(json.dumps(tree, indent=4))\n```\n\nOutput:\n\n```\n[\n    {\n        \"id\": 1,\n        \"parent\": 0,\n        \"flow\": \"1\",\n        \"children\": [\n            {\n                \"id\": 2,\n                \"parent\": 1,\n                \"new_key\": \"new value\",\n                \"flow\": \"1-1\",\n                \"children\": [\n                    {\n                        \"id\": 4,\n                        \"parent\": 2,\n                        \"flow\": \"1-1-1\"\n                    }\n                ]\n            },\n            {\n                \"id\": 3,\n                \"parent\": 1,\n                \"flow\": \"1-2\"\n            }\n        ]\n    },\n    {\n        \"id\": 5,\n        \"parent\": 0,\n        \"flow\": \"2\"\n    }\n]\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "library to work with tree and lists",
    "version": "0.1.1",
    "project_urls": {
        "Documentation": "https://tree-parsing.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/scjorge/tree-parsing",
        "Repository": "https://github.com/scjorge/tree-parsing"
    },
    "split_keywords": [
        "node",
        "tree",
        "json",
        "json-schema",
        "hints",
        "parsin",
        "parsing",
        "workflow"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "215daa7325aea49b447719c4977808626949f0b2099358d05ebc9f4a4b30f3b8",
                "md5": "c868560e2baea4e790d745a449b5c63b",
                "sha256": "180dbd83700f757bd52399d39d1997806f7a01e4195614224ff79172ecc7470b"
            },
            "downloads": -1,
            "filename": "tree_parsing-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c868560e2baea4e790d745a449b5c63b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 4144,
            "upload_time": "2023-05-22T01:54:31",
            "upload_time_iso_8601": "2023-05-22T01:54:31.950429Z",
            "url": "https://files.pythonhosted.org/packages/21/5d/aa7325aea49b447719c4977808626949f0b2099358d05ebc9f4a4b30f3b8/tree_parsing-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bdf092adc92bea5bc9633d27bf9778e88d5f7dd076cf3a432e69ce0cdc1d9407",
                "md5": "87176beaafc6a24b689dcfc05f723af7",
                "sha256": "212c543354b206df9f2db3a9a859c07cca156437e8856d4083cf3fd66634b78d"
            },
            "downloads": -1,
            "filename": "tree_parsing-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "87176beaafc6a24b689dcfc05f723af7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 4111,
            "upload_time": "2023-05-22T01:54:33",
            "upload_time_iso_8601": "2023-05-22T01:54:33.670107Z",
            "url": "https://files.pythonhosted.org/packages/bd/f0/92adc92bea5bc9633d27bf9778e88d5f7dd076cf3a432e69ce0cdc1d9407/tree_parsing-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-22 01:54:33",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "scjorge",
    "github_project": "tree-parsing",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tree-parsing"
}
        
Elapsed time: 0.96777s