API Reference#
This section documents the public API exposed by the python-project-template package.
Calculator utilities for applying and composing operations. |
|
Operation container and a few example math operations. |
|
Operation registry used to register and lookup operations by name. |
|
Custom exceptions used across the mini calculator package. |
|
Tiny helper utilities used by the examples and tests. |
Calculator utilities for applying and composing operations.
The Calculator provides a thin layer over OperationRegistry.
It supports applying named operations, composing unary operations into a
callable, and a small ‘chain’ helper for mixed sequences of unary/binary
operations used by the examples and tests.
Keep the behaviour minimal: methods raise OperationError for
operation-related failures.
- class python_project_template_AS.calculator.Calculator(registry=None)[source]
Bases:
objectCalculator using an OperationRegistry.
- Parameters:
registry (OperationRegistry | None)
- apply(op_name, *args)[source]
Apply a named operation with arguments.
- Return type:
Any- Parameters:
op_name (str)
args (Any)
- chain(sequence, initial)[source]
Apply a sequence of operations and values starting from initial.
- Return type:
Any- Parameters:
sequence (Iterable[str | int])
initial (Any)
- compose(ops, *, left_to_right=True)[source]
Compose unary operations into a single callable.
- Return type:
Callable[[Any],Any]- Parameters:
ops (Iterable[str])
left_to_right (bool)
Operation container and a few example math operations.
The Operation is a tiny callable wrapper that validates arity and
delegates to the underlying function. A small set of convenience
instances (ADD, SUB, MUL, DIV, NEG, SQR) are provided for tests/examples.
- class python_project_template_AS.operations.Operation(name, func, arity=1)[source]
Bases:
objectNamed callable with arity check.
- Parameters:
name (str)
func (Callable[[...], Any])
arity (int)
- name
Operation name.
- Type:
str
- func
Operation function.
- Type:
Callable
- arity
Expected argument count.
- Type:
int
-
arity:
int= 1
-
func:
Callable[...,Any]
-
name:
str
- python_project_template_AS.operations.add(a, b)[source]
Return the sum of two numbers.
- python_project_template_AS.operations.mul(a, b)[source]
Return the product of two numbers.
- python_project_template_AS.operations.neg(a)[source]
Return the numeric negation of a value.
- python_project_template_AS.operations.safe_div(a, b)[source]
Divide a by b, raising
OperationErroron zero division.
- python_project_template_AS.operations.square(a)[source]
Return the square of a value.
- python_project_template_AS.operations.sub(a, b)[source]
Return the difference of two numbers.
Operation registry used to register and lookup operations by name.
The OperationRegistry is intentionally minimal: register operations,
retrieve them by name, list registered names, and update from another
registry. It raises RegistryError for lookup/registration problems.
- class python_project_template_AS.registry.OperationRegistry[source]
Bases:
objectA simple name->Operation registry.
Example
reg = OperationRegistry() reg.register(Operation(‘add’, func, arity=2)) op = reg.get(‘add’)
- get(name)[source]
Return a registered Operation by name.
- Raises:
RegistryError – if the name is unknown.
- Return type:
- Parameters:
name (str)
- list_ops()[source]
Return a list of registered operation names.
- Return type:
Iterable[str]
- register(op, *, replace=False)[source]
Register an
Operation.- Parameters:
op (
Operation) – operation instance to register.replace (
bool) – if False (default) raise on duplicate names.
- Return type:
None
- update(other)[source]
Update the registry with operations from another registry.
- Return type:
None- Parameters:
other (OperationRegistry)
Custom exceptions used across the mini calculator package.
All exceptions inherit from CalculatorError so callers may catch the
base class for broad error handling. Use the more specific subclasses for
programmatic checks in tests or higher-level code.
- exception python_project_template_AS.exceptions.CalculatorError[source]
Bases:
ExceptionBase exception for calculator errors.
- exception python_project_template_AS.exceptions.OperationError[source]
Bases:
CalculatorErrorRaised for operation failures (e.g., wrong arity, invalid input).
- exception python_project_template_AS.exceptions.RegistryError[source]
Bases:
CalculatorErrorRaised for registry errors (duplicate or missing name).
Tiny helper utilities used by the examples and tests.
Only lightweight predicates live here to keep the package dependency-free and easy to read.
- python_project_template_AS.utils.is_number(x)[source]
Return True if x is int or float.
- Return type:
bool- Parameters:
x (Any)