Welcome, guest | Sign In | My Account | Store | Cart

This will create a header for a Python script. It is a nice way keep a track of what your script does and when it was created, the author of the script, etc..

Update: Now, it will automatically open with one of the two most popular editors, Vim or Emacs! This script has been updated from the second version to replace spaces with underscores in the title, to convert uppercase to lowercase. It also checks to see if there is a script with the same name in the current working directory so it will not overwrite another file. Please leave comments and suggestions.

Python, 80 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
#title           :pyscript.py
#description     :This will create a header for a python script.
#author          :bgw
#date            :20110930
#version         :0.4
#usage           :python pyscript.py
#notes           :
#python_version  :2.6.6  
#==============================================================================

# Import the modules needed to run the script.
from os.path import exists
from time import strftime
import os

title = raw_input("Enter a title for your script: ")

# Add .py to the end of the script.
title = title + '.py'

# Convert all letters to lower case.
title = title.lower()

# Remove spaces from the title.
title = title.replace(' ', '_')

# Check to see if the file exists to not overwrite it.
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 = '======================================='

# Create a file that can be written to.
filename = open(title, 'w')

# Set the date automatically.
date = strftime("%Y%m%d")

# Write the data to the file. 
filename.write('#!/usr/bin/python')
filename.write('\n#title\t\t\t:' + title)
filename.write('\n#description\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:2.6.6')
filename.write('\n#' + div * 2 + '\n')
filename.write('\n')
filename.write('\n')

# Close the file after writing to it.
filename.close()

# Clear the screen. This line of code will not work on Windows.
os.system("clear") 

def select_editor():
    '''Open the file with either the Vim or Emacs editor.'''
    editor = raw_input("Select an number:\n\n1 for Vim.\n2 for Emacs.\n")
    if editor == "1" or editor == "2":
        if editor == "1":
            os.system("vim +12 " + title)
            exit()
        elif editor == "2":
            os.system("emacs +12 " + title)
            exit()
    else:
        os.system("clear")
        print "\nI do not understand your answer.\n"
        print "Press <Ctrl + C> to quit.\n"
        return select_editor()

select_editor()

I use this script every day that I type some code. I thought that other people might find it useful.

5 comments

Enchanter Thunderbird 12 years, 8 months ago  # | flag

If you can modify your code to open the default editor is much better. Because I am emacs user and don't have vim installed :-(

userend (author) 12 years, 8 months ago  # | flag

I will have to add the choice to use vim, emacs or other. I'll install emacs on my computer and figure out how to do that. Thank you for your commentary.

If you delete the "m" in vim it will open.

userend (author) 12 years, 7 months ago  # | flag

There is now the choice to use either Vim or Emacs.

Michael Tobis 12 years, 7 months ago  # | flag

A more unixy solution would be to set your environment variable $EDITOR

If you use csh or tcsh, at the shell prompt, enter: setenv EDITOR editor Replace editor with the editor you want to use (e.g., emacs or vi).

If you use sh, ksh, or bash, at the shell prompt, enter: EDITOR=editor; export EDITOR

Then in your code

system(os.environ["EDITOR"] + " %s" % title)

userend (author) 12 years, 6 months ago  # | flag

Michael

Thanks for the advice. I did not think of that.