Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
3a84f72f
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,发现更多精彩内容 >>
提交
3a84f72f
编写于
4月 27, 2022
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): add the new pop method
上级
a96f06ef
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
183 addition
and
1 deletion
+183
-1
test/tree/common/test_storage.py
test/tree/common/test_storage.py
+104
-0
test/tree/tree/test_tree.py
test/tree/tree/test_tree.py
+35
-0
treevalue/tree/common/storage.pxd
treevalue/tree/common/storage.pxd
+2
-0
treevalue/tree/common/storage.pyx
treevalue/tree/common/storage.pyx
+18
-0
treevalue/tree/tree/tree.pxd
treevalue/tree/tree/tree.pxd
+2
-1
treevalue/tree/tree/tree.pyx
treevalue/tree/tree/tree.pyx
+22
-0
未找到文件。
test/tree/common/test_storage.py
浏览文件 @
3a84f72f
...
...
@@ -97,6 +97,110 @@ class TestTreeStorage:
assert
t1
.
get_or_default
(
'fff'
,
delayed_partial
(
lambda
:
2345
))
==
2345
assert
not
t1
.
contains
(
'fff'
)
def
test_pop
(
self
):
t
=
create_storage
({
'a'
:
1
,
'b'
:
2
,
'c'
:
raw
({
'x'
:
3
,
'y'
:
4
}),
'd'
:
{
'x'
:
3
,
'y'
:
4
}})
assert
t
.
pop
(
'a'
)
==
1
with
pytest
.
raises
(
KeyError
):
t
.
pop
(
'a'
)
assert
t
.
pop
(
'b'
)
==
2
assert
t
.
pop
(
'c'
)
==
{
'x'
:
3
,
'y'
:
4
}
td
=
t
.
pop
(
'd'
)
assert
isinstance
(
td
,
TreeStorage
)
assert
td
.
get
(
'x'
)
==
3
assert
td
.
get
(
'y'
)
==
4
with
pytest
.
raises
(
KeyError
):
t
.
pop
(
'aksjdlasdkjf'
)
cnt1
,
cnt2
,
cnt3
=
0
,
0
,
0
def
f1
():
nonlocal
cnt1
cnt1
+=
1
return
2
def
f2
(
x
,
y
):
nonlocal
cnt2
cnt2
+=
1
return
{
'x'
:
x
,
'y'
:
y
}
def
f3
(
x
,
y
):
nonlocal
cnt3
cnt3
+=
1
return
create_storage
({
'x'
:
x
,
'y'
:
raw
(
y
)})
t2
=
create_storage
({
'a'
:
1
,
'b'
:
delayed_partial
(
f1
),
'c'
:
delayed_partial
(
f2
,
delayed_partial
(
f1
),
3
),
'd'
:
delayed_partial
(
f3
,
3
,
delayed_partial
(
f2
,
3
,
4
))
})
assert
t2
.
pop
(
'a'
)
==
1
assert
cnt1
==
0
assert
t2
.
pop
(
'b'
)
==
2
assert
cnt1
==
1
with
pytest
.
raises
(
KeyError
):
t2
.
pop
(
'b'
)
assert
cnt1
==
1
assert
(
cnt1
,
cnt2
)
==
(
1
,
0
)
assert
t2
.
pop
(
'c'
)
==
{
'x'
:
2
,
'y'
:
3
}
assert
(
cnt1
,
cnt2
)
==
(
2
,
1
)
with
pytest
.
raises
(
KeyError
):
t2
.
pop
(
'c'
)
assert
(
cnt1
,
cnt2
)
==
(
2
,
1
)
assert
(
cnt1
,
cnt2
,
cnt3
)
==
(
2
,
1
,
0
)
assert
t2
.
get
(
'd'
).
pop
(
'x'
)
==
3
assert
t2
.
get
(
'd'
).
pop
(
'y'
)
==
{
'x'
:
3
,
'y'
:
4
}
assert
(
cnt1
,
cnt2
,
cnt3
)
==
(
2
,
2
,
1
)
with
pytest
.
raises
(
KeyError
):
t2
.
get
(
'd'
).
pop
(
'x'
)
with
pytest
.
raises
(
KeyError
):
t2
.
get
(
'd'
).
pop
(
'y'
)
assert
(
cnt1
,
cnt2
,
cnt3
)
==
(
2
,
2
,
1
)
def
test_pop_or_default
(
self
):
t
=
create_storage
({
'a'
:
1
,
'b'
:
2
,
'c'
:
raw
({
'x'
:
3
,
'y'
:
4
}),
'd'
:
{
'x'
:
3
,
'y'
:
4
}})
assert
t
.
pop_or_default
(
'a'
,
233
)
==
1
with
pytest
.
raises
(
KeyError
):
t
.
pop
(
'a'
)
assert
t
.
pop_or_default
(
'a'
,
233
)
==
233
assert
t
.
pop_or_default
(
'b'
,
233
)
==
2
assert
t
.
pop_or_default
(
'c'
,
233
)
==
{
'x'
:
3
,
'y'
:
4
}
td
=
t
.
pop_or_default
(
'd'
,
233
)
assert
isinstance
(
td
,
TreeStorage
)
assert
td
.
pop_or_default
(
'x'
,
233
)
==
3
assert
td
.
pop_or_default
(
'y'
,
233
)
==
4
assert
t
.
pop_or_default
(
'fff'
,
233
)
==
233
t
=
create_storage
({
'a'
:
1
,
'b'
:
2
,
'c'
:
raw
({
'x'
:
3
,
'y'
:
4
}),
'd'
:
{
'x'
:
3
,
'y'
:
4
}})
t1
=
create_storage
({
'a'
:
delayed_partial
(
lambda
:
t
.
get
(
'a'
)),
'b'
:
delayed_partial
(
lambda
:
t
.
get
(
'b'
)),
'c'
:
delayed_partial
(
lambda
:
t
.
get
(
'c'
)),
'd'
:
delayed_partial
(
lambda
:
t
.
get
(
'd'
)),
})
assert
t1
.
pop_or_default
(
'a'
,
233
)
==
1
assert
t1
.
pop_or_default
(
'b'
,
233
)
==
2
assert
t1
.
pop_or_default
(
'c'
,
233
)
==
{
'x'
:
3
,
'y'
:
4
}
t1d
=
t1
.
pop_or_default
(
'd'
,
233
)
assert
isinstance
(
t1d
,
TreeStorage
)
assert
t1d
.
pop_or_default
(
'x'
,
233
)
==
3
assert
t1d
.
pop_or_default
(
'y'
,
233
)
==
4
assert
t1
.
pop_or_default
(
'fff'
,
233
)
==
233
assert
t1
.
pop_or_default
(
'fff'
,
delayed_partial
(
lambda
:
2345
))
==
2345
assert
not
t1
.
contains
(
'fff'
)
def
test_set
(
self
):
t
=
create_storage
({})
t
.
set
(
'a'
,
1
)
...
...
test/tree/tree/test_tree.py
浏览文件 @
3a84f72f
...
...
@@ -268,6 +268,41 @@ class TestTreeTreeTree:
_
=
tv1
.
get
(
'e'
)
assert
tv1
.
get
(
'e'
,
233
)
==
233
tv1
=
TreeValue
({
'a'
:
1
,
'b'
:
2
,
'c'
:
{
'x'
:
2
,
'y'
:
3
},
'd'
:
raw
({
'x'
:
2
,
'y'
:
3
})})
tv2
=
TreeValue
({
'a'
:
delayed
(
lambda
:
tv1
.
a
),
'b'
:
delayed
(
lambda
:
tv1
.
b
),
'c'
:
delayed
(
lambda
:
tv1
.
c
),
})
assert
tv2
.
get
(
'a'
)
==
1
assert
tv2
.
get
(
'b'
)
==
2
assert
tv2
.
get
(
'c'
)
==
TreeValue
({
'x'
:
2
,
'y'
:
3
})
def
test_pop
(
self
):
tv1
=
TreeValue
({
'a'
:
1
,
'b'
:
2
,
'c'
:
{
'x'
:
2
,
'y'
:
3
},
'd'
:
raw
({
'x'
:
2
,
'y'
:
3
})})
assert
tv1
.
pop
(
'a'
)
==
1
with
pytest
.
raises
(
KeyError
):
_
=
tv1
.
pop
(
'a'
)
assert
tv1
.
pop
(
'a'
,
233
)
==
233
assert
tv1
.
pop
(
'b'
)
==
2
assert
tv1
.
pop
(
'c'
)
==
TreeValue
({
'x'
:
2
,
'y'
:
3
})
assert
tv1
.
pop
(
'd'
)
==
{
'x'
:
2
,
'y'
:
3
}
with
pytest
.
raises
(
KeyError
):
_
=
tv1
.
pop
(
'e'
)
assert
tv1
.
pop
(
'e'
,
233
)
==
233
tv1
=
TreeValue
({
'a'
:
1
,
'b'
:
2
,
'c'
:
{
'x'
:
2
,
'y'
:
3
},
'd'
:
raw
({
'x'
:
2
,
'y'
:
3
})})
tv2
=
TreeValue
({
'a'
:
delayed
(
lambda
:
tv1
.
a
),
'b'
:
delayed
(
lambda
:
tv1
.
b
),
'c'
:
delayed
(
lambda
:
tv1
.
c
),
})
assert
tv2
.
pop
(
'a'
)
==
1
assert
tv2
.
pop
(
'b'
)
==
2
assert
tv2
.
pop
(
'c'
)
==
TreeValue
({
'x'
:
2
,
'y'
:
3
})
def
test_keys
(
self
):
tv1
=
TreeValue
({
'a'
:
1
,
'b'
:
2
,
'c'
:
{
'x'
:
2
,
'y'
:
3
},
'd'
:
raw
({
'x'
:
2
,
'y'
:
3
})})
assert
set
(
tv1
.
keys
())
==
{
'a'
,
'b'
,
'c'
,
'd'
}
...
...
treevalue/tree/common/storage.pxd
浏览文件 @
3a84f72f
...
...
@@ -12,6 +12,8 @@ cdef class TreeStorage:
cpdef
public
void
set
(
self
,
str
key
,
object
value
)
except
*
cpdef
public
object
get
(
self
,
str
key
)
cpdef
public
object
get_or_default
(
self
,
str
key
,
object
default
)
cpdef
public
object
pop
(
self
,
str
key
)
cpdef
public
object
pop_or_default
(
self
,
str
key
,
object
default
)
cpdef
public
void
del_
(
self
,
str
key
)
except
*
cpdef
public
boolean
contains
(
self
,
str
key
)
cpdef
public
uint
size
(
self
)
...
...
treevalue/tree/common/storage.pyx
浏览文件 @
3a84f72f
...
...
@@ -35,6 +35,24 @@ cdef class TreeStorage:
v
=
self
.
map
.
get
(
key
,
default
)
return
_c_undelay_check_data
(
self
.
map
,
key
,
v
)
cpdef
public
object
pop
(
self
,
str
key
):
cdef
object
v
,
nv
,
res
try
:
v
=
self
.
map
[
key
]
res
=
_c_undelay_data
(
self
.
map
,
key
,
v
)
del
self
.
map
[
key
]
return
res
except
KeyError
:
raise
KeyError
(
f
"Key
{
repr
(
key
)
}
not found in this tree."
)
cpdef
public
object
pop_or_default
(
self
,
str
key
,
object
default
):
cdef
object
v
,
nv
,
res
v
=
self
.
map
.
get
(
key
,
default
)
res
=
_c_undelay_data
(
self
.
map
,
key
,
v
)
if
key
in
self
.
map
:
del
self
.
map
[
key
]
return
res
cpdef
public
void
del_
(
self
,
str
key
)
except
*
:
try
:
del
self
.
map
[
key
]
...
...
treevalue/tree/tree/tree.pxd
浏览文件 @
3a84f72f
...
...
@@ -18,6 +18,7 @@ cdef class TreeValue:
cpdef
_setitem_extern
(
self
,
object
key
,
object
value
)
cpdef
_delitem_extern
(
self
,
object
key
)
cpdef
get
(
self
,
str
key
,
object
default
=
*
)
cpdef
pop
(
self
,
str
key
,
object
default
=
*
)
cdef
str
_prefix_fix
(
object
text
,
object
prefix
)
cdef
object
_build_tree
(
TreeStorage
st
,
object
type_
,
str
prefix
,
dict
id_pool
,
tuple
path
)
...
...
treevalue/tree/tree/tree.pyx
浏览文件 @
3a84f72f
...
...
@@ -119,6 +119,28 @@ cdef class TreeValue:
return
self
.
_unraw
(
value
)
@
cython
.
binding
(
True
)
cpdef
pop
(
self
,
str
key
,
object
default
=
_GET_NO_DEFAULT
):
"""
Overview:
Pop item from the tree node.
Arguments:
- key (:obj:`str`): Item's name.
- default (:obj:`default`): Default value when this item is not found, default is
\
`_GET_NO_DEFAULT` which means just raise `KeyError` when not found.
Returns:
- value: Item's value.
"""
cdef
object
value
if
default
is
_GET_NO_DEFAULT
:
value
=
self
.
_st
.
pop
(
key
)
else
:
value
=
self
.
_st
.
pop_or_default
(
key
,
default
)
return
self
.
_unraw
(
value
)
@
cython
.
binding
(
True
)
cpdef
_attr_extern
(
self
,
str
key
):
r
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录