Python implements sample code for automatically adding script header information, and python sample code
Preface
Each person may write a script in a different format. Some may indicate some information about the script itself, while others may open the door. In fact, there is nothing in the small team. You also know what others do, however, it would be troublesome to put a large team, because as the number of people increases, scripts become exponential growth. If the style of each person is not uniform, it will lead to very large drawbacks in the end, therefore, when the number of teams increases, there must be a set of standards to form a unified coding rule, so that even if you do not look at the specific implementation of the script, you will know what the function of this script is.
A script we shared today is a script that automatically adds annotation information. The added information includes the Script Name, author, time, description, script usage, language version, and remarks. The following shows the sample code.
#!/usr/bin/env python from os.path import existsfrom time import strftimeimport os title = raw_input("Enter a title for your script: ") title = title + '.py' title = title.lower() title = title.replace(' ', '_') if exists(title): print "\nA script with this name already exists." exit(1) descrpt = raw_input("Enter a description: ")name = raw_input("Enter your name: ")ver = raw_input("Enter the version number: ")div = '=======================================' filename = open(title, 'w') date = strftime("%Y%m%d") filename.write('#!/usr/bin/python')filename.write('\n#title\t\t\t:' + title)filename.write('\n#description\t\t:' + descrpt)filename.write('\n#author\t\t\t:' + name)filename.write('\n#date\t\t\t:' + date)filename.write('\n#version\t\t:' + ver)filename.write('\n#usage\t\t\t:' + 'python ' + title)filename.write('\n#notes\t\t\t:')filename.write('\n#python_version\t\t:2.6.6')filename.write('\n#' + div * 2 + '\n')filename.write('\n')filename.write('\n') filename.close() os.system("clear") os.system("vim +12 " + title)exit()
The script does not explain too much. It basically gets the information and writes it into a file. The script is simple enough. Let's take a look at the generated results:
#! /Usr/bin/python # title: test4.py # description: I am test script # author: python technology # date: 20160902 # version: 0.1 # usage: python test4.py # notes: # python_version: 2.6.6 # =================================================== ==========================================================
Summary
If you think about the basic information in front of each script, is it clear? I hope this script will help you. If you have any questions, please leave a message. Thank you for your support.