又发现一个好玩的 Pycharm 插件,通过AI自动生成代码块注释

经常在开发代码块的过程中都需要写大量的注释来完成对代码块的说明。作为一名程序猿可能经常在抱怨别人开发的代码块没有注释或是注释不清楚,但我们自己又不想把大量的时间花费在代码块注释的编写上面。

今天要说的这款插件是Mintlify Doc Writer,不仅支持Python,同时还支持了多种其他语言的文档注释的自动生成。比如,Java、JavaScript等等编程语言都可以通过AI自动生成代码块注释。

由于我们是以Python主题,这里使用Pycharm开发工具来介绍如何在开发python代码块的过程中使用AI生成代码块的注释。

首先,在pycharm中安装Mintlify Doc Writer插件,安装完成之后需要重新启动pycharm开发工具。
图片

这里开发几个比较常用的python代码块看看能否使用AI生成比较合适的python代码块注释,毕竟不用再绞尽脑汁的开发代码块的注释了,何乐而不为呢。

# 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)))

上面是我们开发了一个日志打印的函数,并且导入两个模块loguru、timeit到代码块中,我们使用AI生成了函数的功能说明以及导入的模块的注释,看起来效果还可以。

于是,我还想开发一个稍微不太常规的函数,看看能否生成能说明函数作用的注释效果。下面我们开发了一个读取文件内容的函数read_file_data来通过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())

我们给read_file_data函数的每一行都执行了AI添加注释,并且给函数也添加了注释,效果也还是很理想的。准备在以后的公众号文章案例中就是用AI来生成注释了,哈哈~

使用AI生成注释的方法:上面我们已经给Pycharm开发工具安装了Mintlify Doc Writer插件,使用时只需要将鼠标的光标放在需要添加注释的代码所在行上面使用快捷键ctrl+shift+.,或者鼠标右键后选择’Generate Docs’即可直接生成AI注释。

图片

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年7月15日
下一篇 2023年7月15日

相关推荐