Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
672875b8
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,发现更多精彩内容 >>
提交
672875b8
编写于
2月 24, 2023
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): fix support of rise and subside for namedtuple
上级
d99d476c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
42 addition
and
4 deletion
+42
-4
test/tree/tree/test_structural.py
test/tree/tree/test_structural.py
+26
-0
treevalue/tree/tree/structural.pyx
treevalue/tree/tree/structural.pyx
+16
-4
未找到文件。
test/tree/tree/test_structural.py
浏览文件 @
672875b8
from
collections
import
namedtuple
import
pytest
from
treevalue.tree
import
TreeValue
,
mapping
,
union
,
raw
,
subside
,
rise
,
delayed
...
...
@@ -116,6 +118,18 @@ class TestTreeTreeStructural:
assert
subside
({
'a'
:
1
,
'b'
:
2
,
'x'
:
{
'c'
:
3
,
'd'
:
4
},
'e'
:
[
3
,
4
,
5
]})
==
\
{
'a'
:
1
,
'b'
:
2
,
'x'
:
{
'c'
:
3
,
'd'
:
4
},
'e'
:
[
3
,
4
,
5
]}
nt
=
namedtuple
(
'nt'
,
[
'a'
,
'b'
,
'c'
])
a
=
nt
(
MyTreeValue
({
'x'
:
1
,
'y'
:
2
,
'z'
:
{
'v'
:
3
}}),
MyTreeValue
({
'x'
:
4
,
'y'
:
5
,
'z'
:
{
'v'
:
6
}}),
MyTreeValue
({
'x'
:
7
,
'y'
:
8
,
'z'
:
{
'v'
:
9
}}),
)
assert
subside
(
a
)
==
MyTreeValue
({
'x'
:
nt
(
1
,
4
,
7
),
'y'
:
nt
(
2
,
5
,
8
),
'z'
:
{
'v'
:
nt
(
3
,
6
,
9
),
},
})
def
test_subside_delayed
(
self
):
class
MyTreeValue
(
TreeValue
):
pass
...
...
@@ -273,3 +287,15 @@ class TestTreeTreeStructural:
MyTreeValue
({
'v'
:
{
'x'
:
3
,
'y'
:
8
}}),
]
}
nt
=
namedtuple
(
'nt'
,
[
'a'
,
'b'
,
'c'
])
t11
=
MyTreeValue
({
'x'
:
nt
(
1
,
4
,
7
),
'y'
:
nt
(
2
,
5
,
8
),
'z'
:
{
'v'
:
nt
(
3
,
6
,
9
),
},
})
assert
rise
(
t11
)
==
nt
(
MyTreeValue
({
'x'
:
1
,
'y'
:
2
,
'z'
:
{
'v'
:
3
}}),
MyTreeValue
({
'x'
:
4
,
'y'
:
5
,
'z'
:
{
'v'
:
6
}}),
MyTreeValue
({
'x'
:
7
,
'y'
:
8
,
'z'
:
{
'v'
:
9
}}),
)
treevalue/tree/tree/structural.pyx
浏览文件 @
672875b8
...
...
@@ -16,6 +16,15 @@ from ..func.modes cimport _c_load_mode
MISSING_NOT_ALLOW
=
SingletonMark
(
"missing_not_allow"
)
cdef
inline
bool
_c_is_namedtuple
(
object
type_
):
return
issubclass
(
type_
,
tuple
)
and
hasattr
(
type_
,
'_fields'
)
and
hasattr
(
type_
,
'_asdict'
)
cdef
inline
object
_c_create_sequence_with_type
(
object
type_
,
object
iters
):
if
_c_is_namedtuple
(
type_
):
return
type_
(
*
iters
)
else
:
return
type_
(
iters
)
cdef
object
_c_subside_process
(
tuple
value
,
object
it
):
cdef
type
type_
cdef
list
items
...
...
@@ -29,7 +38,7 @@ cdef object _c_subside_process(tuple value, object it):
_l_res
=
[]
for
v
in
items
:
_l_res
.
append
(
_c_subside_process
(
v
,
it
))
return
type_
(
_l_res
)
return
_c_create_sequence_with_type
(
type_
,
_l_res
)
elif
issubclass
(
type_
,
dict
):
_d_res
=
{}
for
k
,
v
in
items
:
...
...
@@ -270,7 +279,7 @@ cdef object _c_rise_struct_builder(tuple p, object it):
for
v
in
item
:
_l_res
.
append
(
_c_rise_struct_builder
(
v
,
it
))
return
type_
(
_l_res
)
return
_c_create_sequence_with_type
(
type_
,
_l_res
)
else
:
return
next
(
it
)
...
...
@@ -298,7 +307,10 @@ cdef tuple _c_rise_struct_process(list objs, object template):
raise
ValueError
(
f
"At least
{
repr
(
_l_temp
-
2
)
}
value expected due to template "
f
"
{
repr
(
template
)
}
, but length is
{
repr
(
_l_obj_0
)
}
."
)
_a_template
=
type
(
template
)(
chain
(
template
[:
-
2
],
(
template
[
-
2
],)
*
(
_l_obj_0
-
_l_temp
+
2
)))
_a_template
=
_c_create_sequence_with_type
(
type
(
template
),
chain
(
template
[:
-
2
],
(
template
[
-
2
],)
*
(
_l_obj_0
-
_l_temp
+
2
))
)
else
:
_a_template
=
template
elif
template
is
None
:
...
...
@@ -330,7 +342,7 @@ cdef tuple _c_rise_struct_process(list objs, object template):
if
failed
:
_a_template
=
object
else
:
_a_template
=
_
t_type
(
None
for
_
in
range
(
length
))
_a_template
=
_
c_create_sequence_with_type
(
_t_type
,
(
None
for
_
in
range
(
length
)
))
else
:
_a_template
=
object
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录