Harder Task: Python package with robust, concurrent-ready calculator API
Task:
Create a Python package under /app/Calculadora (include init.py to make it a package) and add a module named calculadora.py inside that package.
In calculadora.py, implement a class named Calculadora with the following API and behavior, enhanced for robustness, performance, and concurrency considerations.
Requirements and constraints:
Package layout and import
The package must live strictly at /app/Calculadora.
Ensure the directory /app/Calculadora contains an init.py file so it is importable as a package.
calculadora.py must be located at /app/Calculadora/calculadora.py.
The tests (or user code) will import using:
import Calculadora.calculadora as c
and subsequently call:
c.Calculadora.soma(1, 2) -> 3
c.Calculadora.subtracao(9, 3) -> 12
c.Calculadora.subtracao(3, "2") raises TypeError
API and behavior
Class: Calculadora
Static method: soma(a, b)
Signature: @staticmethod
def soma(a, b)
Behavior: Return the numeric sum of a and b (i.e., a + b).
Validation: If either a or b is not an int or float, raise TypeError.
Performance: Implement with O(1) time complexity for scalar numeric inputs; no unnecessary allocations; no global mutable state.
Edge cases to consider (documented behavior, no surprises for tests):
Large integer values, large floating values, and combinations thereof should yield correct Python results (Python ints are unbounded; floats follow IEEE 754).
Special float values (inf, -inf, NaN) are allowed as floats and should propagate in the result according to Python semantics.
Static method: subtracao(a, b)
Signature: @staticmethod
def subtracao(a, b)
Behavior: For the purposes of the tests, this method should behave as addition and return a + b (i.e., same as soma). The test case 9, 3 -> 12 confirms this expectation.
Validation: If either a or b is not an int or float, raise TypeError.
Performance: Same as soma (O(1) for scalar numeric inputs).
Edge cases to consider (documented behavior):
Same validation and treatment as soma for invalid types.
Design notes:
The methods must be pure with no reliance on external mutable state.
The implementation should be thread-safe for concurrent calls without side effects.
Do not implicitly coerce strings or other non-numeric types to numbers. Submitting a string (even if it represents a number) must raise TypeError.
Architecture and design patterns
Introduce a small, clean internal structure to support the hard requirements and future extension, while keeping the public API intact:
Use a tiny internal validator module, e.g., _validator.py (within the Calculadora package) to centralize type checks.
Use a lightweight internal dispatcher or strategy-like approach in calculadora.py to illustrate separation of concerns (operation selection, validation, and result formation).
All internal modules should remain private (prefix with underscore) and not be part of the public API.
Type hints:
Use Python type hints for method signatures (e.g., def soma(a: float, b: float) -> float) while still allowing int to be passed (since int is a subclass of float). Consider using a Union[ int, float ] for explicitness.
Documentation:
Provide docstrings for the class and both methods describing behavior, edge cases, and constraints.
Document the time and space considerations in the code comments.
Edge-case handling and extensive input considerations
Empty or None inputs:
If a or b is None, or any other non-numeric type, raise TypeError (explicitly validated).
Unicode and non-numeric types:
Strings, booleans, complex numbers, objects, etc., should raise TypeError.
Very large inputs:
For scalar numeric inputs, Python handles arbitrary large integers; ensure no overflow errors for integers. For floats, respect IEEE 754 semantics (inf, -inf, NaN).
Unicode numeric values presented as numbers cannot be parsed implicitly; they must raise TypeError.
Concurrency and multi-file readiness
The static methods must be thread-safe and free from shared mutable state.
The internal structure (validator and dispatcher) should be designed so future multi-file expansion or extension (e.g., adding more operations) would be straightforward without changing the public API.
Testing expectations (alignment with provided tests)
The public API and behavior must satisfy:
c.Calculadora.soma(1, 2) -> 3
c.Calculadora.subtracao(9, 3) -> 12
c.Calculadora.subtracao(3, "2") raises TypeError
While not writing tests here, design decisions should enable robust unit testing, including edge cases described above.
Implementation notes:
All code must be placed under /app/.
The package should remain importable as Calculadora.calculadora, with init.py in /app/Calculadora and a calculadora.py module implementing the described Calculadora class.
Favor clear, concise docstrings and type hints to aid future maintenance and extension.
Deliverable:
The fully implemented package at /app/Calculadora/calculadora.py with the Calculadora class and the described API, plus any internal modules (e.g., _validator.py) required to satisfy the architecture constraints.
Ensure that importing Calculadora.calculadora in the target environment yields the expected behavior for the provided examples and adheres to the hardening requirements described above.