Implementation:173TECH Sayn PythonLoader
| Knowledge Sources | |
|---|---|
| Domains | Python, Module_Loading |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for dynamically loading Python modules from the project python/ directory and discovering task classes and decorator-wrapped functions provided by the SAYN framework.
Description
The PythonLoader class handles dynamic Python module loading using importlib:
register_module(key, folder): Registers a folder assayn_{key}insys.modulesusingSourceFileLoader, executing the module's__init__.pyget_class(module_key, class_path): Imports a submodule and retrieves a class by dotted path (for class-basedPythonTasksubclasses)get_objects(module_key, module_str, type_filter): Imports a submodule and usesinspect.getmembersto find objects of a specific type (for@task-decorated functions viaDecoratorTaskWrapper)
The module is registered as sayn_python_tasks in sys.modules, allowing import statements like from sayn_python_tasks.load_data import LoadDataTask.
Usage
Used internally by App.start_app() to register the python/ folder, and by get_tasks_dict() to discover decorator-based tasks. Also used by App.get_task_class() for class-based Python tasks.
Code Reference
Source Location
- Repository: SAYN
- File:
sayn/utils/python_loader.py - Lines: L9-96 (
PythonLoaderclass)
Signature
class PythonLoader:
modules: list
def has_module(self, key: str) -> bool
def register_module(self, key: str, folder: str) -> Result
def get_class(self, module_key: str, class_path: str) -> Result[type]
def get_objects(self, module_key: str, module_str: str, type_filter=None) -> Result[List]
Import
from sayn.utils.python_loader import PythonLoader
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | str | Yes Module key (e.g., "python_tasks") | |
| folder | str | Yes Path to the Python folder containing __init__.py
| |
| class_path | str | Yes (get_class) Dotted path to the class (e.g., "load_data.LoadDataTask") | |
| type_filter | type | No (get_objects) Type to filter members by (e.g., DecoratorTaskWrapper)
|
Outputs
| Name | Type | Description |
|---|---|---|
| get_class returns | Result[type] The loaded Python class | |
| get_objects returns | Result[List] List of objects matching the type filter |
Usage Examples
Registering and Discovering Tasks
from sayn.utils.python_loader import PythonLoader
loader = PythonLoader()
# Register the python/ folder
result = loader.register_module("python_tasks", "python")
if result.is_err:
print(f"Failed: {result.error}")
# Discover @task-decorated functions
result = loader.get_objects(
"python_tasks",
"load_data", # module path within python/
type_filter=DecoratorTaskWrapper,
)
if result.is_ok:
for task_wrapper in result.value:
print(f"Found task: {task_wrapper.func.__name__}")
# Load a class-based task
result = loader.get_class("python_tasks", "custom_etl.CustomETL")
if result.is_ok:
CustomETL = result.value