之前写Python代码,都是随着性子来,想到哪,写到哪,时间长了,发现代码可读性下降,也不利于与其他Pythoner交流。通过阅读一些已有Python模块的代码,归纳出一个程序入口的模板。
纪录下来,备用。
# -*- coding: utf-8 -*-
"""
Name of current .py
xx/xx/20xx
~~~~~~~~
Description of current .py
Copyright
"""
__version__ = ''
__author__ = 'A.TNG <jiyucn@163.com>'
__url__ = 'http://blog.youkuaiyun.com/jiyucn'
__license__ = ''
__docformat__ = 'restructuredtext'
__all__ = []
import sys
import getopt
USAGE = """
.py usage...
"""
def main(argv):
try:
opts, args = getopt.gnu_getopt(argv[1:], "h", ["help"])
for o, a in opts:
if o in ("-h", "--help"):
print USAGE
except getopt.GetoptError:
print >> sys.stderr, USAGE
return 2
if __name__ == '__main__':
sys.exit(main(sys.argv))

本文分享了一个提高Python代码可读性和便于交流的程序入口模板。该模板包括了版本、作者等元信息,并使用getopt模块处理命令行参数。
8994

被折叠的 条评论
为什么被折叠?



