Created
March 9, 2011 21:11
-
-
Save brentp/863014 to your computer and use it in GitHub Desktop.
skeleton python script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
%prog [options] files | |
""" | |
import optparse | |
import sys | |
import gzip | |
from itertools import izip | |
def nopen(f, mode="rb"): | |
""" | |
open a file that's gzipped or return stdin for '-' | |
>>> nopen('-') == sys.stdin, nopen('-', 'w') == sys.stdout | |
(True, True) | |
>>> nopen(sys.argv[0]) | |
<open file '...', mode 'r...> | |
# an already open file. | |
>>> nopen(open(sys.argv[0])) | |
<open file '...', mode 'r...> | |
""" | |
if not isinstance(f, basestring): | |
return f | |
return {"r": sys.stdin, "w": sys.stdout}[mode[0]] if f == "-" \ | |
else gzip.open(f, mode) if f.endswith(".gz") \ | |
else open(f, mode) | |
def tokens(line, sep="\t"): | |
r""" | |
>>> tokens("a\tb\tc\n") | |
['a', 'b', 'c'] | |
""" | |
return line.rstrip("\r\n").split(sep) | |
def reader(fname, header=True, sep="\t"): | |
r""" | |
for each row in the file `fname` generate dicts if `header` is True | |
or lists if `header` is False. The dict keys are drawn from the first | |
line. If `header` is a list of names, those will be used as the dict | |
keys. | |
>>> from StringIO import StringIO | |
>>> get_str = lambda : StringIO("a\tb\tname\n1\t2\tfred\n11\t22\tjane") | |
>>> list(reader(get_str())) | |
[{'a': '1', 'b': '2', 'name': 'fred'}, | |
{'a': '11', 'b': '22', 'name': 'jane'}] | |
>>> list(reader(get_str(), header=False)) | |
[['a', 'b', 'name'], ['1', '2', 'fred'], ['11', '22', 'jane']] | |
""" | |
line_gen = (l.rstrip("\r\n").split(sep) for l in nopen(fname)) | |
if header == True: | |
header = line_gen.next() | |
header[0] = header[0].lstrip("#") | |
if header: | |
for toks in line_gen: | |
yield dict(izip(header, toks)) | |
else: | |
for toks in line_gen: | |
yield toks | |
def main(): | |
p = optparse.OptionParser(__doc__) | |
p.add_option("-n", dest="n", help="number of stuffs", type='int') | |
opts, args = p.parse_args() | |
if (opts.n is None or len(args) == 0): | |
sys.exit(not p.print_help()) | |
if __name__ == "__main__": | |
import doctest | |
if doctest.testmod(optionflags=doctest.ELLIPSIS |\ | |
doctest.NORMALIZE_WHITESPACE).failed == 0: | |
main() |
i have this in my .bashrc::
alias start-script="wget -q -nv -O - http://gist.github.com/raw/863014/script-skeleton.py"
then use::
start-script > new-script.py
i like this alias better
alias start-script="wget -q -nv -O - http://gist.github.com/raw/863014/script-skeleton.py | vim - -c 'set filetype=python'"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
me too.
https://github.com/tanghaibao/jcvi/blob/master/apps/script.py