Passed
Pull Request — dev (#850)
by Uwe
08:28 queued 07:10
created

check_examples   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 110
Duplicated Lines 20 %

Importance

Changes 0
Metric Value
wmc 2
eloc 84
dl 22
loc 110
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A notebook_run() 0 33 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import os
2
import subprocess
3
import tempfile
4
import warnings
5
from datetime import datetime
6
7
import matplotlib
8
import nbformat
9
from matplotlib import pyplot as plt
10
from termcolor import colored
11
12
warnings.filterwarnings("ignore", "", UserWarning)
13
matplotlib.use("Agg")
14
15
stop_at_error = False  # If True script will stop if error is raised
16
exclude_notebooks = False
17
exclude_python_scripts = False
18
19
20
def notebook_run(path):
21
    """
22
    Execute a notebook via nbconvert and collect output.
23
    Returns (parsed nb object, execution errors)
24
    """
25
    dirname, __ = os.path.split(path)
26
    os.chdir(dirname)
27
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
28
        args = [
29
            "jupyter",
30
            "nbconvert",
31
            "--to",
32
            "notebook",
33
            "--execute",
34
            "--ExecutePreprocessor.timeout=60",
35
            "--output",
36
            fout.name,
37
            path,
38
        ]
39
        subprocess.check_call(args)
40
41
        fout.seek(0)
42
        nb = nbformat.read(fout, nbformat.current_nbformat)
43
44
    errors = [
45
        output
46
        for cell in nb.cells
47
        if "outputs" in cell
48
        for output in cell["outputs"]
49
        if output.output_type == "error"
50
    ]
51
52
    return nb, errors
53
54
55
fullpath = os.path.join(os.getcwd())
56
57
checker = {}
58
number = 0
59
60
start = datetime.now()
61
62
for root, dirs, files in sorted(os.walk(fullpath)):
63
    for name in sorted(files):
64
        if (
65
            name[-3:] == ".py"
66
            and not exclude_python_scripts
67
            and not name == "check_examples.py"
68
        ):
69
            fn = os.path.join(root, name)
70
            os.chdir(root)
71
            number += 1
72 View Code Duplication
            if stop_at_error is True:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
                print(fn)
74
                exec(open(fn).read())
75
                checker[name] = "okay"
76
            else:
77
                try:
78
                    exec(open(fn).read())
79
                    checker[name] = "okay"
80
                except Exception as e:
81
                    print(e)
82
                    checker[name] = "failed"
83
        elif name[-6:] == ".ipynb" and not exclude_notebooks:
84
            fn = os.path.join(root, name)
85
            os.chdir(root)
86
            number += 1
87 View Code Duplication
            if stop_at_error is True:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
88
                print(fn)
89
                notebook_run(fn)
90
                checker[name] = "okay"
91
            else:
92
                try:
93
                    notebook_run(fn)
94
                    checker[name] = "okay"
95
                except Exception as e:
96
                    print(e)
97
                    checker[name] = "failed"
98
        plt.close()
99
100
101
print("******* TEST RESULTS ***********************************")
102
103
print("\n{0} examples tested in {1}.\n".format(number, datetime.now() - start))
104
105
for k, v in checker.items():
106
    if v == "failed":
107
        print(k, colored(v, "red"))
108
    else:
109
        print(k, colored(v, "green"))
110