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

07/12/2022
by   Zejun Zhang, et al.
0

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.

READ FULL TEXT

page 1

page 2

page 3

page 4

research
03/20/2023

Method Chaining Redux: An Empirical Study of Method Chaining in Java, Kotlin, and Python

There are possible benefits and drawbacks to chaining methods together, ...
research
04/11/2023

A Data Set of Generalizable Python Code Change Patterns

Mining repetitive code changes from version control history is a common ...
research
09/05/2020

Teddy: Automatic Recommendation of Pythonic Idiom Usage For Pull-Based Software Projects

Pythonic code is idiomatic code that follows guiding principles and prac...
research
11/01/2022

Excel Spreadsheet Analyzer

Spreadsheets are widely used in various fields to do large numerical ana...
research
01/04/2023

Optimization of the Analysis of Vital Events Using Threads

Objective: Optimize the time of data analysis of Vital Events (births, d...
research
07/10/2019

Executability of Python Snippets in Stack Overflow

Online resources today contain an abundant amount of code snippets for d...
research
08/10/2021

PyNose: A Test Smell Detector For Python

Similarly to production code, code smells also occur in test code, where...

Please sign up or login with your details

Forgot password? Click here to reset