Python Script using ChatGPT - Learning
I am an Intern Fresh out of College.
The team I work at is bringing internationalization to source code. They need to sift through every bit of English strings within code which can be converted into a placeholder.
There are tens of thousands of English strings part of Logs, Exceptions, placeholders, Labels and whatnot. All of these had to be changed into a standard placeholder.
Very cumbersome task and only someone with the least valuable time has to do the job. I as an intern with a few others got the task to do it.
Now the first problem was to find the strings, manually reading the code was out of the question. A senior developer gave us a regex pattern that we could give to IntelliJ and it would find almost all relevant occurrences of strings that needed to be translated.
The Requirements
Here are the requirements I came up with
Find a string that needs to be replaced by a placeholder
add a placeholder in place of the string in the source code
Add the placeholder to a
.properties
file which the Team can use later as a reference.
The task was boring and we were using trackpads of our MacBooks to select, click, navigate etc. Very hard on our wrists.
I have somewhat experience in scripting in bash and python. Hence I could not resist thinking about how can I make it lighter on my hands and preserve my willpower to complete this task. Only if I could automate things...
Taking inspiration from grep -C
unix command I thought about a script that could do the following.
Go through all the java source files from a directory I give
Find an occurrence of string that matches the regex i have
shown me the context by printing the lines around it.
Ask me for a replacement string and then automatically make the change and also create the
.properties
in the required format.As a bonus add colors to the string that would be changed when prompting
I was thinking about whether to use bash or python and looking at the complexity I thought I would be better off with python. Anyway, it would improve my python scripting skills.
Writing the Script
Suddenly an idea came to my mind. I thought why not tell chatGPT to write it? I didn't expect much but I was quite surprised. I had made it write some basic scripts before such as Pomodoro timers and Diary managing scripts but this seemed to be a complex one.
I wrote a verbose paragraph telling it what I want from python and also adding colors to places. ChatGPT started writing the script in python and I was getting surprised by how well it wrote everything as it was writing line after line of python code.
Some changes had to be made but it produces quite usable code in mere minutes which for me could have taken several hours and definitely would need more attempts to get it correct. It finally turned out to be so good that i shared it with the team and now they no longer need to touch the mouse and scripts do all the job of searching and replacing.
Here is the Python Script :-
import os
import re
import sys
from termcolor import colored
# Define the regular expression pattern
pattern = r'"[^"\n]+ [^"\n]+"'
# Get the directory path from the command-line arguments
if len(sys.argv) < 2:
print("Please provide the directory path as a command-line argument.")
sys.exit(1)
directory = sys.argv[1]
# Create a log file to record changes
log_file = open('change_log.properties', 'w')
# Recursively process all Java source code files in the directory
for root, dirs, files in os.walk(directory):
for filename in files:
# Only process files with a .java extension
if filename.endswith('.java'):
# Construct the full file path
file_path = os.path.join(root, filename)
# Open the file for reading and writing
with open(file_path, 'r+') as file:
# Read all lines from the file
lines = file.readlines()
# Initialize the replacement variable
replacement = ''
# Iterate over each line in the file
for i, line in enumerate(lines):
# Search for the regular expression pattern in the line
match = re.search(pattern, line)
# If a match is found, prompt the user for a replacement string
while match:
# Print the line number and surrounding lines for context
print(f"Found a match in file {file_path} at line {i+1}:")
for j in range(max(0, i-2), min(i+3, len(lines))):
if j == i:
print(colored(lines[j], 'yellow'), end='')
else:
print(lines[j], end='')
# Ask the user for a replacement string
replacement = input("Enter the replacement string (or 'q' to quit): ")
# If the user enters 'q', quit the loop
if replacement == 'q':
print(colored("Replacement cancelled.", 'red'))
break
# Replace the matched string with the user-provided replacement string
old_string = re.search(pattern, line).group()
new_string = '"'+ replacement + '"'
lines[i] = re.sub(pattern, new_string, line)
# Add the change to the log file
log_file.write(f"{replacement}={old_string}\n")
# Search for the regular expression pattern in the updated line
match = re.search(pattern, lines[i])
print(colored("Replacement successful.", 'green'))
# If the user entered 'q', quit the outer loop as well
if replacement == 'q':
break
# Move the file pointer back to the beginning of the file
file.seek(0)
# Write the modified lines back to the file
file.writelines(lines)
# Truncate the file to the new size (in case the new file is shorter than the old one)
file.truncate()
# If the user entered 'q', quit the outer loop as well
if replacement == 'q':
break
Thanks for reading.
I wrote it more for personal records rather than something that people can read. Got this advice from a senior developer. In case you reach the end thanks for reading it.