Last active
November 21, 2020 10:02
-
-
Save 2torus/f78b7cef5770927a92e3ca652f38ff89 to your computer and use it in GitHub Desktop.
Replacement for ipython_doctester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ipython_doctester module is outdated. I tried to replace its test function with the function below | |
Code is from https://stackoverflow.com/a/47590703/996379 | |
""" | |
import doctest | |
import copy | |
def test(func): | |
''' | |
Use test as a decorator to a function with doctests in Jupyter notebook. | |
Run the cell to see the results of the doctests. | |
Obligatory doctest: | |
>>> @test | |
... def mult(a, b): | |
... """ | |
... >>> mult(2, 2) | |
... 4 | |
... >>> mult(3, 6) | |
... 18 | |
... """ | |
... return a * b | |
... | |
Finding tests in mult | |
Trying: | |
mult(2, 2) | |
Expecting: | |
4 | |
ok | |
Trying: | |
mult(3, 6) | |
Expecting: | |
18 | |
ok | |
''' | |
globs = copy.copy(globals()) | |
globs.update({func.__name__:func}) | |
doctest.run_docstring_examples(func, globs, verbose=True, name=func.__name__) | |
return func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tamuhey - good catch. Thanks for the feedback. I took the import out.