Difference between revisions of "Python starter script"

From Noah.org
Jump to navigationJump to search
Line 7: Line 7:
 
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.
 
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... So 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.
+
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 add the following lines to my .vimrc:
+
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:
  
 
<pre>
 
<pre>

Revision as of 13:17, 7 December 2007


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 svncat src="file:///home/svn/src/dotfiles/.vim/templates/py.tpl" highlight="python" />