A Python application that calculates the present value of a loan based on periodic payments, interest rate, and loan term.
Python Version: 3.14 (latest stable)
Package Manager: uv (recommended) or pip
Testing: pytest with 80% coverage requirement
# macOS/Linux
curl -LsSf https://astral.sh/uv/install.sh | sh
# OR via Homebrew
brew install uv
# OR via pip
pip install uvuv pip install -r requirements.txtuv run pytest test_loan_calculator.py -vuv run pytest test_loan_calculator.py --cov=loan_calculator --cov-report=htmlloan-calculator/
βββ .github/
β βββ workflows/
β βββ python-ci.yml # GitHub Actions CI/CD pipeline
βββ loan_calculator.py # Main calculator module
βββ test_loan_calculator.py # Pytest test suite (30 tests)
βββ requirements.txt # Python dependencies
βββ pyproject.toml # Project configuration
βββ .gitignore # Git ignore rules
βββ README.md # This file
python loan_calculator.pyThen enter:
- Monthly payment amount
- Annual interest rate (as percentage)
- Loan term in years
from loan_calculator import LoanCalculator
# Calculate PV for a $1,000/month payment at 5% annual rate for 5 years
pv = LoanCalculator.calculate_present_value(
payment=1000,
annual_rate=0.05,
periods=60,
periods_per_year=12
)
print(f"Present Value: ${pv:,.2f}")uv run pytest test_loan_calculator.py -vuv run pytest test_loan_calculator.py --cov=loan_calculator --cov-report=htmlopen htmlcov/index.html # macOS
xdg-open htmlcov/index.html # LinuxOn every push/PR to main or develop:
- β Runs 30 tests on Python 3.14
- β Generates coverage reports (must be β₯80%)
- β Checks code quality with flake8
- β Verifies formatting with black
- β Checks import sorting with isort
- β Uploads coverage artifacts
- β Displays coverage in GitHub Actions summary
- Copy all files to your repository
- Commit and push:
git add . git commit -m "Add loan calculator with CI/CD pipeline" git push
- View pipeline: Go to GitHub β Actions tab
- Present value of annuity formula:
PV = PMT Γ [(1 - (1 + r)^-n) / r] - Handles zero interest rate edge case
- Supports multiple payment frequencies (annual, semi-annual, quarterly, monthly, weekly, daily)
- Results rounded to 2 decimal places
- Payment: Must be positive, β€ $1,000,000,000
- Interest Rate: Must be non-negative, β€ 100% per period
- Periods: Must be integer between 1 and 600
- Periods per Year: Must be 1, 2, 4, 12, 52, or 365
- 30 comprehensive test cases
- 82%+ code coverage
- Tests validation, calculations, edge cases, and consistency
pv = LoanCalculator.calculate_present_value(
payment=2000,
annual_rate=0.04,
periods=360, # 30 years Γ 12 months
periods_per_year=12
)
# Result: $418,922.48pv = LoanCalculator.calculate_present_value(
payment=500,
annual_rate=0.06,
periods=60, # 5 years Γ 12 months
periods_per_year=12
)
# Result: $25,863.78pv = LoanCalculator.calculate_present_value(
payment=3000,
annual_rate=0.08,
periods=40, # 10 years Γ 4 quarters
periods_per_year=4
)
# Result: $81,725.67# Format code with black
uv run black loan_calculator.py test_loan_calculator.py
# Check with flake8
uv run flake8 loan_calculator.py test_loan_calculator.py --max-line-length=100
# Sort imports with isort
uv run isort loan_calculator.py test_loan_calculator.py- All tests pass:
uv run pytest - Coverage β₯80%:
uv run pytest --cov=loan_calculator - Code formatted:
uv run black . - No linting errors:
uv run flake8 . - Imports sorted:
uv run isort .
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes
- Run tests:
uv run pytest - Commit:
git commit -m 'Add amazing feature' - Push:
git push origin feature/amazing-feature - Open a Pull Request
The CI/CD pipeline will automatically run on your PR!
- Python β₯3.14
- pytest β₯7.4.0
- pytest-cov β₯4.1.0
- coverage[toml] β₯7.3.0
This project is open source and available under the MIT License.
# Make sure dependencies are installed
uv pip install -r requirements.txt
# Run tests with verbose output
uv run pytest test_loan_calculator.py -v# View detailed coverage report
uv run pytest --cov=loan_calculator --cov-report=html
open htmlcov/index.html
# See which lines aren't covered
uv run pytest --cov=loan_calculator --cov-report=term-missing- Check the Actions tab for detailed error messages
- Ensure all files are committed (especially
.github/workflows/python-ci.yml) - Verify Python 3.14 is specified in the workflow
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Or use pip instead
pip install -r requirements.txt
pytest test_loan_calculator.py -vMade with β€οΈ using Python 3.14 and uv