Created
December 11, 2016 08:54
-
-
Save EWouters/40bbc13b8561f2c9968e87c055617ee5 to your computer and use it in GitHub Desktop.
Jupyter notebook that shows cell magic to run python2 code in python3 kernel
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": [ | |
{ | |
"metadata": { | |
"toc": "true" | |
}, | |
"cell_type": "markdown", | |
"source": "# Table of Contents\n <p>" | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "print('hi')", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "hi\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "import lib2to3\nfrom lib2to3.refactor import RefactoringTool, get_fixers_from_package\navail_fixes = set(get_fixers_from_package('lib2to3.fixes'))\nrefactoringTool = RefactoringTool(avail_fixes)\ndel avail_fixes, get_fixers_from_package, RefactoringTool\ndef refactor_cell(src):\n try:\n tree = refactoringTool.refactor_string(src+'\\n', '<dummy_name>')\n except (lib2to3.pgen2.parse.ParseError,\n lib2to3.pgen2.tokenize.TokenError):\n return src\n else:\n return str(tree)[:-1]", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "from IPython.core.magic import register_cell_magic\n\n@register_cell_magic\ndef run2in3(line, cell):\n \"\"\"\n Cell magic that runs python2 code as python3 code, using 2to3lib\n \"\"\"\n return eval(refactor_cell(cell))\n\ndel register_cell_magic", | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "%%run2in3\n\nprint 'hi'", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "hi\n" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "%whos", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": "Variable Type Data/Info\n----------------------------------------------\nFormatCode function <function FormatCode at 0x000001CB179A5840>\nlib2to3 module <module 'lib2to3' from 'C<...>b\\\\lib2to3\\\\__init__.py'>\nrefactor_cell function <function refactor_cell at 0x000001CB18BC9F28>\nrefactoringTool RefactoringTool <lib2to3.refactor.Refacto<...>ct at 0x000001CB18C43278>\nrun2in3 function <function run2in3 at 0x000001CB18BC9730>\n" | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python [default]", | |
"language": "python" | |
}, | |
"anaconda-cloud": {}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Jupyter notebook that shows cell magic to run python2 code in python3 kernel", | |
"public": true | |
} | |
}, | |
"language_info": { | |
"name": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"nbconvert_exporter": "python", | |
"mimetype": "text/x-python", | |
"file_extension": ".py" | |
}, | |
"toc": { | |
"threshold": 4, | |
"number_sections": true, | |
"toc_cell": true, | |
"toc_window_display": false, | |
"toc_section_display": "block", | |
"sideBar": true, | |
"navigate_menu": true, | |
"nav_menu": { | |
"height": "12px", | |
"width": "252px" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment