Skip to content

Instantly share code, notes, and snippets.

@jaytaylor
Last active February 5, 2024 17:21
Show Gist options
  • Save jaytaylor/0ab845a4b88747e58d2774feb9ea7a29 to your computer and use it in GitHub Desktop.
Save jaytaylor/0ab845a4b88747e58d2774feb9ea7a29 to your computer and use it in GitHub Desktop.
Jay's Python programming skeleton snippets quick reference
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def main(args):
try:
print('hi from %s' % (__name__,))
except BaseException as e:
print('Error: %s' % (e,))
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Fancy python bare bones skeleton.
This file header comment will become the nicely
formatted -h/--help description.
"""
import argparse
import logging
import sys
formatter = logging.Formatter('%(asctime)s, %(levelname)s %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
def parse_opts(args):
parser = argparse.ArgumentParser(
argument_default=False,
description=__doc__,
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument('-v', '--verbose', default=False, action='store_true', help='Display verbose log output')
opts = parser.parse_args(args)
if opts.verbose:
logger.setLevel(logging.DEBUG)
return opts
def main(args):
try:
opts = parse_opts(args)
logger.info('hi from %s' % (__name__,))
except BaseException as e:
logger.exception('Problem encountered in main()')
return 1
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment