This project implements a custom numerical type in Python called AdvancedNumber
that demonstrates the use of Python's special methods (magic methods) to create a rich, fully-featured numeric type.
- Initialize with integer or float values
- Human-readable string representation
- Developer-focused representation
- Addition (
+
) - Subtraction (
-
) - Multiplication (
*
) - Division (
/
) - Modulus (
%
) - Works with both
AdvancedNumber
objects and regular numbers
- Less than (
<
) - Less than or equal to (
<=
) - Greater than (
>
) - Greater than or equal to (
>=
) - Equal to (
==
) - Not equal to (
!=
)
- Hashable (can be used in sets and as dictionary keys)
- Boolean conversion
- Callable objects (returns square of the value)
- Custom string formatting
- Cleanup notification on object destruction
- Clone this repository:
git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>
- Create a virtual environment (optional but recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies:
pip install pytest
# Create AdvancedNumber objects
num1 = AdvancedNumber(5)
num2 = AdvancedNumber(3)
# Arithmetic operations
result = num1 + num2 # AdvancedNumber(8)
product = num1 * 2 # AdvancedNumber(10)
# Comparison
is_greater = num1 > num2 # True
# Callable behavior
squared = num1() # 25
# Custom formatting
formatted = f"{num1:.2f}" # "5.00"
hex_format = f"{num1:#x}" # "0x5"
Run the tests using pytest:
pytest
.
├── advanced_number.py # Main implementation
├── test_assignment.py # Test suite
├── .gitignore # Git ignore file
├── README.md # This file
└── .github/
└── workflows/
└── python-tests.yml # GitHub Actions workflow
- Python 3.9 or higher
- pytest (for running tests)
This is an assignment project. Please do not contribute directly.