[docs]defrun_python_file(path:Path)->None:""" Executes a Python file from a path. Args: path: File path Returns: None """sys.path.append(str(path.parent))__import__(path.with_suffix("").name)
[docs]defrun_all_python_files(path:Path,recursive=True,verbose=True)->None:""" Executes all Python files and Jupyter Notebooks in a directory. Args: path: A Path-type object (Path from built-in pathlib) representing a filepath recursive: Executes recursively (e.g. searches all subfolders too) Returns: None """# Exclusions:ifpath==Path(os.path.abspath(__file__)):# Don't run this filereturnif("ignore"instr(path).lower()):# Don't run any file or folder with the word "ignore" in the name.returnifpath.is_file():### Run the file if it's a Python fileifpath.suffix==".py":ifverbose:print(f"##### Running file: {path}")run_python_file(path)### Run the file if it's a Jupyter notebookifpath.suffix==".ipynb":ifverbose:print(f"##### Converting file: {path}")notebook=pathpython_file=path.with_suffix(".py")convert_ipynb_to_py(notebook,python_file)run_all_python_files(python_file,recursive=False,verbose=verbose)elifpath.is_dir()andrecursive:ifverbose:print(f"##### Opening directory: {path}")forsubpathinpath.iterdir():run_all_python_files(subpath,recursive=recursive,verbose=verbose)