Created
June 29, 2015 10:33
-
-
Save novocaine/381835576664894019a8 to your computer and use it in GitHub Desktop.
lib2to3 fixer for changing division operator into div()
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
"""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