derwiki / scripts

Repository of shell scripts that have made my life easier
10 stars 3 forks source link

make abstract base class for boilerplate Python #3

Open derwiki opened 13 years ago

derwiki commented 13 years ago

A lot of the features of json2csv.py would be useful in other scripts. It would be nice to make the marginal cost of a new script to be just the transform function -- to work with stdin/out or --input, --output for free.

derwiki commented 13 years ago

options = dict() for arg in sys.argv[1:]: try: key, value = arg.strip().split('=') key = key[2:].replace('-', '_') options[key] = value except ValueError: pass

try: fin = open(options['infile'], 'r') except KeyError: fin = sys.stdin

try: fout = open(options['outfile'], options.get('write_mode', 'w+')) except KeyError: fout = sys.stdout

derwiki commented 13 years ago

Ugh, spacing is awful on that last comment. options is a kludge, it needs replaced with argParser or optionParser -- the fin/fout stuff seems pretty clean though.