def test_should_detect_parameters(header, conf, work_dir):
    """
        Test Notebook is converted to parameterized python script,
        parameter cell in Notebook is detected and well handled.
        The parameter cell must be the first code cell. It should be detected
        if there is a markdown header or not.
    """
    output_path = join(work_dir, 'out.py')

    docstring_cell = '''
# Parameters
"""
    :param str subset: The kind of subset to generate.
    :param int rate: The rate of I don't know what
    :param param3:
"""
subset = 'train'
toto = 12
        '''
    notebook_path = gen_notebook(cells=[('code', 'print(\'poney\')')],
                                 tmp_dir=work_dir,
                                 file_name='test.ipynb',
                                 docstring=docstring_cell,
                                 header=header)
    export_to_script(input_notebook_path=notebook_path,
                     output_path=output_path,
                     conf=conf)
    assert exists(output_path)
    with open(output_path, 'r') as fd:
        content = fd.read()

    # Check main method is created
    assert 'def mlvtools_test(subset: str, rate: int, param3):' in content
def test_should_raise_if_more_than_one_docstring_in_first_cell(conf, work_dir):
    """
        Test multi docstring in the parameter cell is detected
    """
    output_path = join(work_dir, 'out.py')

    docstring_cell = '''
    # Parameters
    """
        :param param3: Plop
    """
     """
        :param param6: AA
    """
    subset = 'train'
    toto = 12
    '''

    notebook_path = gen_notebook(cells=[('code', 'print(\'poney\')')],
                                 tmp_dir=work_dir,
                                 file_name='test.ipynb',
                                 docstring=docstring_cell)
    with pytest.raises(MlVToolException):
        export_to_script(input_notebook_path=notebook_path,
                         output_path=output_path,
                         conf=conf)
Esempio n. 3
0
    def run(self, *args, **kwargs):
        args = ArgumentBuilder(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
                               description='Convert Notebook to python script') \
            .add_work_dir_argument() \
            .add_conf_path_argument() \
            .add_docstring_conf() \
            .add_force_argument() \
            .add_argument('-n', '--notebook', type=str, required=True,
                          help='The notebook to convert') \
            .parse(args)
        self.set_log_level(args)
        conf = self.get_conf(args.working_directory, args.notebook,
                             args.conf_path)
        if not conf.path:
            raise MlVToolException('Configuration file is mandatory')
        docstring_conf_path = args.docstring_conf or conf.docstring_conf
        docstring_conf = load_docstring_conf(
            docstring_conf_path) if docstring_conf_path else None

        output_script = get_script_output_path(args.notebook, conf)
        out_dvc_cmd = get_dvc_cmd_output_path(output_script, conf)
        self.check_force(args.force, [output_script, out_dvc_cmd])

        export_to_script(args.notebook, output_script, conf)
        gen_dvc_command(output_script, out_dvc_cmd, conf, docstring_conf)
Esempio n. 4
0
def create_notebook_and_convert_it(cells, script_name, conf, work_dir) -> str:
    """
        Create a notebook from cells then convert it into a python script
    """
    notebook_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None, cells=cells)
    script_base_path = join(work_dir, script_name)
    export_to_script(notebook_path, script_base_path, conf)
    return notebook_path, script_base_path
def test_should_be_resilient_to_empty_notebook(conf, work_dir):
    """
        Test templating is resilient to empty Notebook, no exception.
    """
    output_path = join(work_dir, 'out.py')
    notebook_path = gen_notebook(cells=[('code', 'print(\'poney\')')],
                                 tmp_dir=work_dir,
                                 file_name='test.ipynb',
                                 docstring=None)
    export_to_script(input_notebook_path=notebook_path,
                     output_path=output_path,
                     conf=conf)
    assert exists(output_path)
def test_should_convert_notebook_to_python_script(conf, work_dir):
    """
        Test Notebook is converted to python script
    """
    output_path = join(work_dir, 'out.py')
    notebook_path = gen_notebook(cells=[('code', 'print(\'poney\')')],
                                 tmp_dir=work_dir,
                                 file_name='test.ipynb',
                                 docstring=None)
    export_to_script(input_notebook_path=notebook_path,
                     output_path=output_path,
                     conf=conf)

    assert exists(output_path)
    with open(output_path, 'r') as fd:
        content = fd.read()

    # Check main method is created
    assert 'def mlvtools_test():' in content
def test_should_raise_if_invalid_docstring(conf, work_dir):
    """
        Test an MlVTool exception is raised if docstring is invalid
    """

    output_path = join(work_dir, 'out.py')

    docstring_cell = '''
    # Parameters
    """
        :param param3
    """
    subset = 'train'
    toto = 12
    '''

    notebook_path = gen_notebook(cells=[('code', 'print(\'poney\')')],
                                 tmp_dir=work_dir,
                                 file_name='test.ipynb',
                                 docstring=docstring_cell)
    with pytest.raises(MlVToolException):
        export_to_script(input_notebook_path=notebook_path,
                         output_path=output_path,
                         conf=conf)