dream_people 发表于 2013-1-28 19:30:27

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

不知道你是不是用vi,如果是的话,可以用这段代码,我是放在缺省路径下的,取名为pyTool
 
在vi的一个函数行进入命令模式,命令为 :pyTool -func
就会给当前这个函数生成注释
 
类上 运行 :pyTool -cloass
就会给当前类生成注释
 
代码如下
 
#!/bin/env python#coding:utf-8import os,sys,reimport re,timeclass 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('')      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('')      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 commentansy = PyAnsy()if len(sys.argv) < 2:    print """命令行: pyTool option     选项:      -func 添加函数备注"""   sys.exit(-1)flag = sys.argvif len(sys.argv) != 3:    filename = ''else:    filename = sys.argvif flag == '-func':    ansy.FuncComment()elif flag == '-class':    ansy.ClassComment()else:    sys.exit(-1)
页: [1]
查看完整版本: 我在vi里自动给python代码生成注释的代码,linux