论文标题

用Pythonic Idioms自动重构非偶像Python代码来制作Python代码

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

论文作者

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

论文摘要

与其他编程语言(例如Java)相比,Python具有更多的成语,可以使Python代码简洁有效。尽管Pythonic成语在Python社区中被广泛接受,但Python程序员在使用它们时经常面临许多挑战,例如,没有意识到某些Pythonic Idioms或不知道如何正确使用它们。基于对Github上7,638个Python存储库的分析,我们发现可以用Pythonic Idioms实施的非异乎体性Python代码经常出现。不幸的是,没有工具可以将这种非异想天开的代码自动重构为惯用代码。在本文中,我们设计并实施了一种自动重构工具,以使Python代码惯用。我们通过系统地对比Python和Java的抽象语法语法来识别九个Pythonic成语。然后,我们定义用于检测每个Pythonic Idiom的非异乎寻常代码的句法模式。最后,我们设计了原子AST练习操作,并重构步骤将非异乎寻常的代码重构为惯用代码。我们从GitHub测试和审查了超过4,115个重构,适用于1,065个Python项目,并向84个项目提交90个随机抽样重构的拉动请求。这些评估证实了我们在现实世界中的重构工具的高临界性,实用性和实用性。我们的重构工具可以通过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.

扫码加入交流群

加入微信交流群

微信交流群二维码

扫码加入学术交流群,获取更多资源