Beispiel #1
0
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)
Beispiel #2
0
def test_should_handle_notebook_with_invalid_python_name_with_conf(
        work_dir, mocker):
    """
        Test invalid python filename are converted
    """
    notebook_path = gen_notebook(cells=[('code', 'pass')],
                                 tmp_dir=work_dir,
                                 file_name='01_(test) nb.ipynb')

    # Create conf in working directory
    conf_data = write_conf(work_dir=work_dir,
                           conf_path=join(work_dir, DEFAULT_CONF_FILENAME),
                           ignore_keys=['# Ignore', 'remove='])

    cmd_arguments = ['-n', notebook_path, '-w', work_dir]
    IPynbToPython().run(*cmd_arguments)

    # This path is generated using the conf script_dir and the notebook name
    output_script_path = join(work_dir,
                              conf_data['path']['python_script_root_dir'],
                              'mlvtools_01__test_nb.py')
    assert exists(output_script_path)

    with open(output_script_path, 'r') as fd:
        file_content = fd.read()

    # Ensure generated file syntax is right
    compile(file_content, output_script_path, 'exec')
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
Beispiel #4
0
def test_should_generate_valid_python_if_only_trailing_cells(work_dir):
    """
        Convert a Jupyter Notebook with no 'code cell' to a Python 3 script
    """
    cells = [('markdown', 'This is a trailing comment cell'),
             ('code', '# No effect\ntrailing = 2')]
    notebook_path = gen_notebook(cells,
                                 tmp_dir=work_dir,
                                 file_name='test_nb.ipynb')

    output_path = join(work_dir, 'out.py')
    cmd_arguments = [
        '-n', notebook_path, '-o', output_path, '--working-directory', work_dir
    ]
    IPynbToPython().run(*cmd_arguments)

    assert exists(output_path)

    with open(output_path, 'r') as fd:
        file_content = fd.read()

    assert 'def mlvtools_test_nb():' in file_content
    assert not is_in(cells[0][1], file_content)
    assert not is_in(cells[1][1], file_content)
    assert is_in('pass', file_content)
    assert 'mlvtools_test_nb()' in file_content
Beispiel #5
0
def test_should_handle_notebook_with_invalid_python_name_with_conf(work_dir, mocker):
    """
        Test invalid python filename are converted
    """
    mocked_check_output = mocker.patch('subprocess.check_output', return_value=work_dir.encode())
    notebook_path = gen_notebook(cells=[('code', 'pass')], tmp_dir=work_dir, file_name='01_(test) nb.ipynb')

    # Create conf in a freshly init git repo
    conf_data = write_conf(work_dir=work_dir, conf_path=join(work_dir, DEFAULT_CONF_FILENAME),
                           ignore_keys=['# Ignore', 'remove='])

    cmd_arguments = ['-n', notebook_path]
    IPynbToPython().run(*cmd_arguments)

    # This path is generated using the conf script_dir and the notebook name
    output_script_path = join(work_dir, conf_data['path']['python_script_root_dir'], 'mlvtools_01__test_nb.py')
    assert exists(output_script_path)

    with open(output_script_path, 'r') as fd:
        file_content = fd.read()

    # Ensure generated file syntax is right
    compile(file_content, output_script_path, 'exec')

    assert mocked_check_output.mock_calls == [mocker.call(
        ['git', 'rev-parse', '--show-toplevel'],
        cwd=work_dir)]
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)
def ref_notebook_path(work_dir):
    notebook_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None,
                                 cells=[('code', '# A Tag\nprint(\'poney\')'),
                                        ('comment', '# This is a comment'),
                                        ('code', 'def test():\n'
                                                 '  """This is a docstring"""\n'
                                                 '  print("hello")\n')])
    return notebook_path
Beispiel #8
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
Beispiel #9
0
def test_should_raise_if_no_conf(work_dir):
    """
        Test command raise if no conf, neither auto detected or given to command line
    """
    notebook_path = gen_notebook(cells=[('code', 'pass')],
                                 tmp_dir=work_dir,
                                 file_name='test_nb.ipynb')
    arguments = ['-n', notebook_path, '--working-directory', work_dir]
    with pytest.raises(MlVToolException):
        IPynbCheckAllScripts().run(*arguments)
Beispiel #10
0
def test_should_raise_if_script_is_not_a_file(conf, work_dir):
    """
        Test raises if provided script is not a file
    """
    cells = [('code', 'a = 3')]
    notebook_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None, cells=cells)

    with pytest.raises(MlVToolException) as e:
        compare(notebook_path, join(work_dir, 'does_not_exist.py'), conf)
    assert isinstance(e.value.__cause__, IOError)
Beispiel #11
0
def test_should_raise_if_invalid_script(conf, work_dir):
    """
        Test raises if provided script is not a file
    """
    cells = [('code', 'a = 3')]
    notebook_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None, cells=cells)
    script_path = join(work_dir, 'script.py')
    with open(script_path, 'w') as fd:
        fd.write('a:=3')
    with pytest.raises(MlVToolException) as e:
        compare(notebook_path, script_path, conf)
    assert isinstance(e.value.__cause__, SyntaxError)
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)
Beispiel #13
0
def test_should_detect_inconsistency_with_diff_script(conf, work_dir):
    """
        Test detects inconsistency with diff script
    """
    cells = [('code', 'print(\'poney\')'),
             ('comment', '# This is a comment'),
             ('code', 'def test():\n  """This is a docstring"""\n  print("hello")\n')]
    _, script_base_path = create_notebook_and_convert_it(cells, 'a.py', conf, work_dir)

    cells[0] = ('code', 'print("diff")')
    notebook_diff_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None, cells=cells)

    assert not compare(notebook_diff_path, script_base_path, conf)
Beispiel #14
0
def test_should_find_consistency_with_diff_on_comments(conf, work_dir):
    """
        Test there is consistency with diff only on comments
    """
    cells = [('code', 'print(\'poney\')'),
             ('comment', '# This is a comment'),
             ('code', 'def test():\n  """Thsaved_notebok_base_pathis is a docstring"""\n  print("hello")\n')]
    _, script_base_path = create_notebook_and_convert_it(cells, 'base.py', conf, work_dir)

    cells[1] = ('comment', '# This is a DIFFERENT comment')
    notebook_diff_path = gen_notebook(tmp_dir=work_dir, file_name='test.ipynb', docstring=None, cells=cells)

    assert compare(notebook_diff_path, script_base_path, conf)
Beispiel #15
0
def test_should_overwrite_with_force_argument(work_dir):
    """
        Test output paths are overwritten with force argument
    """
    notebook_path = gen_notebook(cells=[('code', 'pass')],
                                 tmp_dir=work_dir,
                                 file_name='test_nb.ipynb')
    output_path = join(work_dir, 'py_script')
    with open(output_path, 'w') as fd:
        fd.write('')
    arguments = [
        '-n', notebook_path, '--working-directory', work_dir, '-o',
        output_path, '--force'
    ]
    IPynbToPython().run(*arguments)
    with open(output_path, 'r') as fd:
        assert fd.read()
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)
Beispiel #18
0
def generate_test_notebook(work_dir: str, notebook_name: str):
    docstring = '"""\n' \
                ':param str subset: The kind of subset to generate.\n' \
                ':param int rate:\n' \
                ':dvc-out output_file: {{ conf.output_file }}\n' \
                '"""\n'
    docstring_cell = ('code',
                      '#Parameters\n{}subset = "train"\n'.format(docstring))
    code_cells = [('code', 'import numpy as np\n'
                   'import pandas as pd\n'
                   'from sklearn.datasets import fetch_20newsgroups\n'),
                  ('code',
                   'newsgroups_train = fetch_20newsgroups(subset=subset,\n'
                   '            remove=("headers", "footers", "quotes"))'),
                  ('code', 'df_train.to_csv("data_train.csv", index=None)')]
    comment_cell = ('markdown', 'This is a comment cell')
    no_effect_cells = [
        ('code', '# Ignore\n# No effect'
         'df_train = pd.DataFrame(newsgroups_train.data, columns=["data"])'),
        ('code', '# No effect\ndf_train')
    ]
    trailing_comment = ('markdown', 'This is a trailing comment cell')
    traling_no_effect = ('code', '# No effect\ntrailing = 2')

    cells = [
        docstring_cell, code_cells[0], code_cells[1], comment_cell,
        no_effect_cells[0], no_effect_cells[1], code_cells[2],
        traling_no_effect, trailing_comment
    ]

    notebook_path = gen_notebook(cells, work_dir, notebook_name)
    dropped_cells = DroppedCells(no_effect_cells,
                                 [trailing_comment, traling_no_effect],
                                 docstring_cell)
    kept_cells = KeptCells(code_cells, [comment_cell])
    return kept_cells, dropped_cells, docstring, notebook_path
Beispiel #19
0
def notebook_dir(work_dir):
    notebook_dir = join(work_dir, 'notebooks')
    makedirs(notebook_dir)
    gen_notebook(tmp_dir=notebook_dir,
                 file_name='hello.ipynb',
                 docstring=None,
                 cells=[('comment', '# This is a comment for hello'),
                        ('code', 'print("hello")'),
                        ('code', '# A Tag\nprint("end")')])
    gen_notebook(tmp_dir=notebook_dir,
                 file_name='hi.ipynb',
                 docstring=None,
                 cells=[('comment', '# This is a comment for hi'),
                        ('code', 'print("hi")'),
                        ('code', '# A Tag\nprint("end")')])
    gen_notebook(tmp_dir=notebook_dir,
                 file_name='bye.ipynb',
                 docstring=None,
                 cells=[('comment', '# This is a comment for bye'),
                        ('code', 'print("bye")'),
                        ('code', '# A Tag\nprint("end")')])
    return notebook_dir
Beispiel #20
0
def setup_with_conf(work_dir: str, conf_path: str) -> Tuple[str, str]:
    write_conf(work_dir=work_dir,
               conf_path=conf_path,
               script_dir='./test_scripts')
    nb_path = gen_notebook([('markdown', '# test')], work_dir, 'nb_test.ipynb')
    return conf_path, nb_path