Skip to content

Instantly share code, notes, and snippets.

@2torus
Last active November 21, 2020 10:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 2torus/f78b7cef5770927a92e3ca652f38ff89 to your computer and use it in GitHub Desktop.
Save 2torus/f78b7cef5770927a92e3ca652f38ff89 to your computer and use it in GitHub Desktop.
Replacement for ipython_doctester
"""
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
@tamuhey
Copy link

tamuhey commented Aug 15, 2019

@2torus
Copy link
Author

2torus commented Aug 15, 2019

@tamuhey - good catch. Thanks for the feedback. I took the import out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment