Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
a40d97c0
T
treevalue
项目概览
OpenDILab开源决策智能平台
/
treevalue
9 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
a40d97c0
编写于
2月 23, 2023
作者:
HansBug
😆
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): optimize graphviz visualization
上级
d99d476c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
27 addition
and
7 deletion
+27
-7
treevalue/tree/tree/graph.py
treevalue/tree/tree/graph.py
+26
-4
treevalue/utils/tree.py
treevalue/utils/tree.py
+1
-3
未找到文件。
treevalue/tree/tree/graph.py
浏览文件 @
a40d97c0
import
html
from
functools
import
lru_cache
from
typing
import
Type
,
Callable
,
Union
,
Optional
,
Tuple
from
graphviz
import
Digraph
,
nohtml
from
graphviz
import
Digraph
from
hbutils.reflection
import
post_process
,
dynamic_call
,
freduce
from
.tree
import
TreeValue
...
...
@@ -134,6 +135,27 @@ _GENERIC_N = 36
_GENERIC_S
=
_GENERIC_N
//
3
def
_custom_repr
(
x
):
if
isinstance
(
x
,
(
str
,)):
return
repr
(
x
)
else
:
return
str
(
x
)
_LEFT_ALIGN_LINESEP
=
r
'\l'
def
_custom_html_escape
(
x
):
s
=
html
.
escape
(
x
)
\
.
replace
(
' '
,
' '
)
\
.
replace
(
'
\r\n
'
,
_LEFT_ALIGN_LINESEP
)
\
.
replace
(
'
\n
'
,
_LEFT_ALIGN_LINESEP
)
\
.
replace
(
'
\r
'
,
_LEFT_ALIGN_LINESEP
)
if
len
(
x
.
splitlines
())
>
1
and
not
s
.
endswith
(
_LEFT_ALIGN_LINESEP
):
s
+=
_LEFT_ALIGN_LINESEP
return
s
def
graphics
(
*
trees
,
title
:
Optional
[
str
]
=
None
,
cfg
:
Optional
[
dict
]
=
None
,
dup_value
:
Union
[
bool
,
Callable
,
type
,
Tuple
[
Type
,
...]]
=
False
,
repr_gen
:
Optional
[
Callable
]
=
None
,
...
...
@@ -182,9 +204,9 @@ def graphics(*trees, title: Optional[str] = None, cfg: Optional[dict] = None,
return
build_graph
(
*
trees
,
node_id_gen
=
_node_tag
,
graph_title
=
title
or
"<untitled>"
,
graph_title
=
title
,
graph_cfg
=
cfg
or
{},
repr_gen
=
repr_gen
or
(
lambda
x
:
nohtml
(
repr
(
x
))),
repr_gen
=
repr_gen
or
(
lambda
x
:
_custom_html_escape
(
_custom_
repr
(
x
))),
iter_gen
=
lambda
n
:
iter
(
n
.
items
())
if
isinstance
(
n
,
TreeValue
)
else
None
,
node_cfg_gen
=
_dict_call_merge
(
lambda
n
,
p
,
np
,
pp
,
is_node
,
is_root
,
root
:
{
'fillcolor'
:
_shape_color
(
root
[
2
]),
...
...
@@ -193,7 +215,7 @@ def graphics(*trees, title: Optional[str] = None, cfg: Optional[dict] = None,
'style'
:
'filled'
,
'shape'
:
'diamond'
if
is_root
else
(
'ellipse'
if
is_node
else
'box'
),
'penwidth'
:
3
if
is_root
else
1.5
,
'fontname'
:
"
Times-Roman bold"
if
is_node
else
"Times-Roman
"
,
'fontname'
:
"
monospace"
if
is_node
else
"monospace
"
,
},
(
node_cfg_gen
or
(
lambda
:
{}))),
edge_cfg_gen
=
_dict_call_merge
(
lambda
n
,
p
,
np
,
pp
,
is_node
,
root
:
{
'arrowhead'
:
'vee'
if
is_node
else
'dot'
,
...
...
treevalue/utils/tree.py
浏览文件 @
a40d97c0
...
...
@@ -6,8 +6,6 @@ from typing import Optional, Mapping, Any, Callable
from
graphviz
import
Digraph
from
hbutils.reflection
import
dynamic_call
,
post_process
from
.random
import
random_hex_with_timestamp
def
_title_flatten
(
title
):
title
=
re
.
sub
(
r
'[^a-zA-Z0-9_]+'
,
'_'
,
str
(
title
))
...
...
@@ -104,7 +102,7 @@ def build_graph(*roots, node_id_gen: Optional[Callable] = None,
roots
=
[
item
for
item
in
roots
if
item
is
not
None
]
node_id_gen
=
dynamic_call
(
suffixed_node_id
(
node_id_gen
or
_default_node_id
))
graph_title
=
graph_title
or
(
'untitled_'
+
random_hex_with_timestamp
())
graph_title
=
graph_title
or
''
graph_name
=
graph_name
or
_title_flatten
(
graph_title
)
graph_cfg
=
_no_none_value
(
graph_cfg
or
{})
...
...
HansBug
😆
@HansBug
mentioned in commit
2eea0303
·
2月 25, 2023
mentioned in commit
2eea0303
mentioned in commit 2eea030367d40b1982d3e207ec47e3dfc00eb94e
开关提交列表
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录