Difference between revisions of "Python starter script"

From Noah.org
Jump to navigationJump to search
 
(19 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Engineering]]
 
[[Category:Engineering]]
 
[[Category:Python]]
 
[[Category:Python]]
== My template for a Python starter script ==
+
[[Category:Free_Software]]
This is what I use to start most of my simple python scripts.
 
<pre class="code">
 
#!/usr/bin/env python
 
"""This describes how to use the script. This script docstring documents the script
 
and will also be printed by exit_with_usage() if there is a problem or if the
 
user requests help (-?, --help, etc.).
 
  
$Id$
+
== My starter template for a Python CLI script ==
"""
 
import sys, os, traceback
 
import re
 
import getopt
 
from pexpect import run, spawn
 
  
def exit_with_usage():
+
This is my quintessential Python starter template for command-line scripts. This is a elaborate hello world that I use whenever I start a new script.
    print globals()['__doc__']
 
    os._exit(1)
 
  
def parse_args (options=None, long_options=None):
+
What inspired this is that whenever I wrote a shell script I always found myself going back to add readline support; then getopt arg parsing; then exception handling; then help documentation, etc... I'd always end up with this hello world script at the core. So what I did was pull this out and make it pretty.
    try:
 
        optlist, args = getopt.getopt(sys.argv[1:], options+'h?', long_options+['help','h','?'])
 
    except Exception, e:
 
        print str(e)
 
        exit_with_usage()
 
    options = dict(optlist)
 
    if [elem for elem in options if elem in ['-h','--h','-?','--?','--help']]:
 
        exit_with_usage()
 
    return (options, args)
 
  
def main ():
+
To make this even more useful I have Vim use this as a template for new Python files. I added the following lines to my .vimrc:
    (options, args) = parse_args('v')
 
    # if args<=0:
 
    #    exit_with_usage()
 
    if '-v' in options:
 
        verbose_flag = True
 
    else:
 
        verbose_flag = False
 
  
if __name__ == "__main__":
+
<pre>
    try:
+
augroup BufNewFileFromTemplate
        main()
+
au!
    except Exception, e:
+
autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.tpl
        print "ERROR, UNEXPECTED EXCEPTION"
+
autocmd BufNewFile * normal! G"_dd1G
        print str(e)
+
autocmd BufNewFile * silent! match Todo /TODO/
        traceback.print_exc()
+
augroup BufNewFileFromTemplate
        os._exit(1)
 
 
</pre>
 
</pre>
 +
 +
Then I put this script in a file named:
 +
  ~/.vim/templates/py.tpl
 +
Now whenever I start a new Python script Vim will automatically copy this template into my buffer.
 +
 +
Click to download: [http://www.noah.org/engineering/src/dotfiles/.vim/templates/py.tpl py.tpl]
 +
<include src="/home/noahspurrier/noah.org/engineering/src/dotfiles/.vim/templates/py.tpl" highlight="python" />

Latest revision as of 16:57, 26 May 2011


My starter template for a Python CLI script

This is my quintessential Python starter template for command-line scripts. This is a elaborate hello world that I use whenever I start a new script.

What inspired this is that whenever I wrote a shell script I always found myself going back to add readline support; then getopt arg parsing; then exception handling; then help documentation, etc... I'd always end up with this hello world script at the core. So what I did was pull this out and make it pretty.

To make this even more useful I have Vim use this as a template for new Python files. I added the following lines to my .vimrc:

augroup BufNewFileFromTemplate
au!
autocmd BufNewFile * silent! 0r $HOME/.vim/templates/%:e.tpl
autocmd BufNewFile * normal! G"_dd1G
autocmd BufNewFile * silent! match Todo /TODO/
augroup BufNewFileFromTemplate

Then I put this script in a file named:

 ~/.vim/templates/py.tpl

Now whenever I start a new Python script Vim will automatically copy this template into my buffer.

Click to download: py.tpl <include src="/home/noahspurrier/noah.org/engineering/src/dotfiles/.vim/templates/py.tpl" highlight="python" />