Skip to content

Commit

Permalink
Replace ExecuteOptions with argparse.Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
frankharkins committed Mar 22, 2024
1 parent 432fc1f commit b2b888b
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions scripts/nb-tester/test-notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
def matches(path: Path, glob_list: list[str]) -> bool:
return any(path.match(glob) for glob in glob_list)

def filter_paths(paths: list[Path], submit_jobs: bool, only_submit_jobs: bool) -> Iterator[Path]:
def filter_paths(paths: list[Path], args: argparse.Namespace) -> Iterator[Path]:
"""
Filter out any paths we don't want to run, printing messages.
"""
if only_submit_jobs:
submit_jobs = True
submit_jobs = args.submit_jobs or args.only_submit_jobs
for path in paths:
if path.suffix != ".ipynb":
print(f"ℹ️ Skipping {path}; file is not `.ipynb` format.")
Expand All @@ -62,7 +61,7 @@ def filter_paths(paths: list[Path], submit_jobs: bool, only_submit_jobs: bool) -
)
continue

if only_submit_jobs and not matches(path, NOTEBOOKS_THAT_SUBMIT_JOBS):
if args.only_submit_jobs and not matches(path, NOTEBOOKS_THAT_SUBMIT_JOBS):
print(
f"ℹ️ Skipping {path} as it does not submit jobs and the --only-submit-jobs flag is set."
)
Expand All @@ -71,12 +70,6 @@ def filter_paths(paths: list[Path], submit_jobs: bool, only_submit_jobs: bool) -
yield path


@dataclass(frozen=True)
class ExecuteOptions:
write: bool
submit_jobs: bool


@dataclass(frozen=True)
class NotebookWarning:
cell_index: int
Expand Down Expand Up @@ -120,7 +113,7 @@ def extract_warnings(notebook: nbformat.NotebookNode) -> list[NotebookWarning]:
return notebook_warnings


def execute_notebook(path: Path, options: ExecuteOptions) -> bool:
def execute_notebook(path: Path, args: argparse.Namespace) -> bool:
"""
Wrapper function for `_execute_notebook` to print status
"""
Expand All @@ -130,7 +123,7 @@ def execute_notebook(path: Path, options: ExecuteOptions) -> bool:
nbclient.exceptions.CellTimeoutError,
)
try:
nb = _execute_notebook(path, options)
nb = _execute_notebook(path, args)
except possible_exceptions as err:
print("\r\n")
print(err)
Expand All @@ -146,21 +139,22 @@ def execute_notebook(path: Path, options: ExecuteOptions) -> bool:
return True


def _execute_notebook(filepath: Path, options: ExecuteOptions) -> nbformat.NotebookNode:
def _execute_notebook(filepath: Path, args: argparse.Namespace) -> nbformat.NotebookNode:
"""
Use nbconvert to execute notebook
"""
submit_jobs = args.submit_jobs or args.only_submit_jobs
nb = nbformat.read(filepath, as_version=4)

processor = nbconvert.preprocessors.ExecutePreprocessor(
# If submitting jobs, we want to wait forever (-1 means no timeout)
timeout=-1 if options.submit_jobs else 100,
timeout=-1 if submit_jobs else 100,
kernel_name="python3",
)

processor.preprocess(nb)

if not options.write:
if not args.write:
return nb

for cell in nb.cells:
Expand Down Expand Up @@ -252,16 +246,12 @@ def create_argument_parser() -> argparse.ArgumentParser:
if __name__ == "__main__":
args = create_argument_parser().parse_args()
paths = map(Path, args.filenames or find_notebooks())
filtered_paths = filter_paths(paths, submit_jobs=args.submit_jobs, only_submit_jobs=args.only_submit_jobs)
filtered_paths = filter_paths(paths, args)

# Execute notebooks
start_time = datetime.now()
execute_options = ExecuteOptions(
write=args.write,
submit_jobs=args.submit_jobs or args.only_submit_jobs
)
print("Executing notebooks:")
results = [execute_notebook(path, execute_options) for path in filtered_paths]
results = [execute_notebook(path, args) for path in filtered_paths]
print("Checking for trailing jobs...")
results.append(cancel_trailing_jobs(start_time))
if not all(results):
Expand Down

0 comments on commit b2b888b

Please sign in to comment.