Created
June 2, 2015 12:30
-
-
Save novocaine/c384b2a1b070e149f281 to your computer and use it in GitHub Desktop.
Using redbaron to find sort and sorted using cmp so we can convert them to key for python 3
This file contains 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
from redbaron import RedBaron | |
import os | |
import sys | |
import io | |
def is_bad_sort(node): | |
# must be .method invocation | |
if node.parent.type != 'atomtrailers': | |
return False | |
# not a function invocation | |
if node.index_on_parent == 0: | |
return False | |
# must be called() | |
if node.next is None or node.next.type != 'call': | |
return False | |
call = node.next | |
arglist = call.value | |
# if it doesn't have any arguments, its fine | |
if len(arglist) == 0: | |
return False | |
# first argument is not a kvarg, must be comparator | |
if arglist[0].target is None: | |
return True | |
# all kvargs, try to find cmp in them | |
for arg in arglist: | |
if arg.target.value == "cmp": | |
return True | |
return False | |
def is_bad_sorted(node): | |
# must be function invocation | |
if node.parent.type != 'atomtrailers': | |
return False | |
if node.index_on_parent != 0: | |
return False | |
# must be called() | |
if node.next is None or node.next.type != 'call': | |
return False | |
call = node.next | |
arglist = call.value | |
# if it only has one argument, its fine | |
if len(arglist) <= 1: | |
return False | |
# second argument is not a kvarg, must be comparator | |
if arglist[1].target is None: | |
return True | |
# second argument is a kvarg, so try to find cmp in | |
# any remaining args | |
for arg in arglist[1:]: | |
if arg.target.value == "cmp": | |
return True | |
return False | |
def process(src): | |
""" | |
>>> process('x.sort(d)') | |
'1:x.sort(d)' | |
>>> process('x.sort()') | |
'' | |
>>> process('sort(d)') | |
'' | |
>>> process('x.sort(cmp=c)') | |
'1:x.sort(cmp=c)' | |
>>> process('x.sort(reverse=True, cmp=c)') | |
'1:x.sort(reverse=True, cmp=c)' | |
>>> process('x.sort(d, c)') | |
'1:x.sort(d, c)' | |
>>> process('x.sorted(d)') | |
'' | |
>>> process('sorted(x, cmp)') | |
'1:sorted(x, cmp)' | |
>>> process('sorted(x)') | |
'' | |
>>> process('sorted(x, reverse=True, cmp=c)') | |
'1:sorted(x, reverse=True, cmp=c)' | |
""" | |
output = io.BytesIO() | |
for sort in RedBaron(src).find_all('name', value='sort'): | |
if is_bad_sort(sort): | |
output.write("%d:%s" % | |
(sort.absolute_bounding_box.top_left.line, sort.parent)) | |
for sort in RedBaron(src).find_all('name', value='sorted'): | |
if is_bad_sorted(sort): | |
output.write("%d:%s" % | |
(sort.absolute_bounding_box.top_left.line, sort.parent)) | |
return output.getvalue() | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
for f in sys.argv[1:]: | |
s = process(open(f).read()) | |
if s: | |
print f | |
print s | |
else: | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment