my python template
Table of Content
my python template¶
This will be updated occassionally.
I use python-json-logger to use json format for logging.
I use python-dotenv to use environment variables from .env file.
"""SCRIPT_NAME_HERE
ENTER_SCRIPT_DESCRIPTION_HERE
# additional packages
python-json-logger
"""
# system and logging
import sys
import os
import logging
from pythonjsonlogger import jsonlogger
import logging.handlers as handlers
# arg
import argparse
# csv, json
# import csv
# import json
# regular expression
# import re
# thread
# import concurrent.futures
# process pool executor
# executer = concurrent.futures.ProcessPoolExecutor(max_workers=16)
# futures = []
# usage:
# executer.submit(`function`, `arg`, `and more arg as needed`)
# example:
# future = executer.submit(netmiko_autodetect, target, host, id)
# futures.append(future)
# getting the result from the threads
# for future in concurrent.futures.as_completed(futures):
# device_type, id, timestamp = future.result()
# memory trace
import tracemalloc
'''
# env, python-dotenv required
from dotenv import load_dotenv
basedir = os.path.abspath(os.path.dirname('.'))
load_dotenv(os.path.join(basedir, '.env'))
'''
# global variables
# MY_VAR = os.environ.get('ENV_KEY_TO_USE', default_value_if_the_key_doesnt_exist)
# classes
# functions
def parse_options():
# parser
parser = argparse.ArgumentParser(
add_help=True,
description="ENTER_SCRIPT_DESCRIPTION_HERE",
)
# switch options
parser.add_argument('--debug', '-d', action='store_true')
parser.add_argument('--test', action='store_true', help=argparse.SUPPRESS)
parser.add_argument(
'--run',
action='store_true',
help="Run the script"
)
# process args
if len(sys.argv) > 1:
args, unknown = parser.parse_known_args()
return args, parser
else:
# print help and still proceed with --test and --debug
parser.print_help()
return sys.exit(1)
def logger_setup(options):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# set console logging
logHandler = logging.StreamHandler()
# format
format_str = "%(asctime)s %(levelname)s %(message)s"
# format_str = "%(asctime)s %(process)d %(processName)s %(thread)d %(threadName)s %(levelname)s %(message)s"
formatter = jsonlogger.JsonFormatter(format_str)
logHandler.setFormatter(formatter)
# debug
if options.debug:
logHandler.setLevel(logging.DEBUG)
else:
logHandler.setLevel(logging.INFO)
logger.addHandler(logHandler)
return logger
def memstats():
current, peak = tracemalloc.get_traced_memory()
logger.debug(f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB")
return 0
def main():
# memory trace
if options.debug:
tracemalloc.start()
# run
if options.test:
memstats()
logger.info("test")
# stop memory trace
if options.debug:
tracemalloc.stop()
# use parser.print_help() wherever appropriate
return 0
if __name__ == "__main__":
# argument parser and logger
options, parser = parse_options()
logger = logger_setup(options)
main()
python document¶
https://docs.python.org/3/index.html
python lifecycle¶
https://devguide.python.org/versions/
another template without python-json-logger¶
"""SCRIPT_NAME_HERE
ENTER_SCRIPT_DESCRIPTION_HERE
"""
# system and logging
import sys
import os
import logging
# arg
import argparse
# memory trace
import tracemalloc
# global variables
MY_VAR = os.environ.get("ENV_KEY_TO_USE", "default_value_if_the_key_doesnt_exist")
# classes
# functions
def parse_options():
# parser
parser = argparse.ArgumentParser(
add_help=True,
description="ENTER_SCRIPT_DESCRIPTION_HERE",
)
# switch options
parser.add_argument("--debug", "-d", action="store_true")
parser.add_argument("--test", action="store_true", help=argparse.SUPPRESS)
parser.add_argument("--run", action="store_true", help="Run the script")
# process args
if len(sys.argv) > 1:
args, unknown = parser.parse_known_args()
return args, parser
else:
# print help and still proceed with --test and --debug
parser.print_help()
return sys.exit(1)
def logger_setup(options):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# set and configure console logging handler
ch = logging.StreamHandler()
if options.debug:
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)
# format
format_str = "%(asctime)s %(levelname)s %(message)s"
# format_str = "%(asctime)s %(process)d %(processName)s %(thread)d %(threadName)s %(levelname)s %(message)s"
formatter = logging.Formatter(format_str)
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
def memstats():
current, peak = tracemalloc.get_traced_memory()
logger.debug(
f"Current memory usage is {current / 10**6}MB; Peak was {peak / 10**6}MB"
)
return 0
def main():
# memory trace
if options.debug:
tracemalloc.start()
# run
if options.test:
memstats()
logger.info("test")
# stop memory trace
if options.debug:
memstats()
tracemalloc.stop()
return 0
if __name__ == "__main__":
# argument parser and logger
options, parser = parse_options()
logger = logger_setup(options)
main()