Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need fixer for list.sort(cmp=...) #116

Open
techtonik opened this issue Mar 5, 2015 · 7 comments
Open

Need fixer for list.sort(cmp=...) #116

techtonik opened this issue Mar 5, 2015 · 7 comments

Comments

@techtonik
Copy link
Contributor

Unhandled Python 2 code:

    available_logs.sort(cmp=lambda x1, x2: locale.strcoll(x1['path'], x2['path']))
TypeError: 'cmp' is an invalid keyword argument for this function
@daira
Copy link
Contributor

daira commented Mar 8, 2015

I don't see a straightforward, idiomatic way to replace cmp= with key=. https://wiki.python.org/moin/HowTo/Sorting#The_Old_Way_Using_the_cmp_Parameter gives a way to do so using an auxiliary class, but:

  • the code for this isn't included in six;
  • we don't have other fixers that add substantial amounts of code to the program;
  • this method isn't particularly idiomatic Python 3 (for any given comparison function, there is probably a simpler way to give a corresponding key function than the generic way described at the above link).

@takluyver
Copy link
Contributor

The cmp_to_key wrapper is actually in functools, so we could write a fixer using that.

I agree that in most cases there will be a simpler and probably more efficient key function than using cmp_to_key(). Where the cmp function is supplied as a lambda, you could try to match a pattern like lambda x, y: cmp(x.age, y.age) and translate it to key=lambda x: x.age - but I think that would be a lot of effort for relatively little reward.

@daira
Copy link
Contributor

daira commented Mar 10, 2015

functools.cmp_to_key is only in Python 2.7, and we claim to support Python 2.6. We could make it an opt-in fixer I suppose.

@techtonik
Copy link
Contributor Author

I think that at least getting warning during processing is a must have. Otherwise the error will popup at run-time when line will be triggered.

It may worth to maintain a sane cookbook with explanations why particular problem exists, and how it can be manually adjusted.

@techtonik
Copy link
Contributor Author

@takluyver
Copy link
Contributor

takluyver commented Mar 11, 2015 via email

@daira
Copy link
Contributor

daira commented Mar 12, 2015

The cookbook would be http://python3porting.com/ -- I believe @brettcannon had plans to update it to recommend python-modernize.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants