Source code for weighted_sample_statistics.main

"""
This is a skeleton file that can serve as a starting point for a Python
console script.

Besides console scripts, the header (i.e., until ``_logger``...) of this file can
also be used as a template for Python modules.

Note:
    This file can be renamed depending on your needs or safely removed if not needed.

References:
    - https://setuptools.pypa.io/en/latest/userguide/entry_point.html
    - https://pip.pypa.io/en/stable/reference/pip_install
"""

import argparse
import logging
import sys

from weighted_sample_statistics import __version__

__author__ = "Eelco van Vliet"
__copyright__ = "Eelco van Vliet"
__license__ = "MIT"

from weighted_sample_statistics import logger as _logger


# ---- Python API ----
# The functions defined in this section can be imported by users in their
# Python scripts/interactive interpreter

# ---- CLI ----
# The functions defined in this section are wrappers around the main Python
# API allowing them to be called directly from the terminal as a CLI
# executable/script.


[docs] def parse_args(args): """Parse command line parameters Args: args (List[str]): command line parameters as a list of strings (for example, ``["--help"]``). Returns: obj:`argparse.Namespace`: command line parameters namespace """ parser = argparse.ArgumentParser(description="Just a Fibonacci demonstration") parser.add_argument( "--version", action="version", version=f"weighted_sample_statistics {__version__}", ) parser.add_argument(dest="n", help="n-th Fibonacci number", type=int, metavar="INT") parser.add_argument( "-v", "--verbose", dest="loglevel", help="set loglevel to INFO", action="store_const", const=logging.INFO, ) parser.add_argument( "-vv", "--very-verbose", dest="loglevel", help="set loglevel to DEBUG", action="store_const", const=logging.DEBUG, ) return parser.parse_args(args)
[docs] def setup_logging(loglevel): """Setup basic logging Args: loglevel (int): minimum loglevel for emitting messages """ log_format = "[%(asctime)s] %(levelname)s:%(name)s:%(message)s" logging.basicConfig( level=loglevel, stream=sys.stdout, format=log_format, datefmt="%Y-%m-%d %H:%M:%S", )
[docs] def main(args): """Wrapper function Args: args (List[str]): command line parameters as a list of strings (for example, ``["--verbose", "42"]``). """ args = parse_args(args) setup_logging(args.loglevel) _logger.debug("Script ends here")
[docs] def run(): """Calls: func:`main` passing the CLI arguments extracted from: obj:`sys.argv` This function can be used as an entry point to create console scripts with setuptools. """ main(sys.argv[1:])
if __name__ == "__main__": # ^ This is a guard statement that will prevent the following code from # being executed in the case someone imports this file instead of # executing it as a script. # https://docs.python.org/3/library/__main__.html # After installing your project with pip, users can also run your Python # modules as scripts via the ``-m`` flag, as defined in PEP 338:: # # python -m weighted_sample_statistics.skeleton 42 # run()