def test_substitute_variable_values(self):
     str_inp = "n_steps=5000\nnsteps == 5\n"
     str_exp = "n_steps = 10; _n_steps__original=5000\nnsteps == 5\n"
     str_out = iw.substitute_variable_values(str_inp, n_steps=10)
     self.assertEqual(str_out, str_exp)
     str_out = iw.substitute_variable_values(str_inp,
                                             n_steps='10',
                                             strings_as_is=True)
     self.assertEqual(str_out, str_exp)
     str_inp = "N=5000\nnsteps == 5\n"
     str_exp = "N = 10\nnsteps == 5\n"
     str_out = iw.substitute_variable_values(str_inp, N=10, keep_original=0)
     self.assertEqual(str_out, str_exp)
     # test exceptions
     str_inp = "n_steps=5000\nnsteps == 5\n"
     self.assertRaises(AssertionError,
                       iw.substitute_variable_values,
                       str_inp,
                       other_var=10)
     str_inp = "other_var == 5\n"
     self.assertRaises(AssertionError,
                       iw.substitute_variable_values,
                       str_inp,
                       other_var=10)
     str_inp = "var, other_var = 5, 6\n"
     self.assertRaises(AssertionError,
                       iw.substitute_variable_values,
                       str_inp,
                       var=10)
 def test_substitute_variable_values(self):
     str_inp = "n_steps=5000\nnsteps == 5\n"
     str_exp = "n_steps = 10; _n_steps__original=5000\nnsteps == 5\n"
     str_out = iw.substitute_variable_values(str_inp, n_steps=10)
     self.assertEqual(str_out, str_exp)
     # test exceptions
     str_inp = "n_steps=5000\nnsteps == 5\n"
     self.assertRaises(AssertionError, iw.substitute_variable_values,
                       str_inp, other_var=10)
     str_inp = "other_var == 5\n"
     self.assertRaises(AssertionError, iw.substitute_variable_values,
                       str_inp, other_var=10)
Beispiel #3
0
def handle_ci_case(args):
    notebook_filepath = args.input
    if args.output:
        notebook_filepath_edited = args.output
    else:
        notebook_filepath_edited = notebook_filepath + '~'

    # parse original notebook
    with open(notebook_filepath, encoding='utf-8') as f:
        nb = nbformat.read(f, as_version=4)

    # add new cells containing the solutions
    if args.scripts:
        for filepath in args.scripts:
            add_cell_from_script(nb, filepath)

    # convert solution cells to code cells
    if args.exercise2:
        convert_exercise2_to_code(nb)

    # remove empty cells (e.g. those below exercise2 cells)
    if args.remove_empty_cells:
        remove_empty_cells(nb)

    # disable plot interactivity
    disable_plot_interactivity(nb)

    # guard against a jupyter bug involving matplotlib
    split_matplotlib_cells(nb)

    if args.substitutions or args.execute:
        # substitute global variables
        cell_separator = f'\n##{uuid.uuid4().hex}\n'
        src = cell_separator.join(get_code_cells(nb))
        new_values = args.substitutions or []
        parameters = dict(x.split('=', 1) for x in new_values)
        src = iw.substitute_variable_values(src,
                                            strings_as_is=True,
                                            keep_original=False,
                                            **parameters)
        set_code_cells(nb, src.split(cell_separator))

    if args.execute:
        execute_notebook(nb, src, cell_separator, args.input)

    # write edited notebook
    with open(notebook_filepath_edited, 'w', encoding='utf-8') as f:
        nbformat.write(nb, f)
Beispiel #4
0
            code = iw.deprotect_ipython_magics(code)
            lines = code.split('\n')
            lineno_end = mapping[visitor.matplotlib_first]
            split_code = '\n'.join(lines[lineno_end:]).lstrip('\n')
            if split_code:
                new_cell = nbformat.v4.new_code_cell(source=split_code)
                nb['cells'].insert(i + 1, new_cell)
            lines = lines[:lineno_end]
            nb['cells'][i]['source'] = '\n'.join(lines).rstrip('\n')
            break

# substitute global variables and disable OpenGL/Mayavi GUI
cell_separator = '\n##{}\n'.format(uuid.uuid4().hex)
src = cell_separator.join(get_code_cells(nb))
parameters = dict(x.split('=', 1) for x in new_values)
src = iw.substitute_variable_values(src, strings_as_is=True,
                                    keep_original=False, **parameters)
src_no_gui = iw.mock_es_visualization(src)

# update notebook with new code
set_code_cells(nb, src_no_gui.split(cell_separator))

# execute notebook
ep = ExecutePreprocessor(timeout=20 * 60, kernel_name='python3')
ep.preprocess(nb, {'metadata': {'path': notebook_dirname}})

# restore notebook with code before the GUI removal step
set_code_cells(nb, src.split(cell_separator))

# write edited notebook
with open(notebook_filepath_edited, 'w', encoding='utf-8') as f:
    nbformat.write(nb, f)