|
|
@ -22,6 +22,21 @@ def cell_attr_register(fn=None, attrs=None):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Cell init attributes register.
|
|
|
|
Cell init attributes register.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
fn (function, optional): The __init__ function of the cell. Defaults to None.
|
|
|
|
|
|
|
|
attrs (list(string) | string, optional): A list of attributes to register.
|
|
|
|
|
|
|
|
Can be a list of strings or a single string. Defaults to None.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
function: The original function wrapped with attribute registration.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
该函数用于注册cell类的初始化属性。
|
|
|
|
|
|
|
|
通过装饰器模式,将cell类的__init__函数的参数保存为operator的属性。
|
|
|
|
|
|
|
|
如果未提供fn参数,则返回装饰器函数wrap_cell,否则返回包装后的__init__函数。
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Cell init attributes register.
|
|
|
|
|
|
|
|
|
|
|
|
Registering the decorator of the built-in operator cell __init__
|
|
|
|
Registering the decorator of the built-in operator cell __init__
|
|
|
|
function will add save all the parameters of __init__ as operator attributes.
|
|
|
|
function will add save all the parameters of __init__ as operator attributes.
|
|
|
|
|
|
|
|
|
|
|
@ -34,8 +49,38 @@ def cell_attr_register(fn=None, attrs=None):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def wrap_cell(fn):
|
|
|
|
def wrap_cell(fn):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
装饰器函数,用于记录类的初始化参数。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
fn (function): 需要被装饰的函数。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
function: 返回一个新的函数,该函数在调用时会记录传递给fn函数的参数。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
@wraps(fn)
|
|
|
|
@wraps(fn)
|
|
|
|
def deco(self, *args, **kwargs):
|
|
|
|
def deco(self, *args, **kwargs):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
这是一个装饰器函数,用于记录类的初始化参数。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
self: 类实例对象。
|
|
|
|
|
|
|
|
*args: 传递给被装饰函数的可变位置参数。
|
|
|
|
|
|
|
|
**kwargs: 传递给被装饰函数的可变关键字参数。
|
|
|
|
|
|
|
|
attrs: 可选参数,指定要记录的属性。可以是字符串或字符串列表。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
|
|
|
ValueError: 如果attrs不是字符串或字符串列表,或者attrs中的元素不是字符串时抛出。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
该函数的主要作用是在类实例初始化时,记录传递给__init__方法的参数。
|
|
|
|
|
|
|
|
如果attrs为None,则记录所有传递给__init__方法的参数(不包括self)。
|
|
|
|
|
|
|
|
如果attrs为字符串或字符串列表,则只记录指定的属性。
|
|
|
|
|
|
|
|
记录的参数将被保存为实例的cell_init_args属性,格式为"类名+参数列表"。
|
|
|
|
|
|
|
|
"""
|
|
|
|
arguments = []
|
|
|
|
arguments = []
|
|
|
|
if attrs is None:
|
|
|
|
if attrs is None:
|
|
|
|
bound_args = inspect.signature(fn).bind(self, *args, **kwargs)
|
|
|
|
bound_args = inspect.signature(fn).bind(self, *args, **kwargs)
|
|
|
|