經(jīng)常在開(kāi)發(fā)代碼塊的過(guò)程中都需要寫(xiě)大量的注釋來(lái)完成對(duì)代碼塊的說(shuō)明。作為一名程序猿可能經(jīng)常在抱怨別人開(kāi)發(fā)的代碼塊沒(méi)有注釋或是注釋不清楚,但我們自己又不想把大量的時(shí)間花費(fèi)在代碼塊注釋的編寫(xiě)上面。
今天要說(shuō)的這款插件是Mintlify Doc Writer,不僅支持Python,同時(shí)還支持了多種其他語(yǔ)言的文檔注釋的自動(dòng)生成。比如,Java、JavaScript等等編程語(yǔ)言都可以通過(guò)AI自動(dòng)生成代碼塊注釋。
由于我們是以Python主題,這里使用Pycharm開(kāi)發(fā)工具來(lái)介紹如何在開(kāi)發(fā)python代碼塊的過(guò)程中使用AI生成代碼塊的注釋。
首先,在pycharm中安裝Mintlify Doc Writer插件,安裝完成之后需要重新啟動(dòng)pycharm開(kāi)發(fā)工具。
這里開(kāi)發(fā)幾個(gè)比較常用的python代碼塊看看能否使用AI生成比較合適的python代碼塊注釋?zhuān)吘共挥迷俳g盡腦汁的開(kāi)發(fā)代碼塊的注釋了,何樂(lè)而不為呢。
# 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('消耗時(shí)間:{} 秒'.format(str(end - begin)))
上面是我們開(kāi)發(fā)了一個(gè)日志打印的函數(shù),并且導(dǎo)入兩個(gè)模塊loguru、timeit到代碼塊中,我們使用AI生成了函數(shù)的功能說(shuō)明以及導(dǎo)入的模塊的注釋?zhuān)雌饋?lái)效果還可以。
于是,我還想開(kāi)發(fā)一個(gè)稍微不太常規(guī)的函數(shù),看看能否生成能說(shuō)明函數(shù)作用的注釋效果。下面我們開(kāi)發(fā)了一個(gè)讀取文件內(nèi)容的函數(shù)read_file_data來(lái)通過(guò)AI自動(dòng)添加注釋。
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('數(shù)據(jù)_{}.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函數(shù)的每一行都執(zhí)行了AI添加注釋?zhuān)⑶医o函數(shù)也添加了注釋?zhuān)Ч策€是很理想的。準(zhǔn)備在以后的公眾號(hào)文章案例中就是用AI來(lái)生成注釋了,哈哈~
使用AI生成注釋的方法:上面我們已經(jīng)給Pycharm開(kāi)發(fā)工具安裝了Mintlify Doc Writer插件,使用時(shí)只需要將鼠標(biāo)的光標(biāo)放在需要添加注釋的代碼所在行上面使用快捷鍵ctrl+shift+.,或者鼠標(biāo)右鍵后選擇’Generate Docs’即可直接生成AI注釋。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-515538.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-515538.html
到了這里,關(guān)于又發(fā)現(xiàn)一個(gè)好玩的 Pycharm 插件,通過(guò)AI自動(dòng)生成代碼塊注釋的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!