Skip to main content

一个循环解决行转树的问题,快速,轻量,无依赖。

Project description

tiny-tree

快速,轻量,无依赖的树结构数据处理函数库。


  • 一个循环解决行转树的问题
  • 除添加 children 属性外,不会修改任何数据
  • 支持任意关系字段,如:非 idparentId, children 字段支持
  • 支持接管插入行为,如:自定义插入顺序
  • 支持动态导出树节点

快速开始

文档

注意: 因为引入了 typing 模块,需要 python>=3.6

安装

# 不支持 python2
$ pip3 install tiny-tree

使用

from tiny_tree.to_tree import to_tree

to_tree([
  { id: 10000, parentId: None, title: "标题 1" },
  { id: 20000, parentId: None, title: "标题 2" },
  { id: 11000, parentId: 10000, title: "标题 1-1" },
])
# [
#   {
#     id: 10000,
#     parentId: None,
#     title: '标题 1',
#     children: [
#       { id: 11000, parentId: 10000, title: '标题 1-1', children: [] }
#     ]
#   },
#   { id: 20000, parentId: None, title: '标题 2', children: [] },
# ]

支持任意关系字段的数据

from tiny_tree.to_tree import to_tree, ROOT_ID
from tiny_tree.helpers import sort_insert, add_property

data = [
  { "uid": 10000, "pid": None, "title": "标题 1", "sort": 1 },
  { "uid": 20000, "pid": None, "title": "标题 2", "sort": 2 },
  { "uid": 11000, "pid": 10000, "title": "标题 1-1", "sort": 3 },
]

tree = to_tree(
  data,
  # 如果 parentId 为 None
  # 使用 ROOT_ID 作为 key 保存
  # 支持函数,动态返回
  root=ROOT_ID,

  # 默认: id
  id_key="uid",

  # 默认:parentId
  parent_key="pid",

  # 挂载子级的属性名称,默认:children
  child_key="items",

  # 数据预处理,接收一个自定义函数
  # 可以在这里操作行数据,返回 None 将被跳过
  transform=add_property('checked', False),

  # 接管插入行为
  # 接收一个自定义函数
  insert=sort_insert('sort')
)

print(tree)
# output:
# [
#   {
#     uid: 10000,
#     pid: None,
#     title: '标题 1',
#     sort: 1,
#     checked: false,
#     items: [
#       { uid: 11000, pid: 10000, title: '标题 1-1', sort: 3, checked: false, items: [] }
#     ]
#   },
#   { uid: 20000, pid: None, title: '标题 2', sort: 2, checked: false, items: [] }
# ]

本地开发

安装打包工具

# 安装 build 模块
$ python3 -m pip install build

# 安装 twine 包
$ python3 -m pip install twine

启动开发模式

# 启动开发模式
# See https://packaging.python.org/guides/distributing-packages-using-setuptools/#id68
$ python3 -m pip install -e . --no-deps

# 代码打包
$ python3 -m build --wheel

# 检查发布内容
$ twine check dist/*

# 发布正式包,需要 pypi 账号
$ twine upload dist/*

# 发布测试包,需要 pypi 账号
$ twine upload --repository testpypi dist/*

相关推荐

License

  • MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tiny_tree-0.1.0-py2.py3-none-any.whl (6.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file tiny_tree-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: tiny_tree-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

File hashes

Hashes for tiny_tree-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0aeb81e4eeccc4c9d7045dc2c3897ab269830fffcfcc85b0bd8d95e99467ca92
MD5 26fdac03f92693eb5b0e1f4ddac0290f
BLAKE2b-256 23597447fdea1c6f9a5c82b8b03ce50917e9b5186c47bb0dfb5438f6f100850b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page