{"id":19451709,"url":"https://github.com/zhengxs2018/tiny-tree","last_synced_at":"2026-06-20T14:02:30.276Z","repository":{"id":57475796,"uuid":"384907956","full_name":"zhengxs2018/tiny-tree","owner":"zhengxs2018","description":"一个循环解决行转树的问题，快速，轻量，无依赖。","archived":false,"fork":false,"pushed_at":"2022-09-14T01:22:05.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-04-03T10:46:44.378Z","etag":null,"topics":["array-to-tree","array2tree","data-structures","tree","tree2array"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhengxs2018.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-07-11T09:31:50.000Z","updated_at":"2022-09-14T01:22:10.000Z","dependencies_parsed_at":"2022-09-07T13:51:33.557Z","dependency_job_id":null,"html_url":"https://github.com/zhengxs2018/tiny-tree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/zhengxs2018/tiny-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhengxs2018%2Ftiny-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhengxs2018%2Ftiny-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhengxs2018%2Ftiny-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhengxs2018%2Ftiny-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhengxs2018","download_url":"https://codeload.github.com/zhengxs2018/tiny-tree/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhengxs2018%2Ftiny-tree/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34572426,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-20T02:00:06.407Z","response_time":98,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array-to-tree","array2tree","data-structures","tree","tree2array"],"created_at":"2024-11-10T16:42:44.281Z","updated_at":"2026-06-20T14:02:30.236Z","avatar_url":"https://github.com/zhengxs2018.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# tiny-tree\n\n\u003ca href=\"https://pypi.org/project/tiny-tree/\"\u003e\u003cimg src=\"https://img.shields.io/pypi/v/tiny_tree\" alt=\"Version\" /\u003e\u003c/a\u003e\n\n\u003e 快速，轻量，无依赖的树结构数据处理函数库。\n\n---\n\n- 一个循环解决行转树的问题\n- 除添加 **children** 属性外，不会修改任何数据\n- 支持任意关系字段，如：非 **id**，**parentId**, **children** 字段支持\n- 支持接管插入行为，如：自定义插入顺序\n- 支持动态导出树节点\n\n## 快速开始\n\n### 文档\n\n- [to_tree 行转树](./docs/to_tree.md)\n- [to_rows 树转行](./docs/to_tows.md)\n\n**注意**: 因为引入了 `typing` 模块，需要 `python\u003e=3.6`。\n\n### 安装\n\n```shell\n# 不支持 python2\n$ pip3 install tiny-tree\n```\n\n### 使用\n\n```python\nfrom tiny_tree.to_tree import to_tree\n\nto_tree([\n  { \"id\": 10000, \"parentId\": None, \"title\": \"标题 1\" },\n  { \"id\": 20000, \"parentId\": None, \"title\": \"标题 2\" },\n  { \"id\": 11000, \"parentId\": 10000, \"title\": \"标题 1-1\" },\n])\n# [\n#   {\n#     \"id\": 10000,\n#     \"parentId\": None,\n#     \"title\": '标题 1',\n#     \"children\": [\n#       { \"id\": 11000, \"parentId\": 10000, \"title\": '标题 1-1', \"children\": [] }\n#     ]\n#   },\n#   { \"id\": 20000, \"parentId\": None, \"title\": '标题 2', \"children\": [] },\n# ]\n```\n\n支持任意关系字段的数据\n\n```python\nfrom tiny_tree.to_tree import to_tree, ROOT_ID\nfrom tiny_tree.helpers import sort_insert, add_property\n\ndata = [\n  { \"uid\": 10000, \"pid\": None, \"title\": \"标题 1\", \"sort\": 1 },\n  { \"uid\": 20000, \"pid\": None, \"title\": \"标题 2\", \"sort\": 2 },\n  { \"uid\": 11000, \"pid\": 10000, \"title\": \"标题 1-1\", \"sort\": 3 },\n]\n\ntree = to_tree(\n  data,\n  # 如果 parentId 为 None\n  # 使用 ROOT_ID 作为 key 保存\n  # 支持函数，动态返回\n  root=ROOT_ID,\n\n  # 默认: id\n  id_key=\"uid\",\n\n  # 默认：parentId\n  parent_key=\"pid\",\n\n  # 挂载子级的属性名称，默认：children\n  child_key=\"items\",\n\n  # 数据预处理，接收一个自定义函数\n  # 可以在这里操作行数据，返回 None 将被跳过\n  transform=add_property('checked', False),\n\n  # 接管插入行为\n  # 接收一个自定义函数\n  insert=sort_insert('sort')\n)\n\nprint(tree)\n# output:\n# [\n#   {\n#     \"uid\": 10000,\n#     \"pid\": None,\n#     \"title\": '标题 1',\n#     \"sort\": 1,\n#     \"checked\": false,\n#     \"items\": [\n#       { \"uid\": 11000, \"pid\": 10000, \"title\": '标题 1-1', \"sort\": 3, \"checked\": false, \"items\": [] }\n#     ]\n#   },\n#   { \"uid\": 20000, \"pid\": None, \"title\": '标题 2', \"sort\": 2, \"checked\": false, \"items\": [] }\n# ]\n```\n\n## 本地开发\n\n### 安装打包工具\n\n```bash\n# 安装 build 模块\n$ python3 -m pip install build\n\n# 安装 twine 包\n$ python3 -m pip install twine\n```\n\n### 启动开发模式\n\n```bash\n# 启动开发模式\n# See https://packaging.python.org/guides/distributing-packages-using-setuptools/#id68\n$ python3 -m pip install -e . --no-deps\n\n# 代码打包\n$ python3 -m build --wheel\n\n# 检查发布内容\n$ twine check dist/*\n\n# 发布正式包，需要 pypi 账号\n$ twine upload dist/*\n\n# 发布测试包，需要 pypi 账号\n$ twine upload --repository testpypi dist/*\n```\n\n## 相关推荐\n\n- [js.tree](https://github.com/zhengxs2018/js.tree)\n\n## License\n\n- MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhengxs2018%2Ftiny-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhengxs2018%2Ftiny-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhengxs2018%2Ftiny-tree/lists"}