From 38760ba8cef218c66dd369bb7aedfbf6b9c221cf Mon Sep 17 00:00:00 2001 From: tanay Date: Sat, 29 Aug 2026 00:33:50 +0530 Subject: [PATCH] adding a big feature for the plugin registry, making plugins usable --- leanpass/__init__.py | 3 ++- leanpass/ops.py | 8 ++++++++ leanpass/plugin_registry.py | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 leanpass/ops.py create mode 100644 leanpass/plugin_registry.py diff --git a/leanpass/__init__.py b/leanpass/__init__.py index 850094d..7447d00 100644 --- a/leanpass/__init__.py +++ b/leanpass/__init__.py @@ -1,5 +1,6 @@ from .tensor import Tensor from . import nn from . import optim +from . import ops -__all__ = ["Tensor", "nn", "optim"] +__all__ = ["Tensor", "nn", "optim", "ops"] diff --git a/leanpass/ops.py b/leanpass/ops.py new file mode 100644 index 0000000..e9c7ff1 --- /dev/null +++ b/leanpass/ops.py @@ -0,0 +1,8 @@ +from .plugin_registry import get_op + +def __getattr__(name: str): + try: + return get_op(name) + except KeyError: + raise AttributeError(f"module '{__name__}' has no attribute '{name}'") + diff --git a/leanpass/plugin_registry.py b/leanpass/plugin_registry.py new file mode 100644 index 0000000..6c7fa1f --- /dev/null +++ b/leanpass/plugin_registry.py @@ -0,0 +1,24 @@ +from typing import Callable, Any, Dict + +# The central registry holding all ops (both built-in and plugins) +_REGISTRY: Dict[str, Callable] = {} + +def register_op(name: str, fn: Callable): + """Registers a function into LeanPass.""" + if name in _REGISTRY: + pass + _REGISTRY[name] = fn + +def op(name: str): + """Decorator for registering ops.""" + def decorator(fn): + register_op(name, fn) + return fn + return decorator + +def get_op(name: str) -> Callable: + """Retrieve an op by name.""" + if name not in _REGISTRY: + raise KeyError(f"Op '{name}' not found. Did you install its plugin?") + return _REGISTRY[name] +