|
37 | 37 |
|
38 | 38 |
|
39 | 39 | class DecoratorBase(Generic[_P, _R]): |
40 | | - """装饰器基类,提供通用装饰器框架 |
| 40 | + """Decorative base class, providing a universal decorative framework. |
41 | 41 |
|
42 | | - 子类只需实现 `process` 方法定义核心逻辑 |
| 42 | + Subclass only needs to implement the 'process' method to define the core logic. |
43 | 43 | """ |
44 | 44 |
|
45 | 45 | def __init__(self, *args: Any, **kwargs: Any) -> None: |
46 | | - """初始化装饰器参数""" |
| 46 | + """Initialize decorator parameters""" |
47 | 47 | self.args = args |
48 | 48 | self.kwargs = kwargs |
49 | 49 |
|
50 | 50 | def __call__(self, func: _DecoratedFunc[_P, _R]) -> _DecoratedFunc[_P, _R]: |
51 | | - """作为装饰器应用的入口点""" |
| 51 | + """As an entry point for decorative applications""" |
52 | 52 |
|
53 | 53 | @functools.wraps(func) |
54 | 54 | def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: |
55 | | - # 预处理参数 |
| 55 | + # Pretreatment parameters |
56 | 56 | processed_args, processed_kwargs = self.process(args, kwargs) |
57 | | - # 调用原函数 |
| 57 | + # Call the original function |
58 | 58 | return func(*processed_args, **processed_kwargs) |
59 | 59 |
|
60 | | - # 保留原始签名 |
| 60 | + # Keep original signature |
61 | 61 | wrapper.__signature__ = inspect.signature(func) |
62 | 62 | return cast("_DecoratedFunc[_P, _R]", wrapper) |
63 | 63 |
|
64 | 64 | def process( |
65 | 65 | self, args: tuple[Any, ...], kwargs: dict[str, Any] |
66 | 66 | ) -> tuple[tuple[Any, ...], dict[str, Any]]: |
67 | | - """子类必须实现的核心处理方法 |
| 67 | + """Core processing methods that subclasses must implement. |
68 | 68 |
|
69 | 69 | Args: |
70 | | - args: 位置参数 |
71 | | - kwargs: 关键字参数 |
| 70 | + args: positional parameter |
| 71 | + kwargs: Keyword Argument |
72 | 72 |
|
73 | 73 | Returns: |
74 | | - 处理后的 (args, kwargs) 元组 |
| 74 | + Processed tuples (args, kwargs) |
75 | 75 | """ |
76 | 76 | raise NotImplementedError("Subclasses must implement this method") |
77 | 77 |
|
78 | 78 |
|
79 | | -# 示例实现:参数别名装饰器 |
| 79 | +# Example implementation: Parameter alias decorator |
80 | 80 | class ParamAliasDecorator(DecoratorBase[_P, _R]): |
81 | | - """参数别名处理的装饰器实现""" |
| 81 | + """Implementation of Decorator for Parameter Alias Processing""" |
82 | 82 |
|
83 | 83 | def __init__(self, alias_mapping: dict[str, Iterable[str]]) -> None: |
84 | 84 | super().__init__() |
|
0 commit comments