Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:173TECH Sayn PythonLoader

From Leeroopedia
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 as sayn_{key} in sys.modules using SourceFileLoader, executing the module's __init__.py
  • get_class(module_key, class_path): Imports a submodule and retrieves a class by dotted path (for class-based PythonTask subclasses)
  • get_objects(module_key, module_str, type_filter): Imports a submodule and uses inspect.getmembers to find objects of a specific type (for @task-decorated functions via DecoratorTaskWrapper)

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 (PythonLoader class)

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

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment