Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
203a0dd1
T
treevalue
项目概览
OpenDILab开源决策智能平台
/
treevalue
大约 1 年 前同步成功
通知
3
Star
213
Fork
3
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
treevalue
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
203a0dd1
编写于
1月 26, 2022
作者:
HansBug
😆
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): update the tree print system
上级
f7d1b6fd
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
42 addition
and
18 deletion
+42
-18
test/utils/test_formattree.py
test/utils/test_formattree.py
+18
-2
treevalue/utils/formattree.py
treevalue/utils/formattree.py
+24
-16
未找到文件。
test/utils/test_formattree.py
浏览文件 @
203a0dd1
...
@@ -34,8 +34,8 @@ from treevalue.utils import (
...
@@ -34,8 +34,8 @@ from treevalue.utils import (
@
pytest
.
mark
.
unittest
@
pytest
.
mark
.
unittest
class
TestFormatTree
(
TestCase
):
class
TestFormatTree
(
TestCase
):
def
format_tree
(
self
,
tree
):
def
format_tree
(
self
,
tree
,
encoding
=
'utf8'
):
return
format_tree
(
tree
,
itemgetter
(
0
),
itemgetter
(
1
))
return
format_tree
(
tree
,
itemgetter
(
0
),
itemgetter
(
1
)
,
encoding
)
def
test_single_node_tree
(
self
):
def
test_single_node_tree
(
self
):
tree
=
(
'foo'
,
[])
tree
=
(
'foo'
,
[])
...
@@ -60,6 +60,22 @@ class TestFormatTree(TestCase):
...
@@ -60,6 +60,22 @@ class TestFormatTree(TestCase):
└── qux
└── qux
'''
),
output
)
'''
),
output
)
def
test_single_level_tree_with_ascii
(
self
):
tree
=
(
'foo'
,
[
(
'bar'
,
[]),
(
'baz'
,
[]),
(
'qux'
,
[]),
],
)
output
=
self
.
format_tree
(
tree
,
encoding
=
'ascii'
)
self
.
assertEqual
(
dedent
(
u
'''
\
foo
+-- bar
+-- baz
`-- qux
'''
),
output
)
def
test_multi_level_tree
(
self
):
def
test_multi_level_tree
(
self
):
tree
=
(
tree
=
(
'foo'
,
[
'foo'
,
[
...
...
treevalue/utils/formattree.py
浏览文件 @
203a0dd1
...
@@ -35,43 +35,45 @@ Changes:
...
@@ -35,43 +35,45 @@ Changes:
import
itertools
import
itertools
import
os
import
os
import
sys
FORK
=
u
'
\u251c
'
_DEFAULT_ENCODING
=
os
.
environ
.
get
(
"PYTHONIOENCODING"
,
sys
.
getdefaultencoding
())
LAST
=
u
'
\u2514
'
VERTICAL
=
u
'
\u2502
'
HORIZONTAL
=
u
'
\u2500
'
NEWLINE
=
u
''
_UTF8_CHARS
=
(
u
'
\u251c
'
,
u
'
\u2514
'
,
u
'
\u2502
'
,
u
'
\u2500
'
,
u
''
)
_ASCII_CHARS
=
(
u
'+'
,
u
'`'
,
u
'|'
,
u
'-'
,
u
''
)
def
_format_newlines
(
prefix
,
formatted_node
):
def
_format_newlines
(
prefix
,
formatted_node
,
chars
:
tuple
):
"""
"""
Convert newlines into U+23EC characters, followed by an actual newline and
Convert newlines into U+23EC characters, followed by an actual newline and
then a tree prefix so as to position the remaining text under the previous
then a tree prefix so as to position the remaining text under the previous
line.
line.
"""
"""
FORK
,
LAST
,
VERTICAL
,
HORIZONTAL
,
NEWLINE
=
chars
replacement
=
u
''
.
join
([
NEWLINE
,
os
.
linesep
,
prefix
])
replacement
=
u
''
.
join
([
NEWLINE
,
os
.
linesep
,
prefix
])
return
replacement
.
join
(
formatted_node
.
splitlines
())
return
replacement
.
join
(
formatted_node
.
splitlines
())
def
_format_tree
(
node
,
format_node
,
get_children
,
prefix
=
u
''
):
def
_format_tree
(
node
,
format_node
,
get_children
,
prefix
=
u
''
,
chars
:
tuple
=
_UTF8_CHARS
):
FORK
,
LAST
,
VERTICAL
,
HORIZONTAL
,
NEWLINE
=
chars
children
=
list
(
get_children
(
node
))
children
=
list
(
get_children
(
node
))
next_prefix
=
u
''
.
join
([
prefix
,
VERTICAL
,
u
' '
])
next_prefix
=
u
''
.
join
([
prefix
,
VERTICAL
,
u
' '
])
for
child
in
children
[:
-
1
]:
for
child
in
children
[:
-
1
]:
yield
u
''
.
join
([
yield
u
''
.
join
([
prefix
,
FORK
,
HORIZONTAL
,
HORIZONTAL
,
u
' '
,
prefix
,
FORK
,
HORIZONTAL
,
HORIZONTAL
,
u
' '
,
_format_newlines
(
next_prefix
,
format_node
(
child
))])
_format_newlines
(
next_prefix
,
format_node
(
child
)
,
chars
)])
for
result
in
_format_tree
(
child
,
format_node
,
get_children
,
next_prefix
):
for
result
in
_format_tree
(
child
,
format_node
,
get_children
,
next_prefix
,
chars
):
yield
result
yield
result
if
children
:
if
children
:
last_prefix
=
u
''
.
join
([
prefix
,
u
' '
])
last_prefix
=
u
''
.
join
([
prefix
,
u
' '
])
yield
u
''
.
join
([
yield
u
''
.
join
([
prefix
,
LAST
,
HORIZONTAL
,
HORIZONTAL
,
u
' '
,
prefix
,
LAST
,
HORIZONTAL
,
HORIZONTAL
,
u
' '
,
_format_newlines
(
last_prefix
,
format_node
(
children
[
-
1
]))])
_format_newlines
(
last_prefix
,
format_node
(
children
[
-
1
])
,
chars
)])
for
result
in
_format_tree
(
children
[
-
1
],
format_node
,
get_children
,
last_prefix
):
for
result
in
_format_tree
(
children
[
-
1
],
format_node
,
get_children
,
last_prefix
,
chars
):
yield
result
yield
result
def
format_tree
(
node
,
format_node
,
get_children
)
->
str
:
def
format_tree
(
node
,
format_node
,
get_children
,
encoding
=
None
)
->
str
:
r
"""
r
"""
Overview:
Overview:
Format the given tree.
Format the given tree.
...
@@ -80,6 +82,8 @@ def format_tree(node, format_node, get_children) -> str:
...
@@ -80,6 +82,8 @@ def format_tree(node, format_node, get_children) -> str:
- node: Node object
- node: Node object
- format_node: Format node getter
- format_node: Format node getter
- get_children: Children getter.
- get_children: Children getter.
- encoding: Encoding to be used. Default is ``None`` which means system encoding. \
When ASCII encoding is used, ASCII chars will be used instead of original chars.
Returns:
Returns:
- formatted: Formatted string.
- formatted: Formatted string.
...
@@ -111,9 +115,13 @@ def format_tree(node, format_node, get_children) -> str:
...
@@ -111,9 +115,13 @@ def format_tree(node, format_node, get_children) -> str:
└── c
└── c
d
d
"""
"""
lines
=
itertools
.
chain
(
if
'ASCII'
in
(
encoding
or
_DEFAULT_ENCODING
).
upper
():
_chars
=
_ASCII_CHARS
else
:
_chars
=
_UTF8_CHARS
return
os
.
linesep
.
join
(
itertools
.
chain
(
[
format_node
(
node
)],
[
format_node
(
node
)],
_format_tree
(
node
,
format_node
,
get_children
),
_format_tree
(
node
,
format_node
,
get_children
,
u
''
,
_chars
),
[
u
''
],
[
u
''
],
)
))
return
os
.
linesep
.
join
(
lines
)
HansBug
😆
@HansBug
mentioned in commit
5c8206fa
·
1月 27, 2022
mentioned in commit
5c8206fa
mentioned in commit 5c8206fabd41f0fc43ae808a68a1848ffe536300
开关提交列表
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录