自动简化/重构Python代码(例如,用于循环->列表理解)? [英] Automatically simplifying/refactoring Python code (e.g. for loops -> list comprehension)?

查看:108
本文介绍了自动简化/重构Python代码(例如,用于循环->列表理解)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我非常喜欢使用列表理解时实现的简洁程度.我喜欢做简洁的列表理解:

myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = [x for x in myList if x > 10]

但是,我经常遇到更详细的实现,例如:

myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = []
for i in xrange(0, len(myList)):
    if myList[i] > 10:
        bigNumbers.append(myList[i])

for loop仅浏览一个数据结构(例如myList[])时,通常会有一个简单的列表理解语句,等效于循环.
考虑到这一点,是否有一个重构工具可以将冗长的Python循环转换为简洁的列表理解语句?


上一个 StackOverflow

2to3 是一种重构工具,可以执行任意重构,只要您可以使用语法模式指定它们即可.您可能要寻找的模式是这个

VARIABLE1 = []
for VARIABLE2 in EXPRESSION1:
    if EXPRESSION2:
        VARIABLE1.append(EXPRESSION3)

这可以安全地重构为

VARIABLE1 = [EXPRESSION3 for VARIABLE2 in EXPRESSION1 if EXPRESSION2]

在您的特定示例中,这将给出

bigNumbers = [myList[i] for i in xrange(0, len(myList)) if myList[i] > 10]

然后,您可以进行另一次重构,将xrange(0,N)替​​换为xrange(N), 另一个替换

[VARIABLE1[VARIABLE2] for VARIABLE2 in xrange(len(VARIABLE1)) if EXPRESSION1]

使用

[VARIABLE3 for VARIABLE3 in VARIABLE1 if EXPRESSION1PRIME]

此重构存在几个问题:

  • EXPRESSION1PRIME必须为EXPRESSION1,且所有出现 VARIABLE1[VARIABLE2]替换为VARIABLE3.这是可能的 2to3,但需要显式代码才能进行遍历和替换.
  • 然后,
  • EXPRESSION1PRIME不能再包含以下内容: VARIABLE1.也可以使用显式代码进行检查.
  • 需要为VARIABLE3命名.您选择了x; 没有合理的方法可以自动完成此操作.你可以 为此选择了回收VARIABLE1(即i),但这可能会造成混淆 因为它表明i仍然是索引.选择合成材料可能会起作用 名称,例如VARIABLE1_VARIABLE2(即myList_i),然后检查是否 否则不会使用.
  • 需要确保VARIABLE1 [VARIABLE2]的收益与您获得的收益相同 使用iter(VARIABLE1)时.无法自动执行此操作.

如果您想学习如何编写2to3修复程序,请看 Lennart Regebro的书. /p>

In Python, I really enjoy how concise an implementation can be when using list comprehension. I love to do concise list comprehensions this:

myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = [x for x in myList if x > 10]

However, I often encounter more verbose implementations like this:

myList = [1, 5, 11, 20, 30, 35] #input data
bigNumbers = []
for i in xrange(0, len(myList)):
    if myList[i] > 10:
        bigNumbers.append(myList[i])

When a for loop only looks through one data structure (e.g. myList[]), there is usually a straightforward list comprehension statement that is equivalent to the loop.
With this in mind, is there a refactoring tool that converts verbose Python loops into concise list comprehension statements?


Previous StackOverflow questions have asked for advice on transforming loops into list comprehension. But, I have yet to find a question about automatically converting loops into list comprehension expressions.


Motivation: There are numerous ways to answer the question "what does it mean for code to be clean?" Personally, I find that making code concise and getting rid of some of the fluff tends to make code cleaner and more readable. Naturally there's a line in the sand between "concise code" and "incomprehensible one-liners." Still, I often find it satisfying to write and work with concise code.

解决方案

2to3 is a refactoring tool that can perform arbitrary refactorings, as long as you can specify them with a syntactical pattern. The pattern you might want to look for is this

VARIABLE1 = []
for VARIABLE2 in EXPRESSION1:
    if EXPRESSION2:
        VARIABLE1.append(EXPRESSION3)

This can be refactored safely to

VARIABLE1 = [EXPRESSION3 for VARIABLE2 in EXPRESSION1 if EXPRESSION2]

In your specific example, this would give

bigNumbers = [myList[i] for i in xrange(0, len(myList)) if myList[i] > 10]

Then, you can have another refactoring that replaces xrange(0, N) with xrange(N), and another one that replaces

[VARIABLE1[VARIABLE2] for VARIABLE2 in xrange(len(VARIABLE1)) if EXPRESSION1]

with

[VARIABLE3 for VARIABLE3 in VARIABLE1 if EXPRESSION1PRIME]

There are several problems with this refactoring:

  • EXPRESSION1PRIME must be EXPRESSION1 with all occurrences of VARIABLE1[VARIABLE2] replaced by VARIABLE3. This is possible with 2to3, but requires explicit code to do the traversal and replacement.
  • EXPRESSION1PRIME then must not contain no further occurrences of VARIABLE1. This can also be checked with explicit code.
  • One needs to come up with a name for VARIABLE3. You have chosen x; there is no reasonable way to have this done automatically. You could chose to recycle VARIABLE1 (i.e. i) for that, but that may be confusing as it suggests that i is still an index. It might work to pick a synthetic name, such as VARIABLE1_VARIABLE2 (i.e. myList_i), and check whether that's not used otherwise.
  • One needs to be sure that VARIABLE1[VARIABLE2] yields the same as you get when using iter(VARIABLE1). It's not possible to do this automatically.

If you want to learn how to write 2to3 fixers, take a look at Lennart Regebro's book.

这篇关于自动简化/重构Python代码(例如,用于循环->列表理解)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆