Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDILab开源决策智能平台
treevalue
提交
fb9d5e23
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,发现更多精彩内容 >>
提交
fb9d5e23
编写于
11月 28, 2022
作者:
HansBug
😆
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dev(hansbug): rename BaseConstraint to Constraint
上级
85a52041
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
70 addition
and
70 deletion
+70
-70
treevalue/tree/tree/__init__.py
treevalue/tree/tree/__init__.py
+1
-1
treevalue/tree/tree/constraint.pxd
treevalue/tree/tree/constraint.pxd
+20
-20
treevalue/tree/tree/constraint.pyx
treevalue/tree/tree/constraint.pyx
+49
-49
未找到文件。
treevalue/tree/tree/__init__.py
浏览文件 @
fb9d5e23
from
.constraint
import
to_constraint
,
Base
Constraint
,
NodeConstraint
,
ValueConstraint
,
cleaf
from
.constraint
import
to_constraint
,
Constraint
,
NodeConstraint
,
ValueConstraint
,
cleaf
from
.flatten
import
flatten
,
unflatten
,
flatten_values
,
flatten_keys
from
.functional
import
mapping
,
filter_
,
mask
,
reduce_
from
.graph
import
graphics
...
...
treevalue/tree/tree/constraint.pxd
浏览文件 @
fb9d5e23
...
...
@@ -6,52 +6,52 @@ from libcpp cimport bool
cdef
class
_WrappedConstraintException
(
Exception
):
pass
cdef
class
Base
Constraint
:
cdef
class
Constraint
:
cpdef
void
_validate_node
(
self
,
object
instance
)
except
*
cpdef
void
_validate_value
(
self
,
object
instance
)
except
*
cpdef
object
_features
(
self
)
cpdef
bool
_contains
(
self
,
Base
Constraint
other
)
cpdef
Base
Constraint
_transaction
(
self
,
str
key
)
cpdef
bool
_contains
(
self
,
Constraint
other
)
cpdef
Constraint
_transaction
(
self
,
str
key
)
cdef
bool
_feature_match
(
self
,
Base
Constraint
other
)
cdef
bool
_contains_check
(
self
,
Base
Constraint
other
)
cdef
bool
_feature_match
(
self
,
Constraint
other
)
cdef
bool
_contains_check
(
self
,
Constraint
other
)
cdef
tuple
_native_validate
(
self
,
object
instance
,
type
type_
,
list
path
)
cpdef
tuple
check
(
self
,
object
instance
)
cpdef
bool
equiv
(
self
,
object
other
)
cdef
Base
Constraint
_r_parse_cons
(
object
obj
)
cpdef
Base
Constraint
to_constraint
(
object
obj
)
cdef
Constraint
_r_parse_cons
(
object
obj
)
cpdef
Constraint
to_constraint
(
object
obj
)
cdef
class
EmptyConstraint
(
Base
Constraint
):
cdef
class
EmptyConstraint
(
Constraint
):
pass
cdef
class
ValueConstraint
(
Base
Constraint
):
cdef
class
ValueConstraint
(
Constraint
):
pass
cdef
class
NodeConstraint
(
Base
Constraint
):
cdef
class
NodeConstraint
(
Constraint
):
pass
cdef
class
TypeConstraint
(
ValueConstraint
):
cdef
readonly
type
type_
cdef
class
LeafConstraint
(
Base
Constraint
):
cdef
class
LeafConstraint
(
Constraint
):
pass
cpdef
LeafConstraint
cleaf
()
cdef
class
TreeConstraint
(
Base
Constraint
):
cdef
class
TreeConstraint
(
Constraint
):
cdef
readonly
dict
_constraints
cdef
Base
Constraint
_s_tree_merge
(
list
constraints
)
cdef
Base
Constraint
_s_tree
(
TreeConstraint
constraint
)
cdef
Constraint
_s_tree_merge
(
list
constraints
)
cdef
Constraint
_s_tree
(
TreeConstraint
constraint
)
cdef
class
CompositeConstraint
(
Base
Constraint
):
cdef
class
CompositeConstraint
(
Constraint
):
cdef
readonly
tuple
_constraints
cdef
void
_rec_composite_iter
(
Base
Constraint
constraint
,
list
lst
)
cdef
list
_r_composite_iter
(
Base
Constraint
constraint
)
cdef
void
_rec_composite_iter
(
Constraint
constraint
,
list
lst
)
cdef
list
_r_composite_iter
(
Constraint
constraint
)
cdef
Base
Constraint
_s_generic_merge
(
list
constraints
)
cdef
Base
Constraint
_s_composite
(
CompositeConstraint
constraint
)
cdef
Constraint
_s_generic_merge
(
list
constraints
)
cdef
Constraint
_s_composite
(
CompositeConstraint
constraint
)
cdef
BaseConstraint
_s_simplify
(
Base
Constraint
constraint
)
cdef
Constraint
_s_simplify
(
Constraint
constraint
)
treevalue/tree/tree/constraint.pyx
浏览文件 @
fb9d5e23
...
...
@@ -10,7 +10,7 @@ from ..common.storage cimport TreeStorage
cdef
class
_WrappedConstraintException
(
Exception
):
pass
cdef
class
Base
Constraint
:
cdef
class
Constraint
:
cpdef
void
_validate_node
(
self
,
object
instance
)
except
*
:
raise
NotImplementedError
# pragma: no cover
...
...
@@ -20,27 +20,27 @@ cdef class BaseConstraint:
cpdef
object
_features
(
self
):
raise
NotImplementedError
# pragma: no cover
cpdef
bool
_contains
(
self
,
Base
Constraint
other
):
cpdef
bool
_contains
(
self
,
Constraint
other
):
raise
NotImplementedError
# pragma: no cover
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
raise
NotImplementedError
# pragma: no cover
cdef
inline
bool
_feature_match
(
self
,
Base
Constraint
other
):
cdef
inline
bool
_feature_match
(
self
,
Constraint
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
_features
()
==
other
.
_features
()
cdef
inline
bool
_contains_check
(
self
,
Base
Constraint
other
):
cdef
inline
bool
_contains_check
(
self
,
Constraint
other
):
return
isinstance
(
other
,
EmptyConstraint
)
or
self
.
_feature_match
(
other
)
or
self
.
_contains
(
other
)
cdef
tuple
_native_validate
(
self
,
object
instance
,
type
type_
,
list
path
):
cdef
dict
raw
cdef
str
key
cdef
object
value
cdef
Base
Constraint
subcons
cdef
Constraint
subcons
cdef
bool
retval
cdef
tuple
retpath
cdef
Base
Constraint
retcons
cdef
Constraint
retcons
cdef
Exception
reterr
if
isinstance
(
instance
,
TreeStorage
):
...
...
@@ -81,7 +81,7 @@ cdef class BaseConstraint:
def
validate
(
self
,
object
instance
):
cdef
bool
retval
cdef
tuple
retpath
cdef
Base
Constraint
retcons
cdef
Constraint
retcons
cdef
Exception
reterr
retval
,
retpath
,
retcons
,
reterr
=
self
.
check
(
instance
)
...
...
@@ -95,30 +95,30 @@ cdef class BaseConstraint:
return
hash
(
self
.
_features
())
cpdef
bool
equiv
(
self
,
object
other
):
cdef
Base
Constraint
c
=
to_constraint
(
other
)
cdef
Constraint
c
=
to_constraint
(
other
)
return
self
.
_contains_check
(
c
)
and
c
.
_contains_check
(
self
)
def
__ge__
(
self
,
other
):
cdef
Base
Constraint
c
=
to_constraint
(
other
)
cdef
Constraint
c
=
to_constraint
(
other
)
return
self
.
_contains_check
(
c
)
def
__gt__
(
self
,
other
):
cdef
Base
Constraint
c
=
to_constraint
(
other
)
cdef
Constraint
c
=
to_constraint
(
other
)
return
self
.
_contains_check
(
c
)
and
not
c
.
_contains_check
(
self
)
def
__le__
(
self
,
other
):
cdef
Base
Constraint
c
=
to_constraint
(
other
)
cdef
Constraint
c
=
to_constraint
(
other
)
return
c
.
_contains_check
(
self
)
def
__lt__
(
self
,
other
):
cdef
Base
Constraint
c
=
to_constraint
(
other
)
cdef
Constraint
c
=
to_constraint
(
other
)
return
c
.
_contains_check
(
self
)
and
not
self
.
_contains_check
(
c
)
def
__repr__
(
self
):
return
f
'<
{
type
(
self
).
__name__
}
{
self
.
_features
()
!
r
}
>'
cdef
inline
Base
Constraint
_r_parse_cons
(
object
obj
):
if
isinstance
(
obj
,
Base
Constraint
):
cdef
inline
Constraint
_r_parse_cons
(
object
obj
):
if
isinstance
(
obj
,
Constraint
):
return
obj
elif
obj
is
None
:
return
EmptyConstraint
()
...
...
@@ -131,10 +131,10 @@ cdef inline BaseConstraint _r_parse_cons(object obj):
else
:
raise
TypeError
(
f
'Invalid constraint -
{
obj
!
r
}
.'
)
cpdef
inline
Base
Constraint
to_constraint
(
object
obj
):
cpdef
inline
Constraint
to_constraint
(
object
obj
):
return
_s_simplify
(
_r_parse_cons
(
obj
))
cdef
class
EmptyConstraint
(
Base
Constraint
):
cdef
class
EmptyConstraint
(
Constraint
):
cpdef
void
_validate_node
(
self
,
object
instance
)
except
*
:
pass
...
...
@@ -144,10 +144,10 @@ cdef class EmptyConstraint(BaseConstraint):
cpdef
object
_features
(
self
):
return
()
cpdef
bool
_contains
(
self
,
Base
Constraint
other
):
cpdef
bool
_contains
(
self
,
Constraint
other
):
return
isinstance
(
other
,
EmptyConstraint
)
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
return
self
def
__bool__
(
self
):
...
...
@@ -156,18 +156,18 @@ cdef class EmptyConstraint(BaseConstraint):
def
__repr__
(
self
):
return
f
'<
{
type
(
self
).
__name__
}
>'
cdef
class
ValueConstraint
(
Base
Constraint
):
cdef
class
ValueConstraint
(
Constraint
):
cpdef
void
_validate_node
(
self
,
object
instance
)
except
*
:
pass
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
return
self
cdef
class
NodeConstraint
(
Base
Constraint
):
cdef
class
NodeConstraint
(
Constraint
):
cpdef
void
_validate_value
(
self
,
object
instance
)
except
*
:
raise
TypeError
(
f
'TreeValue node expected, but value
{
instance
!
r
}
found.'
)
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
return
EmptyConstraint
()
cdef
class
TypeConstraint
(
ValueConstraint
):
...
...
@@ -181,10 +181,10 @@ cdef class TypeConstraint(ValueConstraint):
cpdef
object
_features
(
self
):
return
self
.
type_
cpdef
bool
_contains
(
self
,
Base
Constraint
other
):
cpdef
bool
_contains
(
self
,
Constraint
other
):
return
isinstance
(
other
,
TypeConstraint
)
and
issubclass
(
self
.
type_
,
other
.
type_
)
cdef
class
LeafConstraint
(
Base
Constraint
):
cdef
class
LeafConstraint
(
Constraint
):
cpdef
void
_validate_node
(
self
,
object
instance
)
except
*
:
raise
TypeError
(
f
'TreeValue leaf expected, but node found:
{
os
.
linesep
}{
instance
!
r
}
.'
)
...
...
@@ -194,10 +194,10 @@ cdef class LeafConstraint(BaseConstraint):
cpdef
object
_features
(
self
):
return
None
cpdef
bool
_contains
(
self
,
Base
Constraint
other
):
cpdef
bool
_contains
(
self
,
Constraint
other
):
return
isinstance
(
other
,
LeafConstraint
)
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
return
EmptyConstraint
()
def
__repr__
(
self
):
...
...
@@ -206,7 +206,7 @@ cdef class LeafConstraint(BaseConstraint):
cpdef
inline
LeafConstraint
cleaf
():
return
LeafConstraint
()
cdef
class
TreeConstraint
(
Base
Constraint
):
cdef
class
TreeConstraint
(
Constraint
):
def
__cinit__
(
self
,
dict
constraints
):
self
.
_constraints
=
{
key
:
constraints
[
key
]
for
key
in
sorted
(
constraints
.
keys
())}
...
...
@@ -223,9 +223,9 @@ cdef class TreeConstraint(BaseConstraint):
ft
.
append
((
key
,
self
.
_constraints
[
key
]))
return
tuple
(
ft
)
cpdef
bool
_contains
(
self
,
Base
Constraint
other
):
cpdef
bool
_contains
(
self
,
Constraint
other
):
cdef
str
key
cdef
Base
Constraint
_s_cons
,
_o_cons
cdef
Constraint
_s_cons
,
_o_cons
cdef
list
_f_keys
if
isinstance
(
other
,
TreeConstraint
):
_f_keys
=
sorted
(
set
(
self
.
_constraints
.
keys
())
|
set
(
other
.
_constraints
.
keys
()))
...
...
@@ -239,16 +239,16 @@ cdef class TreeConstraint(BaseConstraint):
else
:
return
False
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
if
key
in
self
.
_constraints
:
return
self
.
_constraints
[
key
]
else
:
return
EmptyConstraint
()
cdef
inline
Base
Constraint
_s_tree_merge
(
list
constraints
):
cdef
inline
Constraint
_s_tree_merge
(
list
constraints
):
cdef
dict
cmap
=
{}
cdef
str
key
cdef
Base
Constraint
cons
cdef
Constraint
cons
cdef
TreeConstraint
tcons
for
tcons
in
constraints
:
for
key
,
cons
in
tcons
.
_constraints
.
items
():
...
...
@@ -269,11 +269,11 @@ cdef inline BaseConstraint _s_tree_merge(list constraints):
else
:
return
EmptyConstraint
()
cdef
inline
Base
Constraint
_s_tree
(
TreeConstraint
constraint
):
cdef
inline
Constraint
_s_tree
(
TreeConstraint
constraint
):
cdef
dict
dcons
=
{}
cdef
list
keys
=
sorted
(
constraint
.
_constraints
.
keys
())
cdef
str
key
cdef
Base
Constraint
cons
,
pcons
cdef
Constraint
cons
,
pcons
for
key
in
keys
:
pcons
=
_s_simplify
(
constraint
.
_constraints
[
key
])
if
not
isinstance
(
pcons
,
EmptyConstraint
):
...
...
@@ -284,12 +284,12 @@ cdef inline BaseConstraint _s_tree(TreeConstraint constraint):
else
:
return
EmptyConstraint
()
cdef
class
CompositeConstraint
(
Base
Constraint
):
cdef
class
CompositeConstraint
(
Constraint
):
def
__cinit__
(
self
,
list
constraints
):
self
.
_constraints
=
tuple
(
sorted
(
constraints
,
key
=
lambda
x
:
repr
(
x
.
_features
())))
cpdef
void
_validate_node
(
self
,
object
instance
)
except
*
:
cdef
Base
Constraint
cons
cdef
Constraint
cons
for
cons
in
self
.
_constraints
:
try
:
cons
.
_validate_node
(
instance
)
...
...
@@ -299,7 +299,7 @@ cdef class CompositeConstraint(BaseConstraint):
raise
_WrappedConstraintException
(
err
,
cons
)
cpdef
void
_validate_value
(
self
,
object
instance
)
except
*
:
cdef
Base
Constraint
cons
cdef
Constraint
cons
for
cons
in
self
.
_constraints
:
try
:
cons
.
_validate_value
(
instance
)
...
...
@@ -311,8 +311,8 @@ cdef class CompositeConstraint(BaseConstraint):
cpdef
object
_features
(
self
):
return
self
.
_constraints
cpdef
bool
_contains
(
self
,
Base
Constraint
other
):
cdef
Base
Constraint
cons
,
pcons
cpdef
bool
_contains
(
self
,
Constraint
other
):
cdef
Constraint
cons
,
pcons
cdef
bool
found_contains
if
isinstance
(
other
,
CompositeConstraint
):
for
pcons
in
other
.
_constraints
:
...
...
@@ -333,24 +333,24 @@ cdef class CompositeConstraint(BaseConstraint):
return
False
cpdef
Base
Constraint
_transaction
(
self
,
str
key
):
cpdef
Constraint
_transaction
(
self
,
str
key
):
return
CompositeConstraint
([
c
.
_transaction
(
key
)
for
c
in
self
.
_constraints
])
cdef
inline
void
_rec_composite_iter
(
Base
Constraint
constraint
,
list
lst
):
cdef
Base
Constraint
cons
cdef
inline
void
_rec_composite_iter
(
Constraint
constraint
,
list
lst
):
cdef
Constraint
cons
if
isinstance
(
constraint
,
CompositeConstraint
):
for
cons
in
constraint
.
_constraints
:
_rec_composite_iter
(
cons
,
lst
)
else
:
lst
.
append
(
constraint
)
cdef
inline
list
_r_composite_iter
(
Base
Constraint
constraint
):
cdef
inline
list
_r_composite_iter
(
Constraint
constraint
):
cdef
list
lst
=
[]
_rec_composite_iter
(
constraint
,
lst
)
return
lst
cdef
inline
Base
Constraint
_s_generic_merge
(
list
constraints
):
cdef
Base
Constraint
gcons
,
cons
,
pcons
cdef
inline
Constraint
_s_generic_merge
(
list
constraints
):
cdef
Constraint
gcons
,
cons
,
pcons
cdef
list
sins
=
[]
cdef
list
trees
=
[]
for
gcons
in
constraints
:
...
...
@@ -361,7 +361,7 @@ cdef inline BaseConstraint _s_generic_merge(list constraints):
elif
not
isinstance
(
pcons
,
EmptyConstraint
):
sins
.
append
(
pcons
)
cdef
Base
Constraint
tree
=
_s_tree_merge
(
trees
)
cdef
Constraint
tree
=
_s_tree_merge
(
trees
)
if
not
isinstance
(
tree
,
EmptyConstraint
):
sins
.
append
(
tree
)
...
...
@@ -391,10 +391,10 @@ cdef inline BaseConstraint _s_generic_merge(list constraints):
else
:
return
CompositeConstraint
(
finals
)
cdef
inline
Base
Constraint
_s_composite
(
CompositeConstraint
constraint
):
cdef
inline
Constraint
_s_composite
(
CompositeConstraint
constraint
):
return
_s_generic_merge
(
list
(
constraint
.
_constraints
))
cdef
inline
BaseConstraint
_s_simplify
(
Base
Constraint
constraint
):
cdef
inline
Constraint
_s_simplify
(
Constraint
constraint
):
if
isinstance
(
constraint
,
CompositeConstraint
):
return
_s_composite
(
constraint
)
elif
isinstance
(
constraint
,
TreeConstraint
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录