Last active
December 11, 2019 04:01
-
-
Save tonyfast/3b66729ac43e6e47ea38e882091b7b71 to your computer and use it in GitHub Desktop.
Literate computing essays https://mybinder.org/v2/gist/tonyfast/3b66729ac43e6e47ea38e882091b7b71/master
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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A common use for notebooks is to \"test an idea\". Designing pathways to mature informal notebook to formal testing tools like `doctest`. This notebook implements the ability to `doctest` when a cell is run." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import doctest, traitlets, IPython, contextlib, ast, textwrap" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"A full featured interactive `doctest` tool will be able to access any modifications to the interactive shell. For example magics must work." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" @contextlib.contextmanager\n", | |
" def wrapped_compiler(shell):\n", | |
" \"\"\"`wrapped_compiler` replaces the `doctest` compiler with the interactive shell.\"\"\"\n", | |
" def compiler(input, filename, symbol, *args, **kwargs):\n", | |
" nonlocal shell\n", | |
" return shell.compile(ast.Interactive(body=shell.transform_ast(shell.compile.ast_parse(shell.transform_cell(textwrap.indent(input, \" \" * 4)))).body), filename, \"single\",)\n", | |
" yield setattr(doctest, \"compile\", compiler)\n", | |
" try: doctest.compile = compile\n", | |
" except: ..." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"`run_docstring_examples` implements `doctest` machinery to test code with the interactive python shell." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" def run_docstring_examples(str, shell=IPython.get_ipython(), verbose=False, compileflags=None):\n", | |
" runner = doctest.DocTestRunner(verbose=verbose, optionflags=doctest.ELLIPSIS)\n", | |
" with wrapped_compiler(shell):\n", | |
" for test in doctest.DocTestFinder(verbose).find(str, name=shell.user_module.__name__):\n", | |
" test.globs = shell.user_ns\n", | |
" runner.run(test, compileflags=compileflags, clear_globs=False)\n", | |
" return runner\n", | |
"\n", | |
" def run(result): return run_docstring_examples(result.info.raw_cell, IPython.get_ipython())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" def unload_ipython_extension(shell):\n", | |
" try: shell.events.unregister('post_run_cell', run)\n", | |
" except ValueError: ...\n", | |
" def load_ipython_extension(shell): unload_ipython_extension(shell), shell.events.register('post_run_cell', run)\n", | |
" __name__ == '__main__' and load_ipython_extension(get_ipython())" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
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
with __import__('importnb').Notebook(): | |
try: from . import __doctest_post_run_cell, __interactive_markdown_cells, __string_expression_transformer, __interactive_jinja_templates, __xonsh_compiler, __emojis__, __return_yield_display, __pidgin_loader, __functional_programming_toolz, __default_markdown_display_for_strings | |
except: import __doctest_post_run_cell, __interactive_markdown_cells, __string_expression_transformer, __interactive_jinja_templates, __xonsh_compiler, __emojis__, __return_yield_display, __pidgin_loader, __functional_programming_toolz, __default_markdown_display_for_strings | |
PidginLoader = __pidgin_loader.PidginLoader | |
def unload_ipython_extension(shell): [x.unload_ipython_extension(shell) for x in (__doctest_post_run_cell, __interactive_markdown_cells, __string_expression_transformer, __interactive_jinja_templates, __xonsh_compiler, __emojis__, __return_yield_display, __default_markdown_display_for_strings)] | |
def load_ipython_extension(shell): [x.load_ipython_extension(shell) for x in (__doctest_post_run_cell, __interactive_markdown_cells, __string_expression_transformer, __interactive_jinja_templates, __xonsh_compiler, __emojis__, __return_yield_display, __default_markdown_display_for_strings)] | |
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
import schematypes | |
from tonyfast.poser import * | |
shell = get_ipython() | |
shell.run_cell("\n %reload_ext tonyfast.literacy") | |
shell.enable_html_pager = True |
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
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
toolz | |
notebook | |
importnb | |
htmlmin | |
nbconvert | |
IPython | |
xonsh | |
graphviz | |
emoji | |
stringcase |
Author
tonyfast
commented
Oct 11, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment