Last active
August 4, 2022 13:52
-
-
Save ftfarias/5b9ed6991d1b0d2d370a4526acde72c9 to your computer and use it in GitHub Desktop.
basic general all-purpose python file template
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
""" | |
{USAGE} | |
python thisfile.py param1 | |
""" | |
import sys | |
import os | |
import pprint as pp | |
import argparse | |
import logging | |
import traceback | |
try: | |
import tqdm | |
except: | |
pass | |
#logging.basicConfig(level=logging.WARNING) | |
logger = logging.getLogger(__name__) | |
MB = 1024 * 1024 | |
# create console handler and set level to debug | |
ch = logging.handlers.RotatingFileHandler('extractor.log', mode='a', maxBytes=50 * MB, backupCount=5, encoding='utf-8') | |
ch.setLevel(logging.INFO) | |
# create formatter | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
# add formatter to ch | |
ch.setFormatter(formatter) | |
# add ch to logger | |
app.logger.addHandler(ch) | |
app.logger.setLevel(logging.INFO) | |
logger.info('*** STARTING EXTRACTOR ***') | |
logger.debug('debug message') | |
logger.info('info message') | |
logger.warn('warn message') | |
logger.error('error message') | |
logger.critical('critical message') | |
def factorial(n): | |
"""Return the factorial of n, an exact integer >= 0. | |
>>> [factorial(n) for n in range(6)] | |
[1, 1, 2, 6, 24, 120] | |
>>> factorial(30) | |
265252859812191058636308480000000 | |
>>> factorial(-1) | |
Traceback (most recent call last): | |
... | |
ValueError: n must be >= 0 | |
Factorials of floats are OK, but the float must be an exact integer: | |
>>> factorial(30.1) | |
Traceback (most recent call last): | |
... | |
ValueError: n must be exact integer | |
>>> factorial(30.0) | |
265252859812191058636308480000000 | |
It must also not be ridiculously large: | |
>>> factorial(1e100) | |
Traceback (most recent call last): | |
... | |
OverflowError: n too large | |
""" | |
import math | |
if not n >= 0: | |
raise ValueError("n must be >= 0") | |
if math.floor(n) != n: | |
raise ValueError("n must be exact integer") | |
if n+1 == n: # catch a value like 1e300 | |
raise OverflowError("n too large") | |
result = 1 | |
factor = 2 | |
while factor <= n: | |
result *= factor | |
factor += 1 | |
return result | |
def main(args): | |
try: | |
pass | |
except: | |
traceback.print_exc(file=sys.stdout) | |
# import ipdb; ipdb.set_trace() # debugging starts here | |
if __name__ == '__main__': | |
# Parses the command line | |
# https://docs.python.org/3/library/argparse.html | |
parser = argparse.ArgumentParser(description='Basic command line Python script') | |
# parser.add_argument('name', type=str, help='random param', default='', nargs='?', action='store') | |
# parser.add_argument('values', type=, help='list of numbers', default='', nargs='*') | |
parser.add_argument('--test', '-t', help='executes a internal self-test', action='store_true') | |
parser.add_argument('--verbose', '-v', action='count') | |
parser.add_argument('--version', action='version', version='%(prog)s 1.0') | |
args = parser.parse_args() | |
# configures the logger and verbosity level | |
verbose_level = args.verbose | |
if verbose_level == 1: | |
logger.setLevel(logging.INFO) | |
if verbose_level >= 2: | |
logger.setLevel(logging.DEBUG) | |
logger.info('*** Begin ***') | |
if args.test: | |
logger.info('*** Self Testing ***') | |
import doctest | |
doctest.testmod(verbose=True) | |
else: | |
main(args) | |
logger.info('*** End ***') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment