77import sys
88from importlib import import_module
99from pathlib import Path
10- from typing import Any , Dict , List , Optional , Tuple
10+ from typing import Any , Dict , List , Optional
1111
1212import nbformat
1313from nbdev .export import nb_export
@@ -18,8 +18,9 @@ class CodeAnalyzer:
1818 Provides code extraction and transformation functionality
1919
2020 Attributes:
21- script_path (Path): Absolute path to the python script generated.
2221 script_name (str): Name of the generated python script.
22+ script_path (Path): Absolute path to the python script generated.
23+ requirements (List[str]): List of pip libraries found in the script.
2324 exported_script_module (ModuleType): The imported module object of the generated script.
2425 available_modules_in_exported_script (list): List of available attributes in the
2526 exported script.
@@ -43,6 +44,7 @@ def __init__(self, notebook_path: Path, output_path: Path) -> None:
4344 f"{ self .script_name } .py" ,
4445 )
4546 ).resolve ()
47+ self .requirements = self ._get_requirements ()
4648 self .__modify_experiment_script ()
4749
4850 def __get_exp_name (self , notebook_path : Path ) -> str :
@@ -292,41 +294,25 @@ def _clean_value(self, value: str) -> str:
292294 value = value .lstrip ("[" ).rstrip ("]" )
293295 return value
294296
295- def get_requirements (self ) -> Tuple [ List [str ], List [ int ], List [ str ] ]:
297+ def _get_requirements (self ) -> List [str ]:
296298 """Extract pip libraries from the script
297299
298300 Returns:
299- tuple: A tuple containing:
300- requirements (list of str): List of pip libraries found in the script.
301- line_nos (list of int): List of line numbers where "pip install" commands are found.
302- data (list of str): The entire script data as a list of lines.
301+ requirements (List[str]): List of pip libraries found in the script.
303302 """
304303 data = None
305304 with self .script_path .open ("r" ) as f :
306305 requirements = []
307- line_nos = []
308306 data = f .readlines ()
309- for i , line in enumerate (data ):
307+ for _ , line in enumerate (data ):
310308 line = line .strip ()
311309 if "pip install" in line :
312- line_nos .append (i )
313310 # Avoid commented lines, libraries from *.txt file, or openfl.git
314311 # installation
315312 if not line .startswith ("#" ) and "-r" not in line and "openfl.git" not in line :
316313 requirements .append (f"{ line .split (' ' )[- 1 ].strip ()} \n " )
317314
318- return requirements , line_nos , data
319-
320- def remove_lines (self , data : List [str ], line_nos : List [int ]) -> None :
321- """Removes pip install lines from the script
322- Args:
323- data (List[str]): The entire script data as a list of lines.
324- line_nos (List[int]): List of line numbers where "pip install" commands are found.
325- """
326- with self .script_path .open ("w" ) as f :
327- for i , line in enumerate (data ):
328- if i not in line_nos :
329- f .write (line )
315+ return requirements
330316
331317 def get_flow_class_details (self , parent_class ) -> Dict [str , Any ]:
332318 """
0 commit comments