9595
9696def load_requirements (path_req : str = PATH_REQ_DEFAULT ) -> list :
9797 """Load the requirements from a file."""
98- with open (path_req ) as fp :
99- req = fp .readlines ()
98+ with open (path_req ) as fopen :
99+ req = fopen .readlines ()
100100 req = [r [: r .index ("#" )] if "#" in r else r for r in req ]
101101 req = [r .strip () for r in req ]
102102 req = [r for r in req if r ]
@@ -252,7 +252,7 @@ def _parse_requirements(folder: str) -> Tuple[str, str]:
252252 for k , v in meta .items ()
253253 if k .startswith (AssistantCLI ._META_PIP_KEY )
254254 }
255- pip_args = ["--extra-index-url https://download.pytorch.org/whl/" + _RUNTIME_VERSIONS .get ("DEVICE" )]
255+ pip_args = ['--find-links=" https://download.pytorch.org/whl/"' + _RUNTIME_VERSIONS .get ("DEVICE" )]
256256 for pip_key in meta_pip_args :
257257 if not isinstance (meta_pip_args [pip_key ], (list , tuple , set )):
258258 meta_pip_args [pip_key ] = [meta_pip_args [pip_key ]]
@@ -335,8 +335,8 @@ def bash_render(folder: str, output_file: str = PATH_SCRIPT_RENDER) -> Optional[
335335 cmd .append (f"git add { pub_ipynb } " )
336336 if not output_file :
337337 return os .linesep .join (cmd )
338- with open (output_file , "w" ) as fp :
339- fp .write (os .linesep .join (cmd ))
338+ with open (output_file , "w" ) as fopen :
339+ fopen .write (os .linesep .join (cmd ))
340340
341341 @staticmethod
342342 def bash_test (folder : str , output_file : str = PATH_SCRIPT_TEST ) -> Optional [str ]:
@@ -386,8 +386,8 @@ def bash_test(folder: str, output_file: str = PATH_SCRIPT_TEST) -> Optional[str]
386386 cmd += ["deactivate" , f"rm -rf { os .path .join (folder , 'venv' )} " ]
387387 if not output_file :
388388 return os .linesep .join (cmd )
389- with open (output_file , "w" ) as fp :
390- fp .write (os .linesep .join (cmd ))
389+ with open (output_file , "w" ) as fopen :
390+ fopen .write (os .linesep .join (cmd ))
391391
392392 @staticmethod
393393 def convert_ipynb (folder : str ) -> None :
@@ -397,8 +397,8 @@ def convert_ipynb(folder: str) -> None:
397397 folder: folder with python script
398398 """
399399 fpath , _ , _ = AssistantCLI ._valid_folder (folder , ext = ".py" )
400- with open (fpath ) as fp :
401- py_script = fp .readlines ()
400+ with open (fpath ) as fopen :
401+ py_script = fopen .readlines ()
402402
403403 meta = AssistantCLI ._load_meta (folder , strict = True )
404404 meta .update (
@@ -414,8 +414,8 @@ def convert_ipynb(folder: str) -> None:
414414
415415 py_script = AssistantCLI ._replace_images (py_script , folder )
416416
417- with open (fpath , "w" ) as fp :
418- fp .writelines (py_script )
417+ with open (fpath , "w" ) as fopen :
418+ fopen .writelines (py_script )
419419
420420 os .system (f'python -m jupytext --set-formats "ipynb,py:percent" { fpath } ' )
421421
@@ -442,8 +442,8 @@ def _replace_images(lines: list, local_dir: str) -> list:
442442 else :
443443 url_path = "/" .join ([URL_PL_DOWNLOAD , local_dir , p_img ])
444444 p_local_img = os .path .join (local_dir , p_img )
445- with open (p_local_img , "rb" ) as fp :
446- im = fp .read ()
445+ with open (p_local_img , "rb" ) as fopen :
446+ im = fopen .read ()
447447 im_base64 = base64 .b64encode (im ).decode ("utf-8" )
448448 _ , ext = os .path .splitext (p_img )
449449 md = md .replace (f'src="{ p_img } "' , f'src="{ url_path } "' )
@@ -489,8 +489,8 @@ def group_folders(
489489 $ python assistant.py group-folders ../target-diff.txt \
490490 --fpath_actual_dirs "['../dirs-main.txt', '../dirs-publication.txt']"
491491 """
492- with open (fpath_gitdiff ) as fp :
493- changed = [ln .strip () for ln in fp .readlines ()]
492+ with open (fpath_gitdiff ) as fopen :
493+ changed = [ln .strip () for ln in fopen .readlines ()]
494494 dirs = [os .path .dirname (ln ) for ln in changed ]
495495 # not empty paths
496496 dirs = [ln for ln in dirs if ln ]
@@ -520,22 +520,23 @@ def group_folders(
520520 raise FileNotFoundError (f"{ msg } nor sub-folder: \n { os .linesep .join (dirs_invalid )} " )
521521
522522 dirs_change = [d for d in dirs_exist if AssistantCLI ._find_meta (d )]
523- with open (fpath_change_folders , "w" ) as fp :
524- fp .write (os .linesep .join (sorted (dirs_change )))
523+ with open (fpath_change_folders , "w" ) as fopen :
524+ fopen .write (os .linesep .join (sorted (dirs_change )))
525525
526526 dirs_drop = [d for d in dirs if not os .path .isdir (d )]
527- with open (fpath_drop_folders , "w" ) as fp :
528- fp .write (os .linesep .join (sorted (dirs_drop )))
527+ with open (fpath_drop_folders , "w" ) as fopen :
528+ fopen .write (os .linesep .join (sorted (dirs_drop )))
529529
530530 @staticmethod
531- def generate_matrix (fpath_change_folders : str ) -> str :
531+ def generate_matrix (fpath_change_folders : str , json_indent : Optional [ int ] = None ) -> str :
532532 """Generate Azure matrix with leaf for each changed notebook.
533533
534534 Args:
535535 fpath_change_folders: output of previous ``group_folders``
536+ json_indent: makes the json more readable, recommendation is 4
536537 """
537- with open (fpath_change_folders ) as fp :
538- folders = [ln .strip () for ln in fp .readlines ()]
538+ with open (fpath_change_folders ) as fopen :
539+ folders = [ln .strip () for ln in fopen .readlines ()]
539540 # set default so the matrix has at least one runner
540541 if not folders :
541542 return ""
@@ -548,7 +549,7 @@ def generate_matrix(fpath_change_folders: str) -> str:
548549 # TODO: allow defining some custom images with with python or PT
549550 "docker-image" : AssistantCLI ._AZURE_DOCKER ,
550551 }
551- return json .dumps (mtx )
552+ return json .dumps (mtx , indent = json_indent )
552553
553554 @staticmethod
554555 def _get_card_item_cell (path_ipynb : str , path_meta : str , path_thumb : Optional [str ]) -> Dict [str , Any ]:
@@ -652,7 +653,12 @@ def copy_notebooks(
652653
653654 @staticmethod
654655 def _copy_notebook (
655- path_ipynb : str , path_root : str , docs_root : str , path_docs_ipynb : str , path_docs_images : str
656+ path_ipynb : str ,
657+ path_root : str ,
658+ docs_root : str ,
659+ path_docs_ipynb : str ,
660+ path_docs_images : str ,
661+ json_indent : Optional [int ] = None ,
656662 ) -> str :
657663 """Copy particular notebook."""
658664 ipynb = path_ipynb .split (os .path .sep )
@@ -678,7 +684,7 @@ def _copy_notebook(
678684 ipynb ["cells" ].append (AssistantCLI ._get_card_item_cell (path_ipynb , path_meta , path_thumb ))
679685
680686 with open (new_ipynb , "w" ) as fopen :
681- json .dump (ipynb , fopen , indent = 4 )
687+ json .dump (ipynb , fopen , indent = json_indent )
682688 return path_ipynb_in_dir
683689
684690 @staticmethod
@@ -691,8 +697,8 @@ def update_env_details(folder: str, base_path: str = DIR_NOTEBOOKS) -> str:
691697 """
692698 meta = AssistantCLI ._load_meta (folder )
693699 # default is COU runtime
694- with open (PATH_REQ_DEFAULT ) as fp :
695- req = fp .readlines ()
700+ with open (PATH_REQ_DEFAULT ) as fopen :
701+ req = fopen .readlines ()
696702 req += meta .get ("requirements" , [])
697703 req = [r .strip () for r in req ]
698704
0 commit comments