Last active
December 10, 2019 08:15
-
-
Save aejh/4618918 to your computer and use it in GitHub Desktop.
A skeleton for python scripts.
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
#!/usr/bin/env python3 | |
# -*- coding: UTF-8 -*- | |
""" | |
An example script | |
""" | |
import logging | |
from os.path import basename | |
logger = logging.getLogger(basename(__file__)) | |
logger.addHandler(logging.NullHandler()) | |
# Classes and module level functions here! | |
def main(): | |
"""CLI handling. | |
""" | |
# The following 3 lines handle Ctrl+C and piping of stdout | |
import signal | |
signal.signal(signal.SIGINT, signal.SIG_DFL) # Ctrl+C to terminate | |
signal.signal(signal.SIGPIPE, signal.SIG_DFL) # Enable sane output piping | |
from argparse import ArgumentParser, RawDescriptionHelpFormatter | |
parser = ArgumentParser(description=__doc__.lstrip(), | |
formatter_class=RawDescriptionHelpFormatter) | |
parser.add_argument("-v", action="store_const", const=logging.DEBUG, | |
dest="loglevel", help="Be verbose", | |
default=logging.INFO) | |
parser.add_argument("-q", action="store_const", const=logging.ERROR, | |
dest="loglevel", help="Be quiet", | |
default=logging.INFO) | |
parser.add_argument("--file-path", help="Any string") | |
parser.add_argument("repeated", help="Repeated arg", nargs="+") | |
args = parser.parse_args() | |
logging.basicConfig(level=args.loglevel, | |
datefmt='%Y-%m-%d %H:%M:%S', | |
format='%(asctime)s.%(msecs)03d %(name)s %(levelname)s ' | |
'"%(message)s" ' | |
'[%(module)s.%(funcName)s:%(lineno)s]') | |
# Program code or module tests here | |
logger.info('Started') | |
print(args) | |
return 0 | |
if __name__ == '__main__': | |
from sys import exit as sys_exit | |
sys_exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment