It's Awesome To Use Chat GPT To Write Docstring For Python Functions
I just found another helpful feature that Chat GPT can do for me. As I'm coding away, I suddenly have the need to write a docstring for a helper function, but I'm too lazy. What am I supposed to do? Easy! Chat GPT can help me with this! Here is how the story actually unfolds. So, I was trying to download some YFinance data for a stock app that I'm coding, and I needed to write this helper function:
def download_stock_data_for_wcr_view(ticker_list, start_date, end_date):
stock_data_list = []
for t in ticker_list:
if not os.path.exists(f'tools/data/{t}.csv'):
stock_data = yf.download(f'{t}', start=start_date, end=end_date)
if os.path.exists('tools/data'):
stock_data.to_csv(f'tools/data/{t}.csv')
stock_data_list.append(f'stock_data_{t}: tools/data/{t}.csv')
else:
stock_data_list.append(f'stock_data_{t}: tools/data/{t}.csv')
return stock_data_list
To help remind me what this function will do in the future, I need an excellent docstring detailing what this function can do. I hate writing docstring on my own most of the time! It's tedious, and if I'm lazy I could write a bad docstring. I went to Chat GPT and asked for help. It showed me the version below with the correct docstring.
def download_stock_data_for_wcr_view(ticker_list, start_date, end_date):
"""
This function downloads the stock data of a list of tickers in the given date range and stores it as a CSV file in the 'tools/data/' directory.
Params:
ticker_list (list): List of stock tickers.
start_date (str): Start date in the format 'YYYY-MM-DD'.
end_date (str): End date in the format 'YYYY-MM-DD'.
Returns:
stock_data_list (list): A list of file paths where the stock data for each ticker is stored as a CSV file.
"""
stock_data_list = []
for t in ticker_list:
if not os.path.exists(f'tools/data/{t}.csv'):
stock_data = yf.download(f'{t}', start=start_date, end=end_date)
if os.path.exists('tools/data'):
stock_data.to_csv(f'tools/data/{t}.csv')
stock_data_list.append(f'stock_data_{t}: tools/data/{t}.csv')
else:
stock_data_list.append(f'stock_data_{t}: tools/data/{t}.csv')
return stock_data_list
Isn't this cool?