Skip to content

API Reference

A template repository for a Python package created by Catalyst Cooperative.

A dummy module so coverage has something to cover.

do_something(a=0, b=0)

A dummy function that does something.

Source code in src/cheshire/dummy.py
def do_something(a: int = 0, b: int = 0) -> int:
    """A dummy function that does something."""
    logger.info("Don't just do something, stand there!")
    c = a + b
    if a == 2 and b == 2:
        c = 5
    return c

A skeleton of a command line interface to be deployed as an entry point script.

It takes two numbers and does something to them, printing out the result.

main()

Demonstrate a really basic command line interface (CLI) that takes arguments.

Source code in src/cheshire/cli.py
def main() -> int:
    """Demonstrate a really basic command line interface (CLI) that takes arguments."""
    logging.basicConfig(
        format="%(asctime)s [%(levelname)8s] %(name)s:%(lineno)s %(message)s",
        level=logging.INFO,
    )

    args = parse_command_line(sys.argv)
    caligula = do_something(a=args.alpha, b=args.beta)
    print(
        "If you are a man Winston, you are the last man: "
        f"{args.alpha} + {args.beta} = {caligula}"
    )
    return 0

parse_command_line(argv)

Parse command line arguments. See the -h option for details.

Parameters:

Name Type Description Default
argv str

Command line arguments, including caller filename.

required

Returns:

Name Type Description
dict Namespace

Dictionary of command line arguments and their parsed values.

Source code in src/cheshire/cli.py
def parse_command_line(argv: list[str]) -> argparse.Namespace:
    """Parse command line arguments. See the -h option for details.

    Args:
        argv (str): Command line arguments, including caller filename.

    Returns:
        dict: Dictionary of command line arguments and their parsed values.

    """

    def formatter(prog) -> argparse.HelpFormatter:
        """This is a hack to create HelpFormatter with a particular width."""
        return argparse.HelpFormatter(prog, width=88)

    # Use the module-level docstring as the script's description in the help message.
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=formatter)

    parser.add_argument(
        "-a",
        "--alpha",
        type=int,
        help="An integer to do something to. Defaults to two (2).",
        default=2,
    )
    parser.add_argument(
        "-b",
        "--beta",
        type=int,
        help="Another integer to do something to. Defaults to two (2).",
        default=2,
    )

    arguments = parser.parse_args(argv[1:])
    return arguments