python 类 参数增加_vim为python自动添加函数参数类型

这个插件正在被完善中,如果您有什么好的idea,请在评论区给出,让我们一起完成一个超级棒的插件。

这个插件原来的名字是agfp(automatic generate function parameters),我觉得这个名字实在是反人类,就把它改成了vim-autodoc。大家请使用新的插件。

目前只支持python3

上周三的在《人工智能》课堂Lab中,助教给我们发了一份python代码,我看着里面的函数一头包:

def enumerate_all(variables, e, bn):

if not variables:

return 1.0

Y, rest = variables[0], variables[1:]

Ynode = bn.variable_node(Y)

# YOUR_CODE_HERE (hint: an if-statement)

# END_YOUR_CODE

def all_events(variables, e):

if not variables:

yield e

else:

X, rest = variables[0], variables[1:]

for e1 in all_events(rest, e):

for x in [True, False]:

yield extend(e1, X, x)

天知道这里面每个函数的参数是什么。当时我采用的方式是加断点调试来观察变量类型,光这个就花费了我很多时间。

当时我就有这个想法:为什么不能让程序自己运行一遍,然后让它自己在对应的位置加上参数的类型注释呢?

于是就有了这个插件:sillybun/vim-autodoc​github.com

这个插件的目的是执行一遍可以运行的python脚本,然后加上每个参数对应的类型的注释。sillybun/vim-autodoc这个插件的目的是执行一遍可以运行的python脚本,然后加上每个参数对应的类型的注释。

如果上面的代码在插件执行后自动的变成:

def enumerate_all(variables, e, bn):

"""@type variables: list[str],list@type e: dict(str=>bool)@type bn: BayesNet"""

if not variables:

return 1.0

Y, rest = variables[0], variables[1:]

Ynode = bn.variable_node(Y)

# YOUR_CODE_HERE (hint: an if-statement)

if Y in e:

return Ynode.p(e[Y], e) * enumerate_all(rest, e, bn)

else:

return sum(Ynode.p(y, e) * enumerate_all(rest, extend(e, Y, y), bn) for y in bn.variable_values(Y))

# END_YOUR_CODE

def all_events(variables, e):

"""@type variables: list[str],list@type e: dict(str=>bool),dict"""

if not variables:

yield e

else:

X, rest = variables[0], variables[1:]

for e1 in all_events(rest, e):

for x in [True, False]:

yield extend(e1, X, x)

给函数添加类型注释的docstring有以下好处:增加程序的可读性

本来自动补全工具(比如youcompleteme)无法针对函数的参数进行语义的补全,这样添加类型注释之后,使对参数的语义补全成为了可能。

安装

建议使用插件管理软件

Vundle:

Bundle "sillybun/vim-autodoc"

vim-plug

Plug "sillybun/vim-autodoc"

使用方式:

运行命令:

“ 给所有函数加上docstring

:RecordParameter

“ 给当前光标下的函数加上docstring

:RecordCurrentFunction

运行前样例程序:

def f(a, b=10):

return a + b

f(1, 2)

运行后样例程序:

def f(a, b=10):

"""@type a: int@type b: int"""

return a + b

f(1, 2)

同样支持PEP484风格:

def f(a: int, b: int=10) -> int:

return a + b

f(1, 2)

插件中还加入了统计函数调用次数和总运行时间的功能(这个功能很有可能在3.0版本提供用户使用vad语言自定义的版本):

def main():

print(gcd(15, 10))

print(gcd(45, 12))

def gcd(a, b):

while b:

a, b = b, a%b

return a

运行后变成:

def main() -> None:

"""called number: 1total time: 8.606910705566406e-05s"""

print(gcd(15, 10))

print(gcd(45, 12))

def gcd(a: int, b: int) -> int:

"""called number: 2total time: 1.6689300537109375e-06s"""

while b:

a, b = b, a%b

return a

if __name__ == "__main__":

main()

插件同样提供了运行参数的支持。

比如另一个文件testgcd调用了gcd:

import gcd

gcd.gcd(1, 2)

在gcd.py文件下,输入:

python testgcd.py 或者 testgcd.py

便会自动运行testgcd.py并且记录gcd模块中的函数的调用情况。

参数设置

let g:autodoc_display_return_type=1

1表示输出返回值的类型,0不输出。

let g:autodoc_typehint_style = "pep484"

注释风格。

let g:autodoc_display_runtime_info = 0

是否统计函数运行信息。如果您觉得这个插件对您有用或者很赞赏作者的工作,请给本文点个赞,或者在github上给这个插件一个星,以便让更多的人能了解到这个插件,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值