Another fun Pycharm plugin was found, which automatically generates code block comments through AI

Often in the process of developing a code block, it is necessary to write a large number of comments to complete the description of the code block. As a programmer, you may often complain that the code blocks developed by others have no comments or the comments are unclear, but we don't want to spend a lot of time writing code block comments.

The plugin I want to talk about today is Mintlify Doc Writer, which not only supports Python, but also supports the automatic generation of document comments in many other languages. For example, programming languages ​​such as Java and JavaScript can automatically generate code block comments through AI.

Since we are using the Python theme, here we use the Pycharm development tool to introduce how to use AI to generate code block comments during the development of python code blocks.

First, install the Mintlify Doc Writer plugin in pycharm. After the installation is complete, you need to restart the pycharm development tool.
picture

Here are a few more commonly used python code blocks to see if you can use AI to generate more appropriate python code block comments. After all, you don’t need to rack your brains to develop code block comments. Why not do it.

# It imports the logger from the loguru module.
from loguru import logger

# It imports the timeit module.
import timeit

def print_logs(message='日志信息'):
    """
    > This function prints a message to the console

    :param message: The message to be printed, defaults to 日志信息 (optional)
    """
    begin = timeit.default_timer()
    logger.info('日志信息:{}'.format(message))
    end = timeit.default_timer()
    logger.info('消耗时间:{} 秒'.format(str(end - begin)))

The above is that we developed a log printing function, and imported two modules loguru and timeit into the code block. We used AI to generate the function description of the function and the comments of the imported modules. It seems that the effect is not bad.

So, I also wanted to develop a slightly less conventional function to see if I could generate comments that would explain what the function does. Below we have developed a function read_file_data that reads file content to automatically add comments through AI.

def read_file_data(n=0):
    """
    This function reads the data from the file and returns the data as a list of lists.

    :param n: The number of lines to read from the file. If n is 0, then the entire file is read, defaults to 0 (optional)
    """

    # It opens the file in read mode.
    with open('数据_{}.txt'.format(n), encoding='utf-8') as file:
        # It reads the data from the file.
        content = file.read()
        # It prints the content of the file.
        print(content.rstrip())

We have executed AI to add comments to each line of the read_file_data function, and added comments to the function, and the effect is still very good. I plan to use AI to generate annotations in future official account articles, haha~

The method of using AI to generate comments: We have installed the Mintlify Doc Writer plug-in for the Pycharm development tool above. When using it, you only need to place the cursor of the mouse on the line where the code needs to be added and use the shortcut key ctrl+shift+., or the right mouse button Then select 'Generate Docs' to directly generate AI annotations.

picture

Guess you like

Origin blog.csdn.net/qq_34160248/article/details/127798229