def test_should_find_consistency_between_notebook_and_script_with_diff_empty_line(conf, work_dir): """ Test there is consistency between a notebook and a script. Ignore empty_lines """ notebook_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None, cells=[('code', 'print(\'poney\')'), ('comment', '# This is a comment'), ('code', 'def test():\n' ' """This is a docstring"""\n' ' print("hello")\n')]) script_content = ''' #!/usr/bin/env python3 from typing import List import argparse def mlvtools_test(): print('poney') # # This is a comment def test(): """This is a docstring""" print("hello") if __name__ == '__main__': parser = argparse.ArgumentParser(description='Command for script mlvtools_test') args = parser.parse_args() mlvtools_test() ''' script_path = join(work_dir, 'script.py') write_python_script(script_content, script_path) assert compare(notebook_path, script_path, conf)
def test_should_raise_if_notebook_is_not_a_file(conf, work_dir): """ Test raises if provided notebook is not a file """ script_path = join(work_dir, 'script.py') write_python_script('import os', script_path) with pytest.raises(MlVToolException) as e: compare(join(work_dir, 'does_not_exist.ipynb'), script_path, conf) assert isinstance(e.value.__cause__, IOError)
def test_should_create_directory_to_write_formatted_python_script(work_dir): """ Test create directory and write formatted and executable python script """ script_content = 'my_var=4' script_path = join(work_dir, 'a_dir', 'test.py') write_python_script(script_content, script_path) assert exists(join(work_dir, 'a_dir')) assert exists(script_path)
def test_write_python_script_should_raise_if_python_syntax_error(work_dir): """ Test write_python_script raise an MlVToolException if python syntax error """ script_content = 'my_var :=: 4' script_path = join(work_dir, 'test.py') with pytest.raises(MlVToolException) as e: write_python_script(script_content, script_path) assert isinstance(e.value.__cause__, SyntaxError)
def test_write_python_script_should_raise_if_cannot_write_executable_script( work_dir, mocker): """ Test write_python_script raise an MlVToolException if can not write executable output """ script_content = 'my_var=4\nmy_list=[1,2,3]' script_path = join(work_dir, 'test.py') mocker.patch('builtins.open', side_effect=IOError) with pytest.raises(MlVToolException) as e: write_python_script(script_content, script_path) assert isinstance(e.value.__cause__, IOError)
def test_should_write_formatted_python_script(work_dir): """ Test write formatted and executable python script """ script_content = 'my_var=4\nmy_list=[1,2,3]' script_path = join(work_dir, 'test.py') write_python_script(script_content, script_path) assert exists(script_path) assert stat.S_IMODE(os_stat(script_path).st_mode) == 0o755 with open(script_path, 'r') as fd: assert fd.read() == 'my_var = 4\nmy_list = [1, 2, 3]\n'
def export_to_script(input_notebook_path: str, output_path: str, conf: MlVToolConf): """ Export a notebook to a parameterize Python 3 script using Jinja templates """ logging.info( f'Generate Python script {output_path} from Jupyter Notebook {input_notebook_path}' ) logging.debug(f'Global Configuration: {conf}') logging.debug(f'Template path {TEMPLATE_PATH}') script_content = get_converted_script(input_notebook_path, conf) if not script_content: logging.warning('Empty notebook provided. Nothing to do.') return write_python_script(script_content, output_path) logging.log( logging.WARNING + 1, f'Python script successfully generated in {abspath(output_path)}')
def export_to_script(input_notebook_path: str, output_path: str, conf: MlVToolConf): """ Export a notebook to a parameterize Python 3 script using Jinja templates """ logging.info( f'Generate Python script {output_path} from Jupyter Notebook {input_notebook_path}' ) logging.debug(f'Global Configuration: {conf}') logging.debug(f'Template path {TEMPLATE_PATH}') exporter = PythonExporter(get_config(TEMPLATE_PATH)) exporter.register_filter(name='filter_trailing_cells', jinja_filter=filter_trailing_cells) exporter.register_filter(name='get_formatted_cells', jinja_filter=get_formatted_cells) exporter.register_filter(name='get_data_from_docstring', jinja_filter=get_data_from_docstring) exporter.register_filter(name='sanitize_method_name', jinja_filter=to_method_name) resources = {'ignore_keys': conf.ignore_keys} logging.debug(f'Template info {resources}') try: script_content, _ = exporter.from_filename(input_notebook_path, resources=resources) except Exception as e: raise MlVToolException(e) from e if not script_content: logging.warning('Empty notebook provided. Nothing to do.') return write_python_script(script_content, output_path) logging.log( logging.WARNING + 1, f'Python script successfully generated in {abspath(output_path)}')