Skip to content

Instantly share code, notes, and snippets.

@novocaine
Created June 29, 2015 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novocaine/381835576664894019a8 to your computer and use it in GitHub Desktop.
Save novocaine/381835576664894019a8 to your computer and use it in GitHub Desktop.
lib2to3 fixer for changing division operator into div()
"""Fixer that replaces / with div() function"""
from ..fixer_base import BaseFix
from ..fixer_util import Call, Name, Node, Comma
class FixDivision(BaseFix):
PATTERN = "term< any+ '/' any+ >"
def transform(self, node, results):
children = node.children
while len(children) >= 3:
left, div, right = children[:3]
args = [left.clone(), Comma(), right.clone()]
if args[0].prefix == " ":
args[0].prefix = ""
div_call = Call(Name('div'), args=args, prefix=node.prefix)
children = [div_call] + children[3:]
node.replace(Node(children=children, type=node.type))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment