Skip to content

Commit e40a28f

Browse files
refai06Ahamed Refai
authored andcommitted
Incorporate comments
Signed-off-by: refai06 <refai.ahamed06@gmail.com>
1 parent 9857c2a commit e40a28f

1 file changed

Lines changed: 34 additions & 20 deletions

File tree

openfl/experimental/workflow/notebooktools/code_analyzer.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,16 @@ def __convert_to_python(self, notebook_path: Path, output_path: Path, export_fil
8989

9090
return Path(output_path).joinpath(export_filename).resolve()
9191

92-
def __extract_user_defined_imports(self, notebook_path) -> List[str]:
92+
def __extract_user_defined_imports(self, notebook_path: Path) -> List[str]:
9393
"""
94-
Extract user-defined imports, excluding inbuild and third-party module
94+
Extract user-defined module imports from the notebook script,
95+
excluding standard library and third-party modules.
9596
9697
Args:
97-
notebook_path: Path to Jupyter notebook.
98-
98+
notebook_path (Path): Path to the Jupyter notebook.
99+
100+
Returns:
101+
List[str]: A list of user-defined module names used in the notebook.
99102
"""
100103
with open(self.script_path, "r") as file:
101104
code = "".join(line for line in file if not line.lstrip().startswith(("!", "%")))
@@ -119,21 +122,24 @@ def __extract_user_defined_imports(self, notebook_path) -> List[str]:
119122

120123
def __copy_user_defined_modules(self, module_names: List[str], notebook_path: Path) -> None:
121124
"""
122-
Copies user-defined modules/packages to the workspace's src directory
125+
Copies user-defined modules/packages to the generated workspace's src directory
123126
124127
Args:
125-
module_names: List of module names.
126-
notebook_path: Path to Jupyter notebook.
128+
module_names (List[str]): A list of user-defined module names
129+
notebook_path (Path): Path to Jupyter notebook.
127130
"""
128131
src_dir = self.script_path.parent
129132
for module_name in module_names:
130-
module_path, module_dir = self._get_module_paths(module_name, notebook_path)
131-
if module_path.exists() and module_path.is_file():
132-
shutil.copy(module_path, src_dir)
133-
print(f"Copied user-defined module: {module_name}.py")
134-
elif module_dir.exists() and module_dir.is_dir():
135-
shutil.copytree(module_dir, src_dir / module_name, dirs_exist_ok=True)
136-
print(f"Copied user-defined directory: {module_name}/")
133+
try:
134+
module_path, module_dir = self._get_module_paths(module_name, notebook_path)
135+
if module_path.exists() and module_path.is_file():
136+
shutil.copy(module_path, src_dir)
137+
print(f"Copied user-defined module: {module_name}.py")
138+
elif module_dir.exists() and module_dir.is_dir():
139+
shutil.copytree(module_dir, src_dir / module_name, dirs_exist_ok=True)
140+
print(f"Copied user-defined directory: {module_name}/")
141+
except Exception as e:
142+
print(f"[WARNING] Failed to copy '{module_name}':{e}")
137143

138144
def __modify_experiment_script(self) -> None:
139145
"""Modifies the given python script by commenting out following code:
@@ -345,26 +351,34 @@ def _clean_value(self, value: str) -> str:
345351

346352
def _is_user_defined_module(self, module_name: str, notebook_path: Path) -> bool:
347353
"""
348-
Check if a module is user-defined
354+
Determine whether a given module is user-defined.
349355
350356
Args:
351-
module_name: Name of the module.
352-
notebook_path: Path to Jupyter notebook.
357+
module_name (str): Name of the module.
358+
notebook_path (Path): Path to Jupyter notebook using the module.
359+
360+
Return:
361+
bool: True if the module is user-defined, False otherwise.
353362
"""
363+
# Reject empty or non-string module names
354364
if not isinstance(module_name, str) or not module_name.strip():
355365
return False
356366

367+
# Expected file path or directory path of the module
357368
module_path, module_dir = self._get_module_paths(module_name, notebook_path)
358369

359370
return (module_path.exists() and module_path.is_file()) or module_dir.exists()
360371

361-
def _get_module_paths(self, module_name: str, notebook_path: Path) -> Tuple:
372+
def _get_module_paths(self, module_name: str, notebook_path: Path) -> Tuple[Path, Path]:
362373
"""
363374
Get the file and directory paths for a user-defined module
364375
365376
Args:
366-
module_name: Name of the module.
367-
notebook_path: Path to the Jupyter notebook.
377+
module_name (str): Name of the module.
378+
notebook_path (Path): Path to the Jupyter notebook.
379+
380+
Returns:
381+
Tuple[Path, Path]: (module_file_path, module_directory_path)
368382
"""
369383
notebook_dir = notebook_path.parent
370384
module_path = notebook_dir / f"{module_name}.py"

0 commit comments

Comments
 (0)