Last active
January 16, 2020 02:18
-
-
Save logworthy/22011fded55fd9237f4e24218e9d872e to your computer and use it in GitHub Desktop.
Python boilerplate with logging and argparse
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
# -*- coding: utf-8 -*- | |
""" TODO: Update package docstring | |
""" | |
import logging | |
import argparse | |
def str2bool(val): | |
""" string to boolean conversion for argparse | |
""" | |
if isinstance(val, bool): | |
return val | |
if val.lower() in ('yes', 'true', 't', 'y', '1'): | |
return True | |
if val.lower() in ('no', 'false', 'f', 'n', '0'): | |
return False | |
raise argparse.ArgumentTypeError('Boolean value expected.') | |
def handle_arguments(): | |
""" Handle arguments | |
""" | |
# read arguments | |
arg_parser = argparse.ArgumentParser( | |
"TODO: Describe purpose of program" | |
) | |
arg_parser.add_argument( | |
'--sample', | |
type=str, | |
help='TODO: named argument' | |
) | |
arg_parser.add_argument( | |
'--local', | |
type=str2bool, | |
nargs='?', | |
const=True, | |
default=False, | |
help="TODO: optional boolean flag argument" | |
) | |
args = arg_parser.parse_args() | |
return args | |
def main(): | |
""" TODO: describe program | |
""" | |
args = handle_arguments() | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(levelname)s [%(asctime)s]: %(message)s', | |
datefmt='%Y-%m-%d %H:%M:%S' | |
) | |
logging.info("Program started with arguments: %s", vars(args)) | |
#breakpoint() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment