Skip to content

Commit c3d737f

Browse files
committed
add dynamic input hook registration
Signed-off-by: Maria Teresa Ortega <teresa.ortega0903@gmail.com>
1 parent 69d13c0 commit c3d737f

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

src/lambkin/core/decorators/benchmark.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from lambkin.core.ctx.context import Context
3232
from lambkin.core.ctx.source import Source
33+
from lambkin.core.decorators.input import InputRegistry
3334

3435

3536
def _parse_options(fn, cli_args):
@@ -70,6 +71,8 @@ def benchmark(variants, num_iterations):
7071
)
7172

7273
def decorator(fn):
74+
inputs = InputRegistry()
75+
7376
@functools.wraps(fn)
7477
def wrapper(args=None, output_dir=None):
7578
cli_args = sys.argv[1:] if args is None else args
@@ -85,8 +88,10 @@ def wrapper(args=None, output_dir=None):
8588
variant_index=variant_index,
8689
output_dir=output_dir,
8790
)
91+
inputs.resolve(ctx)
8892
fn(ctx)
8993

94+
wrapper.input = inputs.register
9095
return wrapper
9196

9297
return decorator

src/lambkin/core/decorators/input.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,25 @@
1414

1515
"""Input decorator for lambkin."""
1616

17-
import functools
1817

18+
class InputRegistry:
19+
"""Manages the registration and resolution of input hooks for a benchmark."""
1920

20-
def input(hook):
21-
"""Wrap a benchmark function to inject an input into ``ctx.inputs``.
21+
def __init__(self):
22+
"""Initialize the InputRegistry."""
23+
self._hooks = []
2224

23-
Uses :func:`inspect.getfile` to read the hook name and injects
24-
the return value into ``ctx.inputs.<name>`` before the benchmark runs.
25+
def register(self, hook_fn):
26+
"""Decorator used to register a function as an input provider."""
27+
self._hooks.append(hook_fn)
28+
return hook_fn
2529

26-
Parameters
27-
----------
28-
hook : callable
29-
Input resolver function. Must accept a single ``ctx`` argument
30-
and return the input value to inject into ``ctx.inputs``.
31-
"""
30+
def resolve(self, ctx):
31+
"""Resolve all registered input hooks.
3232
33-
def decorator(benchmark_fn):
34-
@functools.wraps(benchmark_fn)
35-
def wrapper(ctx):
36-
setattr(ctx.inputs, hook.__name__, hook(ctx))
37-
return benchmark_fn(ctx)
38-
39-
wrapper.__wrapped__ = benchmark_fn
40-
return wrapper
41-
42-
return decorator
33+
Executes all registered hooks and dynamically maps their return values
34+
to the "ctx.inputs" namespace using the original function's name.
35+
"""
36+
for hook in self._hooks:
37+
result = hook(ctx)
38+
setattr(ctx.inputs, hook.__name__, result)

0 commit comments

Comments
 (0)