use_scope(name=None, clear=False) Open or enter a scope. Can be used as context manager and decorator. See :ref:`User manual - use_scope() ` :param str name: Scope name. If it is None, a globally unique scope name is generated. (When used as context manager, the con
(name: str = None, clear: bool = False, **kwargs)
| 2097 | |
| 2098 | |
| 2099 | def use_scope(name: str = None, clear: bool = False, **kwargs): |
| 2100 | """use_scope(name=None, clear=False) |
| 2101 | |
| 2102 | Open or enter a scope. Can be used as context manager and decorator. |
| 2103 | |
| 2104 | See :ref:`User manual - use_scope() <use_scope>` |
| 2105 | |
| 2106 | :param str name: Scope name. If it is None, a globally unique scope name is generated. |
| 2107 | (When used as context manager, the context manager will return the scope name) |
| 2108 | :param bool clear: Whether to clear the contents of the scope before entering the scope. |
| 2109 | |
| 2110 | :Usage: |
| 2111 | |
| 2112 | :: |
| 2113 | |
| 2114 | with use_scope(...) as scope_name: |
| 2115 | put_xxx() |
| 2116 | |
| 2117 | @use_scope(...) |
| 2118 | def app(): |
| 2119 | put_xxx() |
| 2120 | |
| 2121 | """ |
| 2122 | # For backward compatible |
| 2123 | # :param bool create_scope: Whether to create scope when scope does not exist. |
| 2124 | # :param scope_params: Extra parameters passed to `set_scope()` when need to create scope. |
| 2125 | # Only available when ``create_scope=True``. |
| 2126 | create_scope = kwargs.pop('create_scope', True) |
| 2127 | scope_params = kwargs |
| 2128 | |
| 2129 | if name is None: |
| 2130 | name = random_str(10) |
| 2131 | check_dom_name_value(name, 'scope name') |
| 2132 | |
| 2133 | def before_enter(): |
| 2134 | if create_scope: |
| 2135 | if_exist = 'blank' if clear else None |
| 2136 | set_scope(name, if_exist=if_exist, **scope_params) # lock the height of the scope and clear its content |
| 2137 | |
| 2138 | return use_scope_(name=name, before_enter=before_enter) |
| 2139 | |
| 2140 | |
| 2141 | class use_scope_: |
no test coverage detected
searching dependent graphs…