Created
February 3, 2014 16:41
-
-
Save jasonbot/8787343 to your computer and use it in GitHub Desktop.
Alternative to the clunky 2to3 command line script. Streamlined function that will take a Python 2 code string (as unicode), feed it through lib2to3 using all its converters, then return a Python 3 code string.
This file contains hidden or 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
| # coding: utf-8 | |
| import lib2to3 | |
| import lib2to3.fixes | |
| import lib2to3.refactor | |
| __all__ = ["code_to_py3"] | |
| all_fixes = ["lib2to3.fixes.fix_{}".format(l) | |
| for l in lib2to3.refactor.get_all_fix_names("lib2to3.fixes")] | |
| tool = lib2to3.refactor.RefactoringTool(all_fixes, | |
| options={'print_function': False}) | |
| def code_to_py3(code_block): | |
| # + "\n" to make it parse, then [:-1] to chop off tacked-on \n | |
| return tool.refactor_string(code_block + u"\n", | |
| "<script>")[:-1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment