Skip to content

edemaine/python2coffee

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Automatic Python → CoffeeScript (rough) conversion

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.

Usage

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

Example

test.py is a simple example of Python code reasonably supported by the converter, which results in test.coffee.

Features Supported So Far

  • Python 2 or 3 input
  • Comments
    • Generally preserved in the output
    • Close accidental comment blocks (###)
  • Built-ins
    • Escape this variable and CoffeeScript keyword function
    • None -> null
    • print -> console.log (with warning about final comma in Python 2)
    • assert -> console.assert
    • range (1, 2, or 3 arguments, with especially clean code in for loops)
    • str, bin, oct, hex -> .toString
    • int, float -> parseInt, parseFloat
    • ord -> .charCodeAt
    • chr -> String.fromCharCode
    • isinstance -> instanceof
    • len -> .length
  • Blocks
    • for...in, while, if...elif...else
    • One-line versions of above
    • if not -> unless
    • while not -> until
    • while True -> loop
  • Functions
    • def and lambda
    • *args -> ...args in function definition and function call
    • Add null default return value (for implicit return None), and remove final return keyword (unnecessary in CoffeeScript).
    • Remove spaces between function and arguments in function call
  • Booleans
    • True -> true
    • False -> false
  • Strings
    • Replace "...".format(...) with CoffeeScript interpolation
    • Escape accidental interpolation ("#")
    • Escape raw strings (r'...')
    • Convert \a, \U........, and \ continuations
    • Convert \z to \\z, as CoffeeScript \z means z
    • .startswith -> .startsWith
    • .endswith -> .endsWith
    • .find -> .indexOf
    • .rfind -> .lastIndexOf
    • .lower -> .toLowerCase
    • .upper -> .toUpperCase
    • .strip -> .trim
    • .lstrip -> .trimStart
    • .rstrip -> .trimEnd
  • Regular expressions
    • re.sub partial support
  • Lists/arrays
    • .append -> .push
    • .extend(x) -> .push(...x)
    • .extend([x, y]) -> .push(x, y)
  • Classes
    • class blocks
    • Automatic stripping of first self argument from methods
    • self.foo -> @foo
    • self -> @
    • __init__ -> constructor
    • __str__ -> toString
    • => for closures within methods, -> for all other functions

Installation

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.1

Related Work

This is not the first attempt to automatically convert Python to CoffeeScript.

About

Python to CoffeeScript automatic converter

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published