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": [ | |
"All code should compute, <code>code</code> for typographic decoration should require extra effort to suppress. As a rule, code in a document should work.\n", | |
"\n", | |
"\n", | |
"In this document, we'll execute code from within ticks as one would with inline code." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" import ast, mistune, re" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Use `mistune`'s pattern to discover inine code." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" for key in \"code emphasis double_emphasis link autolink url strikethrough footnote\".split():\n", | |
" globals()[key] = re.compile(getattr(mistune.InlineGrammar, key).pattern[1:])\n", | |
" for key in \"block_quote heading\".split():\n", | |
" globals()[key] = re.compile(getattr(mistune.BlockGrammar, key).pattern[1:])\n", | |
" \n", | |
" \n", | |
" else: \n", | |
" emphasis = re.compile('|'.join((emphasis.pattern, double_emphasis.pattern, strikethrough.pattern)))\n", | |
" urls = re.compile('|'.join((url.pattern, autolink.pattern)))\n", | |
" del key, double_emphasis\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Create an `ast.NodeTransformer` that runs code in ticks. In this case, `ast.parse` is applied to any inline code." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" def quote(str):\n", | |
" from pidgin.__interactive_markdown_cells import quote\n", | |
" return quote(str).rstrip().rstrip(';')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" def globals_update(str, node):\n", | |
" return ast.copy_location(ast.parse(\"\"\"globals().update({%s: globals().get(%s, %s)})\"\"\"%(str,str,str)), node).body" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" class String(ast.NodeTransformer):\n", | |
" def __init__(self, shell): self.shell = shell\n", | |
" \n", | |
" def visit_Str(self, node):\n", | |
" for m in code.finditer(node.s): \n", | |
" self.extras += ast.copy_location(self.shell.compile.ast_parse(m.group().strip('`')), node).body\n", | |
"\n", | |
" for pattern in (emphasis, block_quote, heading, link):\n", | |
" for m in pattern.finditer(node.s):\n", | |
" self.extras += globals_update(quote(m.string[slice(*m.span())]).lstrip('!'), node)\n", | |
"\n", | |
" for m in urls.finditer(node.s):\n", | |
" self.extras += globals_update(quote(m.groups()[0]), node)\n", | |
" return node\n", | |
"\n", | |
" def visit_Module(self, node):\n", | |
" self.extras = []\n", | |
" node = super().generic_visit(node)\n", | |
" return ast.Module(node.body[:-1] + self.extras + node.body[-1:])" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Append to the `ast_transformers` with an `IPython` magic." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" def unload_ipython_extension(shell): shell.ast_transformers += [x for x in shell.ast_transformers if not isinstance(x, String)]\n", | |
" def load_ipython_extension(shell): unload_ipython_extension(shell); shell.ast_transformers += [String(shell)]\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