1. 程式人生 > >python基礎===monkeytype可以自動添加註釋的模塊!

python基礎===monkeytype可以自動添加註釋的模塊!

dir mon working app som 我們 test img htm

monkeytype 一個可以自動添加註釋的模塊!

先要下載:

pip install monkeytype

以官網的sample code為例

#moudle.py

def add(a, b):
    return a + b

這個test.py的文件會調用add()方法

#test.py

from
module import add print(add(1, 2))

我們在cmd窗口執行如下命令:

monkeytype run test.py

程序執行完畢,可以看到返回了3

ok!重點來了,我們發現多了一個monkeytype.sqlite3文件。

官網解釋是這樣的:

By default this will dump call traces into a sqlite database in the file monkeytype.sqlite3

in the current working directory. You can then use the monkeytype command to generate a stub file for a module, or apply the type annotations directly to your code.

在cmd下執行命令:

monkeytype stub some.module

技術分享圖片

可以清楚的看到對a 和 b ,以及add方法的註釋

應用註釋到代碼中:

monkeytype apply some.module

技術分享圖片

再看module.py文件:

技術分享圖片

代碼已經自動打上註釋了。

詳細操作指南,參考:

http://monkeytype.readthedocs.io/en/latest/index.html#

python基礎===monkeytype可以自動添加註釋的模塊!