Showing posts with label 2to3. Show all posts
Showing posts with label 2to3. Show all posts
Saturday, October 3, 2009
% formatting to str.format converter
Recent discussions on Python-dev have revolved around transitioning the standard library to the new str.format method. One suggestion was to write a automatic converter for old format strings to new ones. I've taken on the task and written mod2format at https://code.launchpad.net/~gutworth/+junk/mod2format. You can try it out by running "python3 -m mod2format [your format strings here]".
Thursday, December 11, 2008
Get help porting your package!
To help the community with porting their packages to Python 3, we have created the python-porting mailing list. Many core developers are subscribed to the list, so you should be able to get excellent advice on 2to3, the bytes/unicode split, C-API, or other incompatibilities.
Thursday, September 4, 2008
Fun with 2to3
Recently, I've been working on 2to3 code refactoring tool. It's quite exciting really; how often does automatic editing of source code work?
2to3 is completely written in Python using the stdlib. The main steps in code translation are:
Over the past few weeks, I've written a couple of fixers. It's pretty intuitive once you get the hang of it, but writing good tests is very important because Python's flexible syntax produces many possibilities you fixer must deal with. I also refactored lib2to3, so plugging a different system of fixers is much easier for custom applications. I've also written some documentation on it's usage. I hope to start documenting the API and writing a guide for creating fixers soon, so other people can start making use of lib2to3.
2to3 is completely written in Python using the stdlib. The main steps in code translation are:
- 2to3 is given a Python source file and a list of transformations (in units called fixers) to apply to it.
- 2to3 generates a custom parse tree of the source based on a Python grammar that combines elements of 2.x and 3.x's syntax. It takes note of exact indentation and comment so it will be reproduced exactly later.
- Each fixer has a pattern that describes the nodes it wants to match in the parse tree. The tree is traversed while asking each fixer if it matches the given node.
- If the fixer's pattern matches the node, the fixer is ask to transform the code. The fixer can manipulate the parse tree directly.
- A diff against the original source is printed to stdout and optionally written back to the file.
Over the past few weeks, I've written a couple of fixers. It's pretty intuitive once you get the hang of it, but writing good tests is very important because Python's flexible syntax produces many possibilities you fixer must deal with. I also refactored lib2to3, so plugging a different system of fixers is much easier for custom applications. I've also written some documentation on it's usage. I hope to start documenting the API and writing a guide for creating fixers soon, so other people can start making use of lib2to3.
Subscribe to:
Posts (Atom)