arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2207.05613cs.SE
收藏

通过使用Python惯用法自动重构非惯用Python代码使Python代码惯用化

Making Python Code Idiomatic by Automatic Refactoring Non-Idiomatic Python Code with Pythonic Idioms

Zejun Zhang, Zhenchang Xing, Xin Xia, Xiwei Xu, Liming Zhu

摘要:

与其他程序设计语言(例如,Java),Python有更多的习惯用法,使Python代码简洁高效。虽然Python语言习惯用法在Python社区中被广泛接受,但Python程序员在使用它们时经常面临许多挑战,例如,不知道某些Python语言习惯用法或不知道如何正确使用它们。基于对GitHub上7,638个Python存储库的分析,我们发现可以使用Python习语实现的非惯用Python代码频繁且广泛地出现。不幸的是,没有工具可以自动将这种非惯用代码重构为惯用代码。在本文中,我们设计并实现了一个自动重构工具,使Python代码的地道。通过系统地对比Python和Java的抽象语法,我们确定了九个Python习惯用法。然后,我们定义的语法模式,用于检测非惯用代码的每一个Python的习惯用法。最后,我们设计了原子AST重写操作和重构步骤,将非惯用代码重构为惯用代码。我们测试和审查了来自GitHub的1,065个Python项目的4,115个重构,并向84个项目提交了90个随机采样重构的90个pull请求。这些评估证实了我们的重构工具在真实Python代码上的高准确性,实用性和实用性。我们的重构工具可以通过47.242.131.128:5000访问。

英文摘要:

Compared to other programming languages (e.g., Java), Python has more idioms to make Python code concise and efficient. Although pythonic idioms are well accepted in the Python community, Python programmers are often faced with many challenges in using them, for example, being unaware of certain pythonic idioms or do not know how to use them properly. Based on an analysis of 7,638 Python repositories on GitHub, we find that non-idiomatic Python code that can be implemented with pythonic idioms occurs frequently and widely. Unfortunately, there is no tool for automatically refactoring such non-idiomatic code into idiomatic code. In this paper, we design and implement an automatic refactoring tool to make Python code idiomatic. We identify nine pythonic idioms by systematically contrasting the abstract syntax grammar of Python and Java. Then we define the syntactic patterns for detecting non-idiomatic code for each pythonic idiom. Finally, we devise atomic AST-rewriting operations and refactoring steps to refactor non-idiomatic code into idiomatic code. We test and review over 4,115 refactorings applied to 1,065 Python projects from GitHub, and submit 90 pull requests for the 90 randomly sampled refactorings to 84 projects. These evaluations confirm the high-accuracy, practicality and usefulness of our refactoring tool on real-world Python code. Our refactoring tool can be accessed at 47.242.131.128:5000.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑