用 Python 装饰器语法自动注册
| 683 | print(" ──────────────────") |
| 684 | |
| 685 | class DecoratorRegistry: |
| 686 | """用 Python 装饰器语法自动注册""" |
| 687 | def __init__(self): |
| 688 | self._handlers: dict[str, Callable] = {} |
| 689 | |
| 690 | def tool(self, name: str): |
| 691 | """装饰器: @registry.tool("bash")""" |
| 692 | def decorator(func): |
| 693 | self._handlers[name] = func |
| 694 | return func |
| 695 | return decorator |
| 696 | |
| 697 | def execute(self, name: str, inp: str) -> str: |
| 698 | return self._handlers[name](inp) |
| 699 | |
| 700 | r = DecoratorRegistry() |
| 701 |
no outgoing calls
no test coverage detected