Difference between revisions of "Python starter script"
From Noah.org
Jump to navigationJump to searchLine 5: | Line 5: | ||
== My starter template for a Python CLI script == | == My starter template for a Python CLI script == | ||
− | This is my quintessential Python starter script. | + | This is my quintessential Python starter script. This is what I use to start most of my python scripts. This is geared toward a command-line script. This is a elaborate hello world that I use whenever I start a new script. |
− | This is what I use to start most of my python scripts. | + | |
− | This is geared toward a command-line 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. |
+ | |||
+ | To make this even more useful I add the following lines to my .vimrc: | ||
+ | |||
+ | <pre> | ||
+ | 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 | ||
+ | </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. | ||
<pre class="code"> | <pre class="code"> |
Revision as of 13:45, 16 August 2007
My starter template for a Python CLI script
This is my quintessential Python starter script. This is what I use to start most of my python scripts. This is geared toward a command-line script. 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.
To make this even more useful I add 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.
#!/usr/bin/env python """This describes how to use the script. This docstring will be printed by exit_with_usage() if there is a problem or if the user requests help (-?, --help, etc.). $Id$ """ import sys, os, traceback import re import getopt import time from pexpect import run, spawn def exit_with_usage (): print globals()['__doc__'] os._exit(1) def parse_args (options='', long_options=[]): 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 (): (options, args) = parse_args('v') # if args<=0: # exit_with_usage() if '-v' in options: verbose = True else: verbose = False # TODO: Do something if __name__ == '__main__': try: start_time = time.time() print time.asctime() main() print time.asctime() print "TOTAL TIME IN MINUTES:", print (time.time() - start_time) / 60.0 sys.exit(0) except SystemExit, e: raise e except Exception, e: print 'ERROR, UNEXPECTED EXCEPTION' print str(e) traceback.print_exc() os._exit(1)