Skip to content

Commit

Permalink
wip: nnbench fixtures
Browse files Browse the repository at this point in the history
Raw callables only, collected by module lookup.
  • Loading branch information
nicholasjng committed Nov 25, 2024
1 parent 2258bd4 commit 6214b74
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/nnbench/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os
from pathlib import Path
from typing import Any


class FixtureManager:
"""
A lean class responsible for resolving parameter values (aka 'fixtures')
of benchmarks from provider functions.
To resolve a benchmark parameter (in resolve()), the class does
the following:
1. Obtain the path to the file containing the benchmark, as
the __file__ attribute of the benchmark function's origin module.
2. Look for a `conf.py` file in the same directory.
3. Import the `conf.py` module, look for a function named the same as
the benchmark parameter.
4. If necessary, resolve any named inputs to the function **within**
the module scope.
5. If no function member is found, and the benchmark file is not in `root`,
fall back to the parent directory, repeat steps 2-5, until `root` is reached.
6. If no `conf.py` contains any function matching the name, throw an
error (TODO: ImportError? custom?)
"""

def __init__(self, root: str | os.PathLike[str]) -> None:
self.root = Path(root)
self.cache: dict[str, Any] = {}
"""
Cache architecture:
key: directory
value: key-value mapping of fixture name -> fixture value within directory.
"""

def resolve(self):
pass
2 changes: 1 addition & 1 deletion src/nnbench/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def modulename(file: str | os.PathLike[str]) -> str:


def import_file_as_module(file: str | os.PathLike[str]) -> ModuleType:
fpath = Path(file).resolve() # Python module __file__ paths are absolute.
fpath = Path(file)
if not fpath.is_file() or fpath.suffix != ".py":
raise ValueError(f"path {str(file)!r} is not a Python file")

Expand Down

0 comments on commit 6214b74

Please sign in to comment.