From 401b495f597ed5a553234f82d3330aa668cc1dfa Mon Sep 17 00:00:00 2001 From: HansBug Date: Sat, 1 Jan 2022 11:23:56 +0800 Subject: [PATCH] dev(hansbug): add delayed for subside and union --- treevalue/tree/func/cfunc.pyx | 10 ++++++---- treevalue/tree/tree/structural.pxd | 4 ++-- treevalue/tree/tree/structural.pyx | 18 +++++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/treevalue/tree/func/cfunc.pyx b/treevalue/tree/func/cfunc.pyx index c59cf7ad92..47cc69a148 100644 --- a/treevalue/tree/func/cfunc.pyx +++ b/treevalue/tree/func/cfunc.pyx @@ -168,8 +168,8 @@ cdef object _c_func_treelize_run(object func, list args, dict kwargs, _e_tree_mo return TreeStorage(_d_res) def _w_subside_func(object value, bool dict_=True, bool list_=True, bool tuple_=True, bool inherit=True, - object mode='strict', object missing=MISSING_NOT_ALLOW): - return _c_subside(value, dict_, list_, tuple_, inherit, mode, missing)[0] + object mode='strict', object missing=MISSING_NOT_ALLOW, bool delayed=False): + return _c_subside(value, dict_, list_, tuple_, inherit, mode, missing, delayed)[0] def _w_rise_func(object tree, bool dict_=True, bool list_=True, bool tuple_=True, object template=None): return _c_rise(tree, dict_, list_, tuple_, template) @@ -181,9 +181,11 @@ def _w_func_treelize_run(*args, object __w_func, _e_tree_mode __w_mode, object _ cdef list _a_args = [(item._detach() if isinstance(item, TreeValue) else item) for item in args] cdef dict _a_kwargs = {k: (v._detach() if isinstance(v, TreeValue) else v) for k, v in kwargs.items()} + cdef dict _w_subside_cfg if __w_subside is not None: - _a_args = [_w_subside_func(item, **__w_subside) for item in _a_args] - _a_kwargs = {key: _w_subside_func(value, **__w_subside) for key, value in _a_kwargs.items()} + _w_subside_cfg = {'delayed': __w_delayed, **__w_subside} + _a_args = [_w_subside_func(item, **_w_subside_cfg) for item in _a_args] + _a_kwargs = {key: _w_subside_func(value, **_w_subside_cfg) for key, value in _a_kwargs.items()} cdef object _st_res = _c_func_treelize_run(__w_func, _a_args, _a_kwargs, __w_mode, __w_inherit, __w_allow_missing, __w_missing_func, __w_delayed) diff --git a/treevalue/tree/tree/structural.pxd b/treevalue/tree/tree/structural.pxd index a84eab61ed..3fa1204e0a 100644 --- a/treevalue/tree/tree/structural.pxd +++ b/treevalue/tree/tree/structural.pxd @@ -12,10 +12,10 @@ cdef object _c_subside_process(tuple value, object it) cdef tuple _c_subside_build(object value, bool dict_, bool list_, bool tuple_) cdef void _c_subside_missing() cdef object _c_subside(object value, bool dict_, bool list_, bool tuple_, bool inherit, - object mode, object missing) + object mode, object missing, bool delayed) cdef object _c_subside_keep_type(object t) cpdef object subside(object value, bool dict_= *, bool list_= *, bool tuple_= *, - object return_type= *, bool inherit= *, object mode= *, object missing= *) + object return_type= *, bool inherit= *, object mode= *, object missing= *, bool delayed= *) cdef object _c_rise_tree_builder(tuple p, object it) cdef tuple _c_rise_tree_process(object t) diff --git a/treevalue/tree/tree/structural.pyx b/treevalue/tree/tree/structural.pyx index 3d67cf72d7..9d30b6397f 100644 --- a/treevalue/tree/tree/structural.pyx +++ b/treevalue/tree/tree/structural.pyx @@ -84,7 +84,7 @@ cdef inline void _c_subside_missing(): pass cdef object _c_subside(object value, bool dict_, bool list_, bool tuple_, bool inherit, - object mode, object missing): + object mode, object missing, bool delayed): cdef object builder, _i_args, _i_types builder, _i_args, _i_types = _c_subside_build(value, dict_, list_, tuple_) cdef list args = list(_i_args) @@ -94,7 +94,7 @@ cdef object _c_subside(object value, bool dict_, bool list_, bool tuple_, bool i allow_missing, missing_func = _c_missing_process(missing) return _c_func_treelize_run(_SubsideCall(builder), args, {}, - _c_load_mode(mode), inherit, allow_missing, missing_func, False), _i_types + _c_load_mode(mode), inherit, allow_missing, missing_func, delayed), _i_types cdef inline object _c_subside_keep_type(object t): return t @@ -102,7 +102,7 @@ cdef inline object _c_subside_keep_type(object t): @cython.binding(True) cpdef object subside(object value, bool dict_=True, bool list_=True, bool tuple_=True, object return_type=None, bool inherit=True, - object mode='strict', object missing=MISSING_NOT_ALLOW): + object mode='strict', object missing=MISSING_NOT_ALLOW, bool delayed=False): """ Overview: Drift down the structures (list, tuple, dict) down to the tree's value. @@ -119,6 +119,8 @@ cpdef object subside(object value, bool dict_=True, bool list_=True, bool tuple_ - mode (:obj:`str`): Mode of the wrapping, default is `strict`. - missing (:obj:`Union[Any, Callable]`): Missing value or lambda generator of when missing, \ default is `MISSING_NOT_ALLOW`, which means raise `KeyError` when missing detected. + - delayed (:obj:`bool`): Enable delayed mode or not, the calculation will be delayed when enabled, \ + default is ``False``, which means to all the calculation at once. Returns: - return (:obj:`_TreeValue`): Subsided tree value. @@ -143,7 +145,7 @@ cpdef object subside(object value, bool dict_=True, bool list_=True, bool tuple_ >>> #}), all structures above the tree values are subsided to the bottom of the tree. """ cdef object result, _i_types - result, _i_types = _c_subside(value, dict_, list_, tuple_, inherit, mode, missing) + result, _i_types = _c_subside(value, dict_, list_, tuple_, inherit, mode, missing, delayed) cdef object type_ cdef set types @@ -162,7 +164,7 @@ cpdef object subside(object value, bool dict_=True, bool list_=True, bool tuple_ @cython.binding(True) def union(*trees, object return_type=None, bool inherit=True, - object mode='strict', object missing=MISSING_NOT_ALLOW): + object mode='strict', object missing=MISSING_NOT_ALLOW, bool delayed=False): """ Overview: Union tree values together. @@ -170,10 +172,12 @@ def union(*trees, object return_type=None, bool inherit=True, Arguments: - trees (:obj:`_TreeValue`): Tree value objects. - return_type (:obj:`Optional[Type[_ClassType]]`): Return type of the wrapped function, default is `TreeValue`. - - inherit (:obj:`bool`): Allow inherit in wrapped function, default is `True`. + - inherit (:obj:`bool`): Allow inheriting in wrapped function, default is `True`. - mode (:obj:`str`): Mode of the wrapping, default is `strict`. - missing (:obj:`Union[Any, Callable]`): Missing value or lambda generator of when missing, \ default is `MISSING_NOT_ALLOW`, which means raise `KeyError` when missing detected. + - delayed (:obj:`bool`): Enable delayed mode or not, the calculation will be delayed when enabled, \ + default is ``False``, which means to all the calculation at once. Returns: - result (:obj:`TreeValue`): Unionised tree value. @@ -184,7 +188,7 @@ def union(*trees, object return_type=None, bool inherit=True, >>> union(t, tx) # TreeValue({'a': (1, True), 'b': (2, False), 'x': {'c': (3, True), 'd': (4, False)}}) """ cdef object result, _i_types - result, _i_types = _c_subside(tuple(trees), True, True, True, inherit, mode, missing) + result, _i_types = _c_subside(tuple(trees), True, True, True, inherit, mode, missing, delayed) cdef object type_ cdef list types -- GitLab