This project is a rough experiment to see how easily/effectively we can automatically convert Python code into equivalent CoffeeScript code, inspired by the CoffeeScript for Python Programmers guide. The goal is to preserve the existing code's indentation style, comments, etc., but it's still very much a work-in-progress, is not feature complete, and may incorrectly convert some features, so please use with care. Hopefully, if you have some Python code to convert to CoffeeScript, this tool will provide a useful starting point, but you will almost certainly still need to do some manual fixes.
If your code is in Python 3, the following will convert filename.py to
filename.coffee:
python2coffee.py filename.py
If your code is in Python 2, use the following instead:
python2coffee.py -p 2 filename.py
test.py is a simple example of Python code reasonably supported by the converter, which results in test.coffee.
- Python 2 or 3 input
- Comments
- Generally preserved in the output
- Close accidental comment blocks (
###)
- Built-ins
- Escape
thisvariable and CoffeeScript keywordfunction None->nullprint->console.log(with warning about final comma in Python 2)assert->console.assertrange(1, 2, or 3 arguments, with especially clean code inforloops)str,bin,oct,hex->.toStringint,float->parseInt,parseFloatord->.charCodeAtchr->String.fromCharCodeisinstance->instanceoflen->.length
- Escape
- Blocks
for...in,while,if...elif...else- One-line versions of above
if not->unlesswhile not->untilwhile True->loop
- Functions
defandlambda*args->...argsin function definition and function call- Add
nulldefault return value (for implicitreturn None), and remove finalreturnkeyword (unnecessary in CoffeeScript). - Remove spaces between function and arguments in function call
- Booleans
True->trueFalse->false
- Strings
- Replace
"...".format(...)with CoffeeScript interpolation - Escape accidental interpolation (
"#") - Escape raw strings (
r'...') - Convert
\a,\U........, and\continuations - Convert
\zto\\z, as CoffeeScript\zmeansz .startswith->.startsWith.endswith->.endsWith.find->.indexOf.rfind->.lastIndexOf.lower->.toLowerCase.upper->.toUpperCase.strip->.trim.lstrip->.trimStart.rstrip->.trimEnd
- Replace
- Regular expressions
re.subpartial support
- Lists/arrays
.append->.push.extend(x)->.push(...x).extend([x, y])->.push(x, y)
- Classes
classblocks- Automatic stripping of first
selfargument from methods self.foo->@fooself->@__init__->constructor__str__->toString=>for closures within methods,->for all other functions
This project is based on the excellent parso Python parser, which must first be installed. If you want to convert Python 2 files, you should install parso version 0.7.1:
pip3 install parso==0.7.1This is not the first attempt to automatically convert Python to CoffeeScript.
- python-to-coffeescript
translates Python syntax to CoffeeScript syntax, but does not try to
preserve semantics.
It's based on a mix of
astparsing and tokenization to preserve comments etc. - Transcrypt converts Python to JavaScript.
- pyscript may also convert Python to JavaScript.
- Rapydscript (NG) is a language similar to Python that compiles directly to JavaScript (like CoffeeScript).