Package: scripthelper
scripthelperHelper module for simple command line Python scripts.Basic usageexample1.py:#!/usr/bin/env python3importscripthelperlogger=scripthelper.bootstrap()logger.critical("critical message")logger.error("error message")logger.warning("warning message")logger.info("info message")logger.verbose("verbose message")logger.debug("debug message")logger.spam("spam message")It just works. Try--verboseand--quietcommand line options, too.
It uses colored log messages on a terminal.
See--helpfor more information.Adding other command line parametersexample2.py:#!/usr/bin/env python3importscripthelperscripthelper.add_argument("-n","--name",help="Name to greet")logger,args=scripthelper.bootstrap_args()ifargs.name:logger.debug("Name was provided")logger.info(f"Hello{args.name}")else:logger.warning("Name was not provided")For bigger scripts it is good idea to have the logger at the very beginning, and encapsulate the argument parsing phase, which is typically in the main function:example2b.py:#!/usr/bin/env python3importscripthelperlogger=scripthelper.getLogger()defgreet(name):logger.info(f"Hello{name}")defmain():scripthelper.add_argument("--name",default="World")args=scripthelper.initialize()greet(args.name)main()Progressbar works with logging, tooexample3.py:#!/usr/bin/env python3importscripthelperimporttimelogger=scripthelper.bootstrap()logger.info("Doing the calculations...")foriinscripthelper.progressbar(range(100)):ifi%20==0:logger.verbose(f"Iteration{i}")ifi%5==0:logger.debug(f"Iteration{i}")iflogger.isEnabledFor(scripthelper.SPAM):logger.spam(f"Iteration{i}")time.sleep(0.01)logger.info("Done")It is automatically disabled on non-ttystderrby default.Extended log levels can be used in modulesexample4.py:#!/usr/bin/env python3importscripthelperimportexample4modulescripthelper.bootstrap()example4module.do_the_things()example4module.py:#!/usr/bin/env python3importscripthelperlogger=scripthelper.getLogger(__name__)defdo_the_things():logger.verbose("Calling logger.verbose raises an exception if it does not work.")logger.info("Hello from a module.")You can easily preserve logs in filesexample5.py:#!/usr/bin/env python3importscripthelperlogger=scripthelper.bootstrap()scripthelper.setup_file_logging()logger.critical("critical message")logger.error("error message")logger.warning("warning message")logger.info("info message")logger.verbose("verbose message")logger.debug("debug message")logger.spam("spam message")It handles exceptions, warningsexample6.py:#!/usr/bin/env python3importscripthelperscripthelper.bootstrap()scripthelper.warn("This user warning will be captured.")this_variable="will be displayed in stack trace"as_well_as="the other variables"raiseRuntimeError("This exception should be handled.")The local variables will be displayed in stack trace, for example:WARNING example6.py:6: UserWarning: This user warning will be captured.
  scripthelper.warn("This user warning will be captured.")

CRITICAL Uncaught RuntimeError: This exception should be handled.
File "example6.py", line 10, in <module>
    6    scripthelper.warn("This user warning will be captured.")
    7
    8    this_variable = "will be displayed in stack trace"
    9    as_well_as = "the other variables"
--> 10   raise RuntimeError("This exception should be handled.")
    ..................................................
     this_variable = 'will be displayed in stack trace'
     as_well_as = 'the other variables'
    ..................................................Has built-in colored pretty printerexample7.py:#!/usr/bin/env python3importscripthelperfromdataclassesimportdataclassscripthelper.bootstrap()@dataclassclassItem:name:strvalue:intsomething={"string":"value1","bool":True,"none":None,"integer":1234,"item":Item("name",999),}scripthelper.pp(something)Has built-in persisted state handlerThe state is persisted immediately in the background in YAML. Mutable objects (list,dict) also can be used.example9.py:#!/usr/bin/env python3importscripthelperlogger=scripthelper.bootstrap()state=scripthelper.PersistedState(processed_id=0,to_remember=[])state.processed_id+=1state.to_remember.append(f"Element{state.processed_id}")whilelen(state.to_remember)>2:state.to_remember.pop(0)logger.info(f"Processing item #{state.processed_id}")foriteminstate.to_remember:logger.info(f"-{item}")$ python3 example9.py
INFO example9 Processing item #1
INFO example9 - Element 1

$ python3 example9.py
INFO example9 Processing item #2
INFO example9 - Element 1
INFO example9 - Element 2

$ python3 example9.py
INFO example9 Processing item #3
INFO example9 - Element 2
INFO example9 - Element 3Helps issuing a warning only onceexample10.py:#!/usr/bin/env python3importscripthelperscripthelper.bootstrap()for_inrange(10):scripthelper.warning_once("Item #12 has some errors")