`
dream_people
  • 浏览: 59042 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

我在vi里自动给python代码生成注释的代码,linux

阅读更多

不知道你是不是用vi,如果是的话,可以用这段代码,我是放在缺省路径下的,取名为pyTool

 

在vi的一个函数行进入命令模式,命令为 :pyTool -func

就会给当前这个函数生成注释

 

类上 运行 :pyTool -cloass

就会给当前类生成注释

 

代码如下

 

#!/bin/env python
#coding:utf-8

import os,sys,re
import re,time

class PyAnsy:
    author = 'dream.people'

    def Now(self):
        return "%04d/%02d/%02d %02d:%02d" % time.localtime()[:5]

    def GetFile(self,filename):
        _lines = []
        if filename == '':
            while True:
                _line = sys.stdin.readline()
                if _line == '':
                    break
                _lines.append(_line)
        else:
            _lines = open(filename,"r").readlines()

        return _lines
 
    def ClassComment(self):
        line = self.GetFile('')[0]
        if line == '':
            return

        g = re.match("^( *)class  *([^(:]*).*",line)
        if g == None:
            print line
            return

        blank,classname = g.groups()
        blank += "    "
        comment = blank + '"""\n'
        comment += blank + '@author     :    %s\n' % self.author
        comment += blank + 'comment     :    \n'
        comment += blank + 'create date :    %s\n' % self.Now()
        comment += blank + '-----------------------------------------\n'
        comment += blank + 'modify date :    \n'
        comment += blank + '@author     :    \n'
        comment += blank + 'reason      :    \n'
        comment += blank + '"""'

        print line.rstrip()
        print comment

    def FuncComment(self):
        line = self.GetFile('')[0]
        if line == '':
            return

        g = re.match("^( *)def  *([^(]*)\(([^)]*)\).*",line)
        if g == None:
            print line
            return

        blank,fname,params = g.groups()
        params = params.split(",")
        blank += "    "
        comment = blank + '"""\n'
        comment += blank + '@author     :    %s\n' % self.author
        comment += blank + 'comment     :    \n'
        comment += blank + 'parameter   :    \n'
        for param in params:
            comment += blank + '    %s - \n' % param
        comment += blank + 'return value:    \n'
        comment += blank + 'create date :    %s\n' % self.Now()
        comment += blank + '-----------------------------------------\n'
        comment += blank + 'modify date :    \n'
        comment += blank + '@author     :    \n'
        comment += blank + 'reason      :    \n'
        comment += blank + '"""'

        print line.rstrip()
        print comment


ansy = PyAnsy()
if len(sys.argv) < 2:
    print """命令行: pyTool option [filename]
    选项:
        -func 添加函数备注
"""
 
    sys.exit(-1)

flag = sys.argv[1]

if len(sys.argv) != 3:
    filename = ''
else:
    filename = sys.argv[2]

if flag == '-func':
    ansy.FuncComment()
elif flag == '-class':
    ansy.ClassComment()
else:
    sys.exit(-1)

 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics