Skip to content

Commit ae235a4

Browse files
committed
fix: reorganize service layer and move data loading logic to separate module
1 parent 6db3783 commit ae235a4

5 files changed

Lines changed: 91 additions & 11 deletions

File tree

README.md

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,83 @@
1-
# python-app-template
2-
python-app-template
1+
2+
# Python App Template
3+
4+
A modern, modular Python application template with robust CI/CD, best practices, and synthwave terminal UI. This project demonstrates clean architecture, packaging, and workflow automation for professional Python development.
5+
6+
## Features
7+
- **Modular Structure**: src layout, clear separation of core, UI, models, and tests
8+
- **Synthwave Terminal UI**: Rich + pyfiglet for beautiful astronomical data display
9+
- **Robust CI/CD**: GitHub Actions for linting, testing, Docker multi-arch builds, Trivy scanning, and releases
10+
- **Best Practices**: Type hints, logging, config management, reusable workflows, DRY code
11+
- **Packaging**: pyproject.toml, src layout, testable modules, versioned releases
12+
13+
## Architecture
14+
15+
```
16+
python-app-template/
17+
├── src/sample_python_app/
18+
│ ├── core/ # Core logic: config, logging, display (thin wrappers)
19+
│ ├── models/ # Data models (Pydantic)
20+
│ ├── ui/ # Terminal UI (synthwave)
21+
│ ├── data_loader/ # Data loader module (API logic)
22+
│ ├── services/ # Service layer (business logic, imports data loader)
23+
│ └── main.py # Entry point
24+
├── tests/ # Unit tests
25+
├── data/ # Sample data
26+
├── Dockerfile # Containerization
27+
├── pyproject.toml # Packaging
28+
├── .github/workflows # CI/CD workflows
29+
```
30+
31+
## Frameworks & Libraries
32+
- **rich**: Terminal formatting and color
33+
- **pyfiglet**: ASCII art for synthwave look
34+
- **pydantic**: Data validation and settings
35+
- **httpx**: HTTP requests
36+
- **pytest**: Testing
37+
- **ruff, black, isort**: Linting and formatting
38+
39+
## CI/CD & Workflows
40+
41+
- **cache-uv-build.yaml**: Caches Python dependencies for fast builds
42+
- **ruff.yaml**: Lints code for style and quality
43+
- **pytest.yaml**: Runs unit tests
44+
- **docker-build-and-scan.yaml**: Multi-arch Docker builds, Trivy vulnerability scan
45+
- **ci-cd.yaml**: Orchestrates all jobs, triggers on push, PR, release, manual
46+
- **release.yaml**: Publishes releases on main/tags
47+
48+
### Docker Build Safety Features
49+
- **Multi-arch builds**: Uses Docker Buildx and QEMU for isolated, reproducible builds across amd64 and arm64
50+
- **Trivy vulnerability scan**: Automated scan for critical/high OS and library vulnerabilities before release
51+
- **Buildx isolation**: Ensures builds are run in a clean environment, reducing risk of contamination
52+
- **Secrets management**: DockerHub credentials and other secrets are handled securely via GitHub Actions
53+
- **Minimal base image**: Dockerfile uses slim Python base for reduced attack surface
54+
- **No root user**: (Recommended) Run containers as non-root for extra safety (customize Dockerfile as needed)
55+
56+
## Python Best Practices
57+
- **src layout**: Prevents import shadowing, enables clean packaging
58+
- **pyproject.toml**: Modern build system, dependency management
59+
- **Type hints**: Static analysis, clarity
60+
- **Logging**: Traceability for inputs, API calls, UI rendering
61+
- **Modularization**: UI logic in dedicated module, core as thin wrappers, data loader and service layer separated for clarity and maintainability
62+
- **Testing**: pytest for unit and integration tests
63+
- **CI/CD**: Automated lint, test, build, scan, release
64+
- **Multi-arch Docker**: Builds for amd64 and arm64
65+
66+
## Getting Started
67+
1. Clone the repo
68+
2. Install dependencies: `uv sync`
69+
3. Run tests: `uv run pytest`
70+
4. Run app: `uv run src/sample_python_app/main.py`
71+
- Data loading and business logic are now handled via the service layer and data_loader module
72+
5. Build Docker: `docker build -t python-app-template .`
73+
74+
## Contributing
75+
- Follow PEP8 and use ruff, black, isort
76+
- Write tests for new features
77+
- Use modular, reusable workflows
78+
79+
## License
80+
MIT
81+
82+
---
83+
This template is ideal for modern Python apps with clean architecture, robust automation, and beautiful terminal UI.
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
"""Export core modules for use in other modules."""
22

3-
43
from sample_python_app.core.config import Settings, settings, weather_settings
5-
from sample_python_app.core.data_loader import fetch_astronomical_data_from_api
64
from sample_python_app.core.display import display_astronomical_data
75
from sample_python_app.core.logging import setup_logger
86

97
__all__ = [
108
"settings",
119
"weather_settings",
1210
"setup_logger",
13-
"fetch_astronomical_data_from_api",
1411
"Settings",
1512
"display_astronomical_data",
1613
]

src/sample_python_app/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
from sample_python_app.core import (
1313
display_astronomical_data,
14-
fetch_astronomical_data_from_api,
1514
setup_logger,
1615
weather_settings,
1716
)
17+
from sample_python_app.services import fetch_astronomical_data_from_api
1818

1919
logger = setup_logger(mode="silent")
2020

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Service layer for data loading and related business logic."""
2+
3+
from sample_python_app.services.data_loader import fetch_astronomical_data_from_api
4+
5+
__all__ = ["fetch_astronomical_data_from_api"]

src/sample_python_app/core/data_loader.py renamed to src/sample_python_app/services/data_loader.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"""Handles loading and validating weather.gov astronomical data from file."""
1+
"""Handles loading and validating weather.gov astronomical data from file and API."""
22

33
import httpx
44
from pydantic import ValidationError
55

6-
from sample_python_app.core.logging import setup_logger
6+
from sample_python_app.core import setup_logger
77
from sample_python_app.models import AstronomicalData, WeatherGovFeature
88

99

@@ -38,6 +38,3 @@ def fetch_astronomical_data_from_api(lat: float, lon: float) -> AstronomicalData
3838
except ValidationError as e:
3939
logger.error(f"Data validation error: {e}")
4040
raise
41-
except httpx.HTTPError as e:
42-
logger.error(f"Failed to fetch or validate data from API: {e}")
43-
raise

0 commit comments

Comments
 (0)