Skip to content

Reference

This is the API reference for the python-template package.

python_template.core

Core functionality for the Python template package.

Calculator

A simple calculator class for demonstration purposes.

add

add(a: int | float, b: int | float) -> int | float

Add two numbers.

Parameters:

Name Type Description Default
a int | float

The first number.

required
b int | float

The second number.

required

Returns:

Type Description
int | float

The sum of a and b.

Examples:

>>> calc = Calculator()
>>> calc.add(2, 3)
5
>>> calc.add(2.5, 3.7)
6.2
Source code in src/python_template/core.py
def add(self, a: int | float, b: int | float) -> int | float:
    """Add two numbers.

    Args:
        a: The first number.
        b: The second number.

    Returns:
        The sum of a and b.

    Examples:
        >>> calc = Calculator()
        >>> calc.add(2, 3)
        5
        >>> calc.add(2.5, 3.7)
        6.2
    """
    return a + b

divide

divide(a: int | float, b: int | float) -> float

Divide two numbers.

Parameters:

Name Type Description Default
a int | float

The dividend.

required
b int | float

The divisor.

required

Returns:

Type Description
float

The quotient of a and b.

Raises:

Type Description
ValueError

If b is zero.

Examples:

>>> calc = Calculator()
>>> calc.divide(10, 2)
5.0
>>> calc.divide(7, 2)
3.5
Source code in src/python_template/core.py
def divide(self, a: int | float, b: int | float) -> float:
    """Divide two numbers.

    Args:
        a: The dividend.
        b: The divisor.

    Returns:
        The quotient of a and b.

    Raises:
        ValueError: If b is zero.

    Examples:
        >>> calc = Calculator()
        >>> calc.divide(10, 2)
        5.0
        >>> calc.divide(7, 2)
        3.5
    """
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

multiply

multiply(a: int | float, b: int | float) -> int | float

Multiply two numbers.

Parameters:

Name Type Description Default
a int | float

The first number.

required
b int | float

The second number.

required

Returns:

Type Description
int | float

The product of a and b.

Examples:

>>> calc = Calculator()
>>> calc.multiply(4, 5)
20
>>> calc.multiply(2.5, 4)
10.0
Source code in src/python_template/core.py
def multiply(self, a: int | float, b: int | float) -> int | float:
    """Multiply two numbers.

    Args:
        a: The first number.
        b: The second number.

    Returns:
        The product of a and b.

    Examples:
        >>> calc = Calculator()
        >>> calc.multiply(4, 5)
        20
        >>> calc.multiply(2.5, 4)
        10.0
    """
    return a * b

subtract

subtract(a: int | float, b: int | float) -> int | float

Subtract two numbers.

Parameters:

Name Type Description Default
a int | float

The first number.

required
b int | float

The second number.

required

Returns:

Type Description
int | float

The difference of a and b.

Examples:

>>> calc = Calculator()
>>> calc.subtract(5, 3)
2
>>> calc.subtract(5.5, 2.2)
3.3
Source code in src/python_template/core.py
def subtract(self, a: int | float, b: int | float) -> int | float:
    """Subtract two numbers.

    Args:
        a: The first number.
        b: The second number.

    Returns:
        The difference of a and b.

    Examples:
        >>> calc = Calculator()
        >>> calc.subtract(5, 3)
        2
        >>> calc.subtract(5.5, 2.2)
        3.3
    """
    return a - b

greet

greet(name: str, greeting: str = 'Hello') -> str

Greet someone with a custom message.

Parameters:

Name Type Description Default
name str

The name of the person to greet.

required
greeting str

The greeting message to use. Defaults to "Hello".

'Hello'

Returns:

Type Description
str

A formatted greeting string.

Examples:

>>> greet("World")
'Hello, World!'
>>> greet("Alice", "Hi")
'Hi, Alice!'
Source code in src/python_template/core.py
def greet(name: str, greeting: str = "Hello") -> str:
    """Greet someone with a custom message.

    Args:
        name: The name of the person to greet.
        greeting: The greeting message to use. Defaults to "Hello".

    Returns:
        A formatted greeting string.

    Examples:
        >>> greet("World")
        'Hello, World!'
        >>> greet("Alice", "Hi")
        'Hi, Alice!'
    """
    return f"{greeting}, {name}!"

python_template.cli

Command-line interface for the python-template package.

create_parser

create_parser() -> argparse.ArgumentParser

Create the command-line argument parser.

Source code in src/python_template/cli.py
def create_parser() -> argparse.ArgumentParser:
    """Create the command-line argument parser."""
    parser = argparse.ArgumentParser(
        description="Python Template CLI",
        prog="python-template",
    )

    subparsers = parser.add_subparsers(dest="command", help="Available commands")

    # Greet command
    greet_parser = subparsers.add_parser("greet", help="Greet someone")
    greet_parser.add_argument("name", help="Name to greet")
    greet_parser.add_argument(
        "--greeting",
        default="Hello",
        help="Greeting message (default: Hello)",
    )

    # Calculator command
    calc_parser = subparsers.add_parser("calc", help="Perform calculations")
    calc_parser.add_argument("operation", choices=["add", "sub", "mul", "div"])
    calc_parser.add_argument("a", type=float, help="First number")
    calc_parser.add_argument("b", type=float, help="Second number")

    return parser

main

main(argv: list[str] | None = None) -> int

Main entry point for the CLI.

Source code in src/python_template/cli.py
def main(argv: list[str] | None = None) -> int:
    """Main entry point for the CLI."""
    parser = create_parser()
    args = parser.parse_args(argv)

    if args.command == "greet":
        greeting_result = greet(args.name, args.greeting)
        print(greeting_result)
        return 0

    elif args.command == "calc":
        calc = Calculator()
        try:
            calc_result: float
            if args.operation == "add":
                calc_result = calc.add(args.a, args.b)
            elif args.operation == "sub":
                calc_result = calc.subtract(args.a, args.b)
            elif args.operation == "mul":
                calc_result = calc.multiply(args.a, args.b)
            elif args.operation == "div":
                calc_result = calc.divide(args.a, args.b)
            else:
                print(f"Unknown operation: {args.operation}", file=sys.stderr)
                return 1

            print(f"Result: {calc_result}")
            return 0

        except ValueError as e:
            print(f"Error: {e}", file=sys.stderr)
            return 1

    else:
        parser.print_help()
        return 1