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": [ | |
"Authors and readers of `pidgin` programs should enjoy the ability to reuse their literate programs inside other documents or programs. The reuse of documents creates a natural inter-textuality bounded by language, rather than framework." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" import importnb, importlib._bootstrap\n", | |
" try:\n", | |
" from .__interactive_markdown_cells import cleanup_transform as markdown_to_python\n", | |
" from .__emojis__ import demojize\n", | |
" from .__xonsh_compiler import execer\n", | |
" from .__return_yield_display import ReturnYield\n", | |
" visitor = ReturnYield()\n", | |
" except: \n", | |
" import __init__ as literacy\n", | |
" markdown_to_python = literacy.__interactive_markdown_cells.cleanup_transform\n", | |
" demojize = literacy.__emojis__.demojize\n", | |
" execer = literacy.__xonsh_compiler.execer\n", | |
" visitor = literacy.__return_yield_display.ReturnYield()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Each time a `pidgin` program we'll need to execute the `pidgin` programming heuristics:" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"1. `demojize` the source code.\n", | |
"2. Convert `markdown_to_python`, transforming narrative into strings.\n", | |
"3. Apply the `return` and `yield` node transformers at the root level." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" class PidginMixin:\n", | |
" extensions = \".ipynb .xo.ipynb .md.ipynb\".split()\n", | |
" def code(self, str): return ''.join(demojize(super().code(''.join(markdown_to_python(str)))))\n", | |
" def visit(self, node): return super().visit(visitor(node))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"4. Use the `xonsh` compiler." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" class PidginLoader(PidginMixin, importnb.Notebook):\n", | |
" def source_to_code(self, data, path, *, _optimize=-1):\n", | |
" return importlib._bootstrap._call_with_frames_removed(execer.compile, data, 'exec') or compile(data, self.path, 'exec')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The consideration of eccentric document formats has a broad impact. It is possible to consider embedding programs in [word documents](https://github.com/deathbeds/deathbeds.github.io/blob/master/deathbeds/2019-03-08-say-word.ipynb) or portable document formats. " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
" if __name__ == '__main__':\n", | |
" with PidginLoader(): import foo\n", | |
" assert foo.git[0].startswith(\"HEAD detached\")\n", | |
" foo._repr_markdown_ = lambda:'\\n\\n'.join(\n", | |
" ''.join(x['source']) for x in __import__('json').loads(__import__('pathlib').Path(foo.__file__).read_text())['cells'])" | |
] | |
} | |
], | |
"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